Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Unit testing framework for the Launchpad AJAX test suite.
- var client = new LaunchpadClient();
- /* Object use to synchronize tests involving async calls.
- */
- function TestSynchronizer(name) {
- this.name = name;
- this.result = null;
- }
- /* Create the synchronization node, that should make the wait() caller
- * return.
- */
- TestSynchronizer.prototype.sync = function (result) {
- console.log(this.name + '.sync() called.');
- this.result = result;
- var body = document.getElementById('document');
- var node = document.createElement('span');
- node.setAttribute('id', this.name);
- node.id = this.name;
- body.appendChild(node);
- console.log('Node ' + this.name + ' added.');
- };
- /* Wait for the synchronization node to appear.
- * It returns the result object passed to sync().
- */
- TestSynchronizer.prototype.wait = function () {
- console.log(this.name + '.wait() called.');
- windmill.jsTest.actions.waits.forElement({id: this.name});
- console.log(this.name + '.wait() will return:' + this.result);
- return this.result;
- };
- /* Remove the synchronization node, so that the test
- * can be run again.
- */
- TestSynchronizer.prototype.cleanup = function () {
- console.log(this.name + '.cleanup() called.');
- var sync_node = document.getElementById(this.name);
- sync_node.parentNode.removeChild(sync_node);
- console.log('Removed ' + this.name + ' node.');
- };
- var test_no_such_url = new function() {
- //Save to work-around this not being accessible from closure.
- var test = this;
- this.sync = new TestSynchronizer('test_no_such_url');
- this.test_xhr = function () {
- client.get("no-such-url", {
- success: function (response) {
- console.log('Request succeeded.');
- test.sync.sync(response);
- },
- failure: function (response) {
- console.log('Request failed with status ' + response);
- test.sync.sync(response);
- }
- });
- };
- this.test_asserts = [
- {method: "waits.forElement", params: {id: test.sync.name}},
- function () {
- console.log('Testing response...');
- var response = test.sync.result;
- jum.assertEquals(response.status, 404);
- }
- ];
- this.teardown = function () { test.sync.cleanup(); };
- }();
- /* Output of the test:
- Number of tests run: 3
- Number of tests failures: 1
- Test failures:
- test_no_such_url.test_asserts: response is null
- Test: test_no_such_url.teardown
- Test Result:true
- Action: waits.forElement
- Parameters: {"id":"test_no_such_url","orig":"js","test":"undefined"}
- Test Result: true
- Action: function
- Parameters: "(JavaScript code is being executed)"
- Test Result: true
- Log output on Firebug console:
- Testing response...
- Request failed with status 0
- test_no_such_url.sync() called.
- Node test_no_such_url added.
- test_no_such_url.cleanup() called.
- Removed test_no_such_url node.
- This seems to imply that,
- test_asserts is being run first. The waitForElements() return immediately and processing moves on to the function(), before the node has appeared.
- */
Add Comment
Please, Sign In to add comment