I have a single page web app that is written in Polymer Dart, now it's time to write some tests for it, (not a fan of test). Since Selenium IDE/WebDriver doesn't support shadow DOM, I write it with dart's unittest package, like this:
<!-- index.html (the single page app) -->
<title>My App</title>
<body>
<my-app></my-app>
<script type="application/dart" src="index_test.dart"></script>
</body>
// index_test.dart
import 'package:unittest/unittest.dart';
import 'dart:html';
import 'package:polymer/polymer.dart';
void main() {
initPolymer().run(allTests);
}
void allTests() {
test('First page should be "First Page"', () {
expect(window.location.pathname, equals("/somepath"));
expect(document.title, equals("First Page"));
});
test('Load Page 2', () {
document
.querySelector('my-app').shadowRoot
.querySelector('paper-button')
.click(); // load next page (without refresh)
expect(document.title, equals("Second Page"));
});
}
The problem with this method is, when index.html
get loaded, it's title is "My App", then, <my-app>
starts to work, it loads some JSON from server, changes the title and url in the location bar based on the JSON, say it changes title to "First Page", because when the JSON will load complete is not controlled by dart, (it's controlled by internet speed), so sometimes the test will success, (JSON loaded really quick), sometimes the test fails, (internet speed is low).
My question is, is there a way to tell unittest to run the test at the "right" time, other than use some timer to wait? Better, can it be done with out touching <my-app>
's codes, i.e., separate test codes from application logic?