PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17function TestLoop(commandFactory) {
 18    this.commandFactory = commandFactory;
 19}
 20
 21TestLoop.prototype = {
 22
 23    start : function() {
 24        selenium.reset();
 25        LOG.debug("currentTest.start()");
 26        this.continueTest();
 27    },
 28
 29    continueTest : function() {
 30        /**
 31         * Select the next command and continue the test.
 32         */
 33        LOG.debug("currentTest.continueTest() - acquire the next command");
 34        if (! this.aborted) {
 35            this.currentCommand = this.nextCommand();
 36        }
 37        if (! this.requiresCallBack) {
 38            this.continueTestAtCurrentCommand();
 39        } // otherwise, just finish and let the callback invoke continueTestAtCurrentCommand()
 40    },
 41
 42    continueTestAtCurrentCommand : function() {
 43        LOG.debug("currentTest.continueTestAtCurrentCommand()");
 44        if (this.currentCommand) {
 45            // TODO: rename commandStarted to commandSelected, OR roll it into nextCommand
 46            this.commandStarted(this.currentCommand);
 47            this._resumeAfterDelay();
 48        } else {
 49            this._testComplete();
 50        }
 51    },
 52
 53    _resumeAfterDelay : function() {
 54        /**
 55         * Pause, then execute the current command.
 56         */
 57
 58        // Get the command delay. If a pauseInterval is set, use it once
 59        // and reset it.  Otherwise, use the defined command-interval.
 60        var delay = this.pauseInterval || this.getCommandInterval();
 61        this.pauseInterval = undefined;
 62
 63        if (this.currentCommand.isBreakpoint || delay < 0) {
 64            // Pause: enable the "next/continue" button
 65            this.pause();
 66        } else {
 67            window.setTimeout(fnBind(this.resume, this), delay);
 68        }
 69    },
 70
 71    resume: function() {
 72        /**
 73         * Select the next command and continue the test.
 74         */
 75        LOG.debug("currentTest.resume() - actually execute");
 76        try {
 77            selenium.browserbot.runScheduledPollers();
 78            this._executeCurrentCommand();
 79            this.continueTestWhenConditionIsTrue();
 80        } catch (e) {
 81            if (!this._handleCommandError(e)) {
 82                this.testComplete();
 83            } else {
 84                this.continueTest();
 85            }
 86        }
 87    },
 88
 89    _testComplete : function() {
 90        selenium.ensureNoUnhandledPopups();
 91        this.testComplete();
 92    },
 93
 94    _executeCurrentCommand : function() {
 95        /**
 96         * Execute the current command.
 97         *
 98         * @return a function which will be used to determine when
 99         * execution can continue, or null if we can continue immediately
100         */
101        var command = this.currentCommand;
102        LOG.info("Executing: |" + command.command + " | " + command.target + " | " + command.value + " |");
103
104        var handler = this.commandFactory.getCommandHandler(command.command);
105        if (handler == null) {
106            throw new SeleniumError("Unknown command: '" + command.command + "'");
107        }
108
109        command.target = selenium.preprocessParameter(command.target);
110        command.value = selenium.preprocessParameter(command.value);
111        LOG.debug("Command found, going to execute " + command.command);
112        this.result = handler.execute(selenium, command);
113        
114
115        this.waitForCondition = this.result.terminationCondition;
116
117    },
118
119    _handleCommandError : function(e) {
120        if (!e.isSeleniumError) {
121            LOG.exception(e);
122            var msg = "Selenium failure. Please report to the Selenium Users forum at http://forums.openqa.org, with error details from the log window.";
123            msg += "  The error message is: " + extractExceptionMessage(e);
124            return this.commandError(msg);
125        } else {
126            LOG.error(e.message);
127            return this.commandError(e.message);
128        }
129    },
130
131    continueTestWhenConditionIsTrue: function () {
132        /**
133         * Busy wait for waitForCondition() to become true, and then carry
134         * on with test.  Fail the current test if there's a timeout or an
135         * exception.
136         */
137        //LOG.debug("currentTest.continueTestWhenConditionIsTrue()");
138        selenium.browserbot.runScheduledPollers();
139        try {
140            if (this.waitForCondition == null) {
141                LOG.debug("null condition; let's continueTest()");
142                LOG.debug("Command complete");
143                this.commandComplete(this.result);
144                this.continueTest();
145            } else if (this.waitForCondition()) {
146                LOG.debug("condition satisfied; let's continueTest()");
147                this.waitForCondition = null;
148                LOG.debug("Command complete");
149                this.commandComplete(this.result);
150                this.continueTest();
151            } else {
152                //LOG.debug("waitForCondition was false; keep waiting!");
153                window.setTimeout(fnBind(this.continueTestWhenConditionIsTrue, this), 10);
154            }
155        } catch (e) {
156            this.result = {};
157            this.result.failed = true;
158            this.result.failureMessage = extractExceptionMessage(e);
159            this.commandComplete(this.result);
160            this.continueTest();
161        }
162    },
163
164    pause : function() {},
165    nextCommand : function() {},
166    commandStarted : function() {},
167    commandComplete : function() {},
168    commandError : function() {},
169    testComplete : function() {},
170
171    getCommandInterval : function() {
172        return 0;
173    }
174
175}