Guest User

Untitled

a guest
May 24th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. // Unit testing framework for the Launchpad AJAX test suite.
  2. var client = new LaunchpadClient();
  3.  
  4. /* Object use to synchronize tests involving async calls.
  5. */
  6. function TestSynchronizer(name) {
  7. this.name = name;
  8. this.result = null;
  9. }
  10.  
  11. /* Create the synchronization node, that should make the wait() caller
  12. * return.
  13. */
  14. TestSynchronizer.prototype.sync = function (result) {
  15. console.log(this.name + '.sync() called.');
  16. this.result = result;
  17. var body = document.getElementById('document');
  18. var node = document.createElement('span');
  19. node.setAttribute('id', this.name);
  20. node.id = this.name;
  21. body.appendChild(node);
  22. console.log('Node ' + this.name + ' added.');
  23. };
  24.  
  25. /* Wait for the synchronization node to appear.
  26. * It returns the result object passed to sync().
  27. */
  28. TestSynchronizer.prototype.wait = function () {
  29. console.log(this.name + '.wait() called.');
  30. windmill.jsTest.actions.waits.forElement({id: this.name});
  31. console.log(this.name + '.wait() will return:' + this.result);
  32. return this.result;
  33. };
  34.  
  35. /* Remove the synchronization node, so that the test
  36. * can be run again.
  37. */
  38. TestSynchronizer.prototype.cleanup = function () {
  39. console.log(this.name + '.cleanup() called.');
  40. var sync_node = document.getElementById(this.name);
  41. sync_node.parentNode.removeChild(sync_node);
  42. console.log('Removed ' + this.name + ' node.');
  43. };
  44.  
  45. var test_no_such_url = new function() {
  46. //Save to work-around this not being accessible from closure.
  47. var test = this;
  48. this.sync = new TestSynchronizer('test_no_such_url');
  49.  
  50. this.test_xhr = function () {
  51. client.get("no-such-url", {
  52. success: function (response) {
  53. console.log('Request succeeded.');
  54. test.sync.sync(response);
  55. },
  56. failure: function (response) {
  57. console.log('Request failed with status ' + response);
  58. test.sync.sync(response);
  59. }
  60. });
  61. };
  62.  
  63. this.test_asserts = [
  64. {method: "waits.forElement", params: {id: test.sync.name}},
  65. function () {
  66. console.log('Testing response...');
  67. var response = test.sync.result;
  68. jum.assertEquals(response.status, 404);
  69. }
  70. ];
  71.  
  72. this.teardown = function () { test.sync.cleanup(); };
  73. }();
  74.  
  75. /* Output of the test:
  76.  
  77. Number of tests run: 3
  78. Number of tests failures: 1
  79. Test failures:
  80. test_no_such_url.test_asserts: response is null
  81.  
  82.  
  83. Test: test_no_such_url.teardown
  84. Test Result:true
  85.  
  86. Action: waits.forElement
  87. Parameters: {"id":"test_no_such_url","orig":"js","test":"undefined"}
  88. Test Result: true
  89.  
  90. Action: function
  91. Parameters: "(JavaScript code is being executed)"
  92. Test Result: true
  93.  
  94. Log output on Firebug console:
  95.  
  96. Testing response...
  97. Request failed with status 0
  98. test_no_such_url.sync() called.
  99. Node test_no_such_url added.
  100. test_no_such_url.cleanup() called.
  101. Removed test_no_such_url node.
  102.  
  103.  
  104. This seems to imply that,
  105.  
  106. test_asserts is being run first. The waitForElements() return immediately and processing moves on to the function(), before the node has appeared.
  107.  
  108. */
Add Comment
Please, Sign In to add comment