/testing/selenium-core/scripts/selenium-executionloop.js

http://datanucleus-appengine.googlecode.com/ · JavaScript · 175 lines · 113 code · 21 blank · 41 comment · 19 complexity · ee14042b666ae5dfef57e39c4b1d9417 MD5 · raw file

  1. /*
  2. * Copyright 2004 ThoughtWorks, Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. function TestLoop(commandFactory) {
  17. this.commandFactory = commandFactory;
  18. }
  19. TestLoop.prototype = {
  20. start : function() {
  21. selenium.reset();
  22. LOG.debug("currentTest.start()");
  23. this.continueTest();
  24. },
  25. continueTest : function() {
  26. /**
  27. * Select the next command and continue the test.
  28. */
  29. LOG.debug("currentTest.continueTest() - acquire the next command");
  30. if (! this.aborted) {
  31. this.currentCommand = this.nextCommand();
  32. }
  33. if (! this.requiresCallBack) {
  34. this.continueTestAtCurrentCommand();
  35. } // otherwise, just finish and let the callback invoke continueTestAtCurrentCommand()
  36. },
  37. continueTestAtCurrentCommand : function() {
  38. LOG.debug("currentTest.continueTestAtCurrentCommand()");
  39. if (this.currentCommand) {
  40. // TODO: rename commandStarted to commandSelected, OR roll it into nextCommand
  41. this.commandStarted(this.currentCommand);
  42. this._resumeAfterDelay();
  43. } else {
  44. this._testComplete();
  45. }
  46. },
  47. _resumeAfterDelay : function() {
  48. /**
  49. * Pause, then execute the current command.
  50. */
  51. // Get the command delay. If a pauseInterval is set, use it once
  52. // and reset it. Otherwise, use the defined command-interval.
  53. var delay = this.pauseInterval || this.getCommandInterval();
  54. this.pauseInterval = undefined;
  55. if (this.currentCommand.isBreakpoint || delay < 0) {
  56. // Pause: enable the "next/continue" button
  57. this.pause();
  58. } else {
  59. window.setTimeout(fnBind(this.resume, this), delay);
  60. }
  61. },
  62. resume: function() {
  63. /**
  64. * Select the next command and continue the test.
  65. */
  66. LOG.debug("currentTest.resume() - actually execute");
  67. try {
  68. selenium.browserbot.runScheduledPollers();
  69. this._executeCurrentCommand();
  70. this.continueTestWhenConditionIsTrue();
  71. } catch (e) {
  72. if (!this._handleCommandError(e)) {
  73. this.testComplete();
  74. } else {
  75. this.continueTest();
  76. }
  77. }
  78. },
  79. _testComplete : function() {
  80. selenium.ensureNoUnhandledPopups();
  81. this.testComplete();
  82. },
  83. _executeCurrentCommand : function() {
  84. /**
  85. * Execute the current command.
  86. *
  87. * @return a function which will be used to determine when
  88. * execution can continue, or null if we can continue immediately
  89. */
  90. var command = this.currentCommand;
  91. LOG.info("Executing: |" + command.command + " | " + command.target + " | " + command.value + " |");
  92. var handler = this.commandFactory.getCommandHandler(command.command);
  93. if (handler == null) {
  94. throw new SeleniumError("Unknown command: '" + command.command + "'");
  95. }
  96. command.target = selenium.preprocessParameter(command.target);
  97. command.value = selenium.preprocessParameter(command.value);
  98. LOG.debug("Command found, going to execute " + command.command);
  99. this.result = handler.execute(selenium, command);
  100. this.waitForCondition = this.result.terminationCondition;
  101. },
  102. _handleCommandError : function(e) {
  103. if (!e.isSeleniumError) {
  104. LOG.exception(e);
  105. var msg = "Selenium failure. Please report to the Selenium Users forum at http://forums.openqa.org, with error details from the log window.";
  106. msg += " The error message is: " + extractExceptionMessage(e);
  107. return this.commandError(msg);
  108. } else {
  109. LOG.error(e.message);
  110. return this.commandError(e.message);
  111. }
  112. },
  113. continueTestWhenConditionIsTrue: function () {
  114. /**
  115. * Busy wait for waitForCondition() to become true, and then carry
  116. * on with test. Fail the current test if there's a timeout or an
  117. * exception.
  118. */
  119. //LOG.debug("currentTest.continueTestWhenConditionIsTrue()");
  120. selenium.browserbot.runScheduledPollers();
  121. try {
  122. if (this.waitForCondition == null) {
  123. LOG.debug("null condition; let's continueTest()");
  124. LOG.debug("Command complete");
  125. this.commandComplete(this.result);
  126. this.continueTest();
  127. } else if (this.waitForCondition()) {
  128. LOG.debug("condition satisfied; let's continueTest()");
  129. this.waitForCondition = null;
  130. LOG.debug("Command complete");
  131. this.commandComplete(this.result);
  132. this.continueTest();
  133. } else {
  134. //LOG.debug("waitForCondition was false; keep waiting!");
  135. window.setTimeout(fnBind(this.continueTestWhenConditionIsTrue, this), 10);
  136. }
  137. } catch (e) {
  138. this.result = {};
  139. this.result.failed = true;
  140. this.result.failureMessage = extractExceptionMessage(e);
  141. this.commandComplete(this.result);
  142. this.continueTest();
  143. }
  144. },
  145. pause : function() {},
  146. nextCommand : function() {},
  147. commandStarted : function() {},
  148. commandComplete : function() {},
  149. commandError : function() {},
  150. testComplete : function() {},
  151. getCommandInterval : function() {
  152. return 0;
  153. }
  154. }