From 7eafd95bfa21c9c8603e9c437a9d96dc880d92e7 Mon Sep 17 00:00:00 2001 From: wei <> Date: Sat, 30 Sep 2006 05:18:39 +0000 Subject: Update selenium to 0.8 --- .../core/scripts/selenium-executionloop.js | 298 ++++++++------------- 1 file changed, 109 insertions(+), 189 deletions(-) (limited to 'tests/test_tools/selenium/core/scripts/selenium-executionloop.js') diff --git a/tests/test_tools/selenium/core/scripts/selenium-executionloop.js b/tests/test_tools/selenium/core/scripts/selenium-executionloop.js index 14c1a07a..d59fc148 100644 --- a/tests/test_tools/selenium/core/scripts/selenium-executionloop.js +++ b/tests/test_tools/selenium/core/scripts/selenium-executionloop.js @@ -14,253 +14,173 @@ * limitations under the License. */ -SELENIUM_PROCESS_WAIT = "wait"; - function TestLoop(commandFactory) { - this.commandFactory = commandFactory; - this.waitForConditionTimeout = 30 * 1000; // 30 seconds +} + +TestLoop.prototype = { - this.start = function() { + start : function() { selenium.reset(); - LOG.debug("testLoop.start()"); + LOG.debug("currentTest.start()"); this.continueTest(); - }; + }, - /** - * Select the next command and continue the test. - */ - this.continueTest = function() { - LOG.debug("testLoop.continueTest() - acquire the next command"); + continueTest : function() { + /** + * Select the next command and continue the test. + */ + LOG.debug("currentTest.continueTest() - acquire the next command"); if (! this.aborted) { this.currentCommand = this.nextCommand(); } if (! this.requiresCallBack) { - this.beginNextTest(); - } // otherwise, just finish and let the callback invoke beginNextTest() - }; - - this.beginNextTest = function() { - LOG.debug("testLoop.beginNextTest()"); - if (this.currentCommand) { + this.continueTestAtCurrentCommand(); + } // otherwise, just finish and let the callback invoke continueTestAtCurrentCommand() + }, + + continueTestAtCurrentCommand : function() { + LOG.debug("currentTest.continueTestAtCurrentCommand()"); + if (this.currentCommand) { // TODO: rename commandStarted to commandSelected, OR roll it into nextCommand this.commandStarted(this.currentCommand); - this.resumeAfterDelay(); + this._resumeAfterDelay(); } else { - this.testComplete(); + this._testComplete(); } - } - - /** - * Pause, then execute the current command. - */ - this.resumeAfterDelay = function() { + }, + + _resumeAfterDelay : function() { + /** + * Pause, then execute the current command. + */ // Get the command delay. If a pauseInterval is set, use it once // and reset it. Otherwise, use the defined command-interval. var delay = this.pauseInterval || this.getCommandInterval(); this.pauseInterval = undefined; - if (delay < 0) { + if (this.currentCommand.isBreakpoint || delay < 0) { // Pause: enable the "next/continue" button this.pause(); } else { - window.setTimeout("testLoop.resume()", delay); + window.setTimeout(this.resume.bind(this), delay); } - }; + }, - /** - * Select the next command and continue the test. - */ - this.resume = function() { - LOG.debug("testLoop.resume() - actually execute"); + resume: function() { + /** + * Select the next command and continue the test. + */ + LOG.debug("currentTest.resume() - actually execute"); try { - this.executeCurrentCommand(); - this.waitForConditionStart = new Date().getTime(); + selenium.browserbot.runScheduledPollers(); + this._executeCurrentCommand(); this.continueTestWhenConditionIsTrue(); } catch (e) { - this.handleCommandError(e); - this.testComplete(); + this._handleCommandError(e); + this._testComplete(); return; } - }; - - /** - * Execute the current command. - * - * The return value, if not null, should be a function which will be - * used to determine when execution can continue. - */ - this.executeCurrentCommand = function() { - + }, + + _testComplete : function() { + selenium.ensureNoUnhandledPopups(); + this.testComplete(); + }, + + _executeCurrentCommand : function() { + /** + * Execute the current command. + * + * @return a function which will be used to determine when + * execution can continue, or null if we can continue immediately + */ var command = this.currentCommand; LOG.info("Executing: |" + command.command + " | " + command.target + " | " + command.value + " |"); - + var handler = this.commandFactory.getCommandHandler(command.command); if (handler == null) { throw new SeleniumError("Unknown command: '" + command.command + "'"); } - + command.target = selenium.preprocessParameter(command.target); command.value = selenium.preprocessParameter(command.value); LOG.debug("Command found, going to execute " + command.command); var result = handler.execute(selenium, command); - LOG.debug("Command complete"); + LOG.debug("Command complete"); this.commandComplete(result); - if (result.processState == SELENIUM_PROCESS_WAIT) { - this.waitForCondition = function() { - LOG.debug("Checking condition: isNewPageLoaded?"); - return selenium.browserbot.isNewPageLoaded(); - }; - } - }; - - this.handleCommandError = function(e) { - if (!e.isSeleniumError) { + this.waitForCondition = result.terminationCondition; + + }, + + _handleCommandError : function(e) { + if (!e.isSeleniumError) { LOG.exception(e); var msg = "Selenium failure. Please report to selenium-dev@openqa.org, with error details from the log window."; if (e.message) { - msg += " The error message is: " + e.message; + msg += " The error message is: " + e.message; } this.commandError(msg); } else { LOG.error(e.message); this.commandError(e.message); } - }; - - /** - * Busy wait for waitForCondition() to become true, and then carry on - * with test. Fail the current test if there's a timeout or an exception. - */ - this.continueTestWhenConditionIsTrue = function () { - LOG.debug("testLoop.continueTestWhenConditionIsTrue()"); + }, + + continueTestWhenConditionIsTrue: function () { + /** + * Busy wait for waitForCondition() to become true, and then carry + * on with test. Fail the current test if there's a timeout or an + * exception. + */ + LOG.debug("currentTest.continueTestWhenConditionIsTrue()"); + selenium.browserbot.runScheduledPollers(); try { if (this.waitForCondition == null || this.waitForCondition()) { - LOG.debug("condition satisfied; let's continueTest()"); - this.waitForCondition = null; - this.waitForConditionStart = null; - this.continueTest(); - } else { - LOG.debug("waitForCondition was false; keep waiting!"); - if (this.waitForConditionTimeout != null) { - var now = new Date(); - if ((now - this.waitForConditionStart) > this.waitForConditionTimeout) { - throw new SeleniumError("Timed out after " + this.waitForConditionTimeout + "ms"); - } - } - window.setTimeout("testLoop.continueTestWhenConditionIsTrue()", 10); - } - } catch (e) { - var lastResult = new CommandResult(); - lastResult.failed = true; - lastResult.failureMessage = e.message; - this.commandComplete(lastResult); - this.testComplete(); - } - }; - -} - -/** The default is not to have any interval between commands. */ -TestLoop.prototype.getCommandInterval = function() { - return 0; -}; - -TestLoop.prototype.nextCommand = noop; - -TestLoop.prototype.commandStarted = noop; - -TestLoop.prototype.commandError = noop; - -TestLoop.prototype.commandComplete = noop; - -TestLoop.prototype.testComplete = noop; - -TestLoop.prototype.pause = noop; - -function noop() { - -}; - -/** - * Tell Selenium to expect a failure on the next command execution. This - * command temporarily installs a CommandFactory that generates - * CommandHandlers that expect a failure. - */ -Selenium.prototype.assertFailureOnNext = function(message) { - if (!message) { - throw new Error("Message must be provided"); - } + LOG.debug("condition satisfied; let's continueTest()"); + this.waitForCondition = null; + this.continueTest(); + } else { + LOG.debug("waitForCondition was false; keep waiting!"); + window.setTimeout(this.continueTestWhenConditionIsTrue.bind(this), 100); + } + } catch (e) { + var lastResult = {}; + lastResult.failed = true; + lastResult.failureMessage = e.message; + this.commandComplete(lastResult); + this.testComplete(); + } + }, - var expectFailureCommandFactory = - new ExpectFailureCommandFactory(testLoop.commandFactory, message, "failure"); - expectFailureCommandFactory.baseExecutor = executeCommandAndReturnFailureMessage; - testLoop.commandFactory = expectFailureCommandFactory; -}; + pause : function() {}, + nextCommand : function() {}, + commandStarted : function() {}, + commandComplete : function() {}, + commandError : function() {}, + testComplete : function() {}, -/** - * Tell Selenium to expect an error on the next command execution. This - * command temporarily installs a CommandFactory that generates - * CommandHandlers that expect a failure. - */ -Selenium.prototype.assertErrorOnNext = function(message) { - if (!message) { - throw new Error("Message must be provided"); + getCommandInterval : function() { + return 0; } - var expectFailureCommandFactory = - new ExpectFailureCommandFactory(testLoop.commandFactory, message, "error"); - expectFailureCommandFactory.baseExecutor = executeCommandAndReturnErrorMessage; - testLoop.commandFactory = expectFailureCommandFactory; -}; - -function ExpectFailureCommandFactory(originalCommandFactory, expectedErrorMessage, errorType) { - this.getCommandHandler = function(name) { - var baseHandler = originalCommandFactory.getCommandHandler(name); - var baseExecutor = this.baseExecutor; - var expectFailureCommand = {}; - expectFailureCommand.execute = function() { - var baseFailureMessage = baseExecutor(baseHandler, arguments); - var result = new CommandResult(); - if (!baseFailureMessage) { - result.failed = true; - result.failureMessage = "Expected " + errorType + " did not occur."; - } - else { - if (! PatternMatcher.matches(expectedErrorMessage, baseFailureMessage)) { - result.failed = true; - result.failureMessage = "Expected " + errorType + " message '" + expectedErrorMessage - + "' but was '" + baseFailureMessage + "'"; - } - else { - result.passed = true; - result.result = baseFailureMessage; - } - } - testLoop.commandFactory = originalCommandFactory; - return result; - }; - return expectFailureCommand; - }; -}; - -function executeCommandAndReturnFailureMessage(baseHandler, originalArguments) { - var baseResult = baseHandler.execute.apply(baseHandler, originalArguments); - if (baseResult.passed) { - return null; - } - return baseResult.failureMessage; -}; +} -function executeCommandAndReturnErrorMessage(baseHandler, originalArguments) { - try { - baseHandler.execute.apply(baseHandler, originalArguments); +function decorateFunctionWithTimeout(f, timeout) { + if (f == null) { return null; } - catch (expected) { - return expected.message; + if (isNaN(timeout)) { + throw new SeleniumError("Timeout is not a number: " + timeout); } -}; - + var now = new Date().getTime(); + var timeoutTime = now + timeout; + return function() { + if (new Date().getTime() > timeoutTime) { + throw new SeleniumError("Timed out after " + timeout + "ms"); + } + return f(); + }; +} -- cgit v1.2.3