/testing/selenium-core/scripts/selenium-api.js
http://datanucleus-appengine.googlecode.com/ · JavaScript · 3043 lines · 1457 code · 245 blank · 1341 comment · 286 complexity · 8e962db41a4337f77b196d02a4f6462d MD5 · raw file
Large files are truncated click here to view the full file
- /*
- * Copyright 2004 ThoughtWorks, Inc
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
- // TODO: stop navigating this.browserbot.document() ... it breaks encapsulation
- var storedVars = new Object();
- function Selenium(browserbot) {
- /**
- * Defines an object that runs Selenium commands.
- *
- * <h3><a name="locators"></a>Element Locators</h3>
- * <p>
- * Element Locators tell Selenium which HTML element a command refers to.
- * The format of a locator is:</p>
- * <blockquote>
- * <em>locatorType</em><strong>=</strong><em>argument</em>
- * </blockquote>
- *
- * <p>
- * We support the following strategies for locating elements:
- * </p>
- *
- * <ul>
- * <li><strong>identifier</strong>=<em>id</em>:
- * Select the element with the specified @id attribute. If no match is
- * found, select the first element whose @name attribute is <em>id</em>.
- * (This is normally the default; see below.)</li>
- * <li><strong>id</strong>=<em>id</em>:
- * Select the element with the specified @id attribute.</li>
- *
- * <li><strong>name</strong>=<em>name</em>:
- * Select the first element with the specified @name attribute.
- * <ul class="first last simple">
- * <li>username</li>
- * <li>name=username</li>
- * </ul>
- *
- * <p>The name may optionally be followed by one or more <em>element-filters</em>, separated from the name by whitespace. If the <em>filterType</em> is not specified, <strong>value</strong> is assumed.</p>
- *
- * <ul class="first last simple">
- * <li>name=flavour value=chocolate</li>
- * </ul>
- * </li>
- * <li><strong>dom</strong>=<em>javascriptExpression</em>:
- *
- * Find an element by evaluating the specified string. This allows you to traverse the HTML Document Object
- * Model using JavaScript. Note that you must not return a value in this string; simply make it the last expression in the block.
- * <ul class="first last simple">
- * <li>dom=document.forms['myForm'].myDropdown</li>
- * <li>dom=document.images[56]</li>
- * <li>dom=function foo() { return document.links[1]; }; foo();</li>
- * </ul>
- *
- * </li>
- *
- * <li><strong>xpath</strong>=<em>xpathExpression</em>:
- * Locate an element using an XPath expression.
- * <ul class="first last simple">
- * <li>xpath=//img[@alt='The image alt text']</li>
- * <li>xpath=//table[@id='table1']//tr[4]/td[2]</li>
- * <li>xpath=//a[contains(@href,'#id1')]</li>
- * <li>xpath=//a[contains(@href,'#id1')]/@class</li>
- * <li>xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td</li>
- * <li>xpath=//input[@name='name2' and @value='yes']</li>
- * <li>xpath=//*[text()="right"]</li>
- *
- * </ul>
- * </li>
- * <li><strong>link</strong>=<em>textPattern</em>:
- * Select the link (anchor) element which contains text matching the
- * specified <em>pattern</em>.
- * <ul class="first last simple">
- * <li>link=The link text</li>
- * </ul>
- *
- * </li>
- *
- * <li><strong>css</strong>=<em>cssSelectorSyntax</em>:
- * Select the element using css selectors. Please refer to <a href="http://www.w3.org/TR/REC-CSS2/selector.html">CSS2 selectors</a>, <a href="http://www.w3.org/TR/2001/CR-css3-selectors-20011113/">CSS3 selectors</a> for more information. You can also check the TestCssLocators test in the selenium test suite for an example of usage, which is included in the downloaded selenium core package.
- * <ul class="first last simple">
- * <li>css=a[href="#id3"]</li>
- * <li>css=span#firstChild + span</li>
- * </ul>
- * <p>Currently the css selector locator supports all css1, css2 and css3 selectors except namespace in css3, some pseudo classes(:nth-of-type, :nth-last-of-type, :first-of-type, :last-of-type, :only-of-type, :visited, :hover, :active, :focus, :indeterminate) and pseudo elements(::first-line, ::first-letter, ::selection, ::before, ::after). </p>
- * </li>
- *
- * <li><strong>ui</strong>=<em>uiSpecifierString</em>:
- * Locate an element by resolving the UI specifier string to another locator, and evaluating it. See the <a href="http://svn.openqa.org/fisheye/browse/~raw,r=trunk/selenium/trunk/src/main/resources/core/scripts/ui-doc.html">Selenium UI-Element Reference</a> for more details.
- * <ul class="first last simple">
- * <li>ui=loginPages::loginButton()</li>
- * <li>ui=settingsPages::toggle(label=Hide Email)</li>
- * <li>ui=forumPages::postBody(index=2)//a[2]</li>
- * </ul>
- * </li>
- *
- * </ul>
- *
- * <p>
- * Without an explicit locator prefix, Selenium uses the following default
- * strategies:
- * </p>
- *
- * <ul class="simple">
- * <li><strong>dom</strong>, for locators starting with "document."</li>
- * <li><strong>xpath</strong>, for locators starting with "//"</li>
- * <li><strong>identifier</strong>, otherwise</li>
- * </ul>
- *
- * <h3><a name="element-filters">Element Filters</a></h3>
- * <blockquote>
- * <p>Element filters can be used with a locator to refine a list of candidate elements. They are currently used only in the 'name' element-locator.</p>
- * <p>Filters look much like locators, ie.</p>
- * <blockquote>
- * <em>filterType</em><strong>=</strong><em>argument</em></blockquote>
- *
- * <p>Supported element-filters are:</p>
- * <p><strong>value=</strong><em>valuePattern</em></p>
- * <blockquote>
- * Matches elements based on their values. This is particularly useful for refining a list of similarly-named toggle-buttons.</blockquote>
- * <p><strong>index=</strong><em>index</em></p>
- * <blockquote>
- * Selects a single element based on its position in the list (offset from zero).</blockquote>
- * </blockquote>
- *
- * <h3><a name="patterns"></a>String-match Patterns</h3>
- *
- * <p>
- * Various Pattern syntaxes are available for matching string values:
- * </p>
- * <ul>
- * <li><strong>glob:</strong><em>pattern</em>:
- * Match a string against a "glob" (aka "wildmat") pattern. "Glob" is a
- * kind of limited regular-expression syntax typically used in command-line
- * shells. In a glob pattern, "*" represents any sequence of characters, and "?"
- * represents any single character. Glob patterns match against the entire
- * string.</li>
- * <li><strong>regexp:</strong><em>regexp</em>:
- * Match a string using a regular-expression. The full power of JavaScript
- * regular-expressions is available.</li>
- * <li><strong>regexpi:</strong><em>regexpi</em>:
- * Match a string using a case-insensitive regular-expression.</li>
- * <li><strong>exact:</strong><em>string</em>:
- *
- * Match a string exactly, verbatim, without any of that fancy wildcard
- * stuff.</li>
- * </ul>
- * <p>
- * If no pattern prefix is specified, Selenium assumes that it's a "glob"
- * pattern.
- * </p>
- * <p>
- * For commands that return multiple values (such as verifySelectOptions),
- * the string being matched is a comma-separated list of the return values,
- * where both commas and backslashes in the values are backslash-escaped.
- * When providing a pattern, the optional matching syntax (i.e. glob,
- * regexp, etc.) is specified once, as usual, at the beginning of the
- * pattern.
- * </p>
- */
- this.browserbot = browserbot;
- this.optionLocatorFactory = new OptionLocatorFactory();
- // DGF for backwards compatibility
- this.page = function() {
- return browserbot;
- };
- this.defaultTimeout = Selenium.DEFAULT_TIMEOUT;
- this.mouseSpeed = 10;
- }
- Selenium.DEFAULT_TIMEOUT = 30 * 1000;
- Selenium.DEFAULT_MOUSE_SPEED = 10;
- Selenium.RIGHT_MOUSE_CLICK = 2;
- Selenium.decorateFunctionWithTimeout = function(f, timeout) {
- if (f == null) {
- return null;
- }
- var timeoutValue = parseInt(timeout);
- if (isNaN(timeoutValue)) {
- throw new SeleniumError("Timeout is not a number: '" + timeout + "'");
- }
- var now = new Date().getTime();
- var timeoutTime = now + timeoutValue;
- return function() {
- if (new Date().getTime() > timeoutTime) {
- throw new SeleniumError("Timed out after " + timeoutValue + "ms");
- }
- return f();
- };
- }
- Selenium.createForWindow = function(window, proxyInjectionMode) {
- if (!window.location) {
- throw "error: not a window!";
- }
- return new Selenium(BrowserBot.createForWindow(window, proxyInjectionMode));
- };
- Selenium.prototype.reset = function() {
- this.defaultTimeout = Selenium.DEFAULT_TIMEOUT;
- // todo: this.browserbot.reset()
- this.browserbot.selectWindow("null");
- this.browserbot.resetPopups();
- };
- Selenium.prototype.doClick = function(locator) {
- /**
- * Clicks on a link, button, checkbox or radio button. If the click action
- * causes a new page to load (like a link usually does), call
- * waitForPageToLoad.
- *
- * @param locator an element locator
- *
- */
- var element = this.browserbot.findElement(locator);
- this.browserbot.clickElement(element);
- };
- Selenium.prototype.doDoubleClick = function(locator) {
- /**
- * Double clicks on a link, button, checkbox or radio button. If the double click action
- * causes a new page to load (like a link usually does), call
- * waitForPageToLoad.
- *
- * @param locator an element locator
- *
- */
- var element = this.browserbot.findElement(locator);
- this.browserbot.doubleClickElement(element);
- };
- Selenium.prototype.doContextMenu = function(locator) {
- /**
- * Simulates opening the context menu for the specified element (as might happen if the user "right-clicked" on the element).
- *
- * @param locator an element locator
- *
- */
- var element = this.browserbot.findElement(locator);
- this.browserbot.contextMenuOnElement(element);
- };
- Selenium.prototype.doClickAt = function(locator, coordString) {
- /**
- * Clicks on a link, button, checkbox or radio button. If the click action
- * causes a new page to load (like a link usually does), call
- * waitForPageToLoad.
- *
- * @param locator an element locator
- * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
- * event relative to the element returned by the locator.
- *
- */
- var element = this.browserbot.findElement(locator);
- var clientXY = getClientXY(element, coordString)
- this.doMouseMove(locator);
- this.doMouseDown(locator);
- this.browserbot.clickElement(element, clientXY[0], clientXY[1]);
- this.doMouseUp(locator);
- };
- Selenium.prototype.doDoubleClickAt = function(locator, coordString) {
- /**
- * Doubleclicks on a link, button, checkbox or radio button. If the action
- * causes a new page to load (like a link usually does), call
- * waitForPageToLoad.
- *
- * @param locator an element locator
- * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
- * event relative to the element returned by the locator.
- *
- */
- var element = this.browserbot.findElement(locator);
- var clientXY = getClientXY(element, coordString)
- this.doMouseMove(locator);
- this.doMouseDown(locator);
- this.browserbot.doubleClickElement(element, clientXY[0], clientXY[1]);
- this.doMouseUp(locator);
- };
- Selenium.prototype.doContextMenuAt = function(locator, coordString) {
- /**
- * Simulates opening the context menu for the specified element (as might happen if the user "right-clicked" on the element).
- *
- * @param locator an element locator
- * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
- * event relative to the element returned by the locator.
- *
- */
- var element = this.browserbot.findElement(locator);
- var clientXY = getClientXY(element, coordString)
- this.browserbot.contextMenuOnElement(element, clientXY[0], clientXY[1]);
- };
- Selenium.prototype.doFireEvent = function(locator, eventName) {
- /**
- * Explicitly simulate an event, to trigger the corresponding "on<em>event</em>"
- * handler.
- *
- * @param locator an <a href="#locators">element locator</a>
- * @param eventName the event name, e.g. "focus" or "blur"
- */
- var element = this.browserbot.findElement(locator);
- triggerEvent(element, eventName, false);
- };
- Selenium.prototype.doFocus = function(locator) {
- /** Move the focus to the specified element; for example, if the element is an input field, move the cursor to that field.
- *
- * @param locator an <a href="#locators">element locator</a>
- */
- var element = this.browserbot.findElement(locator);
- if (element.focus) {
- element.focus();
- } else {
- triggerEvent(element, "focus", false);
- }
- }
- Selenium.prototype.doKeyPress = function(locator, keySequence) {
- /**
- * Simulates a user pressing and releasing a key.
- *
- * @param locator an <a href="#locators">element locator</a>
- * @param keySequence Either be a string("\" followed by the numeric keycode
- * of the key to be pressed, normally the ASCII value of that key), or a single
- * character. For example: "w", "\119".
- */
- var element = this.browserbot.findElement(locator);
- triggerKeyEvent(element, 'keypress', keySequence, true,
- this.browserbot.controlKeyDown,
- this.browserbot.altKeyDown,
- this.browserbot.shiftKeyDown,
- this.browserbot.metaKeyDown);
- };
- Selenium.prototype.doShiftKeyDown = function() {
- /**
- * Press the shift key and hold it down until doShiftUp() is called or a new page is loaded.
- *
- */
- this.browserbot.shiftKeyDown = true;
- };
- Selenium.prototype.doShiftKeyUp = function() {
- /**
- * Release the shift key.
- *
- */
- this.browserbot.shiftKeyDown = false;
- };
- Selenium.prototype.doMetaKeyDown = function() {
- /**
- * Press the meta key and hold it down until doMetaUp() is called or a new page is loaded.
- *
- */
- this.browserbot.metaKeyDown = true;
- };
- Selenium.prototype.doMetaKeyUp = function() {
- /**
- * Release the meta key.
- *
- */
- this.browserbot.metaKeyDown = false;
- };
- Selenium.prototype.doAltKeyDown = function() {
- /**
- * Press the alt key and hold it down until doAltUp() is called or a new page is loaded.
- *
- */
- this.browserbot.altKeyDown = true;
- };
- Selenium.prototype.doAltKeyUp = function() {
- /**
- * Release the alt key.
- *
- */
- this.browserbot.altKeyDown = false;
- };
- Selenium.prototype.doControlKeyDown = function() {
- /**
- * Press the control key and hold it down until doControlUp() is called or a new page is loaded.
- *
- */
- this.browserbot.controlKeyDown = true;
- };
- Selenium.prototype.doControlKeyUp = function() {
- /**
- * Release the control key.
- *
- */
- this.browserbot.controlKeyDown = false;
- };
- Selenium.prototype.doKeyDown = function(locator, keySequence) {
- /**
- * Simulates a user pressing a key (without releasing it yet).
- *
- * @param locator an <a href="#locators">element locator</a>
- * @param keySequence Either be a string("\" followed by the numeric keycode
- * of the key to be pressed, normally the ASCII value of that key), or a single
- * character. For example: "w", "\119".
- */
- var element = this.browserbot.findElement(locator);
- triggerKeyEvent(element, 'keydown', keySequence, true,
- this.browserbot.controlKeyDown,
- this.browserbot.altKeyDown,
- this.browserbot.shiftKeyDown,
- this.browserbot.metaKeyDown);
- };
- Selenium.prototype.doKeyUp = function(locator, keySequence) {
- /**
- * Simulates a user releasing a key.
- *
- * @param locator an <a href="#locators">element locator</a>
- * @param keySequence Either be a string("\" followed by the numeric keycode
- * of the key to be pressed, normally the ASCII value of that key), or a single
- * character. For example: "w", "\119".
- */
- var element = this.browserbot.findElement(locator);
- triggerKeyEvent(element, 'keyup', keySequence, true,
- this.browserbot.controlKeyDown,
- this.browserbot.altKeyDown,
- this.browserbot.shiftKeyDown,
- this.browserbot.metaKeyDown);
- };
- function getClientXY(element, coordString) {
- // Parse coordString
- var coords = null;
- var x;
- var y;
- if (coordString) {
- coords = coordString.split(/,/);
- x = Number(coords[0]);
- y = Number(coords[1]);
- }
- else {
- x = y = 0;
- }
- // Get position of element,
- // Return 2 item array with clientX and clientY
- return [Selenium.prototype.getElementPositionLeft(element) + x, Selenium.prototype.getElementPositionTop(element) + y];
- }
- Selenium.prototype.doMouseOver = function(locator) {
- /**
- * Simulates a user hovering a mouse over the specified element.
- *
- * @param locator an <a href="#locators">element locator</a>
- */
- var element = this.browserbot.findElement(locator);
- this.browserbot.triggerMouseEvent(element, 'mouseover', true);
- };
- Selenium.prototype.doMouseOut = function(locator) {
- /**
- * Simulates a user moving the mouse pointer away from the specified element.
- *
- * @param locator an <a href="#locators">element locator</a>
- */
- var element = this.browserbot.findElement(locator);
- this.browserbot.triggerMouseEvent(element, 'mouseout', true);
- };
- Selenium.prototype.doMouseDown = function(locator) {
- /**
- * Simulates a user pressing the left mouse button (without releasing it yet) on
- * the specified element.
- *
- * @param locator an <a href="#locators">element locator</a>
- */
- var element = this.browserbot.findElement(locator);
- this.browserbot.triggerMouseEvent(element, 'mousedown', true);
- };
- Selenium.prototype.doMouseDownRight = function(locator) {
- /**
- * Simulates a user pressing the right mouse button (without releasing it yet) on
- * the specified element.
- *
- * @param locator an <a href="#locators">element locator</a>
- */
- var element = this.browserbot.findElement(locator);
- this.browserbot.triggerMouseEvent(element, 'mousedown', true, undefined, undefined, Selenium.RIGHT_MOUSE_CLICK);
- };
- Selenium.prototype.doMouseDownAt = function(locator, coordString) {
- /**
- * Simulates a user pressing the left mouse button (without releasing it yet) at
- * the specified location.
- *
- * @param locator an <a href="#locators">element locator</a>
- * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
- * event relative to the element returned by the locator.
- */
- var element = this.browserbot.findElement(locator);
- var clientXY = getClientXY(element, coordString)
- this.browserbot.triggerMouseEvent(element, 'mousedown', true, clientXY[0], clientXY[1]);
- };
- Selenium.prototype.doMouseDownRightAt = function(locator, coordString) {
- /**
- * Simulates a user pressing the right mouse button (without releasing it yet) at
- * the specified location.
- *
- * @param locator an <a href="#locators">element locator</a>
- * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
- * event relative to the element returned by the locator.
- */
- var element = this.browserbot.findElement(locator);
- var clientXY = getClientXY(element, coordString)
- this.browserbot.triggerMouseEvent(element, 'mousedown', true, clientXY[0], clientXY[1], Selenium.RIGHT_MOUSE_CLICK);
- };
- Selenium.prototype.doMouseUp = function(locator) {
- /**
- * Simulates the event that occurs when the user releases the mouse button (i.e., stops
- * holding the button down) on the specified element.
- *
- * @param locator an <a href="#locators">element locator</a>
- */
- var element = this.browserbot.findElement(locator);
- this.browserbot.triggerMouseEvent(element, 'mouseup', true);
- };
- Selenium.prototype.doMouseUpRight = function(locator) {
- /**
- * Simulates the event that occurs when the user releases the right mouse button (i.e., stops
- * holding the button down) on the specified element.
- *
- * @param locator an <a href="#locators">element locator</a>
- */
- var element = this.browserbot.findElement(locator);
- this.browserbot.triggerMouseEvent(element, 'mouseup', true, undefined, undefined, Selenium.RIGHT_MOUSE_CLICK);
- };
- Selenium.prototype.doMouseUpAt = function(locator, coordString) {
- /**
- * Simulates the event that occurs when the user releases the mouse button (i.e., stops
- * holding the button down) at the specified location.
- *
- * @param locator an <a href="#locators">element locator</a>
- * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
- * event relative to the element returned by the locator.
- */
- var element = this.browserbot.findElement(locator);
- var clientXY = getClientXY(element, coordString)
- this.browserbot.triggerMouseEvent(element, 'mouseup', true, clientXY[0], clientXY[1]);
- };
- Selenium.prototype.doMouseUpRightAt = function(locator, coordString) {
- /**
- * Simulates the event that occurs when the user releases the right mouse button (i.e., stops
- * holding the button down) at the specified location.
- *
- * @param locator an <a href="#locators">element locator</a>
- * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
- * event relative to the element returned by the locator.
- */
- var element = this.browserbot.findElement(locator);
- var clientXY = getClientXY(element, coordString)
- this.browserbot.triggerMouseEvent(element, 'mouseup', true, clientXY[0], clientXY[1], Selenium.RIGHT_MOUSE_CLICK);
- };
- Selenium.prototype.doMouseMove = function(locator) {
- /**
- * Simulates a user pressing the mouse button (without releasing it yet) on
- * the specified element.
- *
- * @param locator an <a href="#locators">element locator</a>
- */
- var element = this.browserbot.findElement(locator);
- this.browserbot.triggerMouseEvent(element, 'mousemove', true);
- };
- Selenium.prototype.doMouseMoveAt = function(locator, coordString) {
- /**
- * Simulates a user pressing the mouse button (without releasing it yet) on
- * the specified element.
- *
- * @param locator an <a href="#locators">element locator</a>
- * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
- * event relative to the element returned by the locator.
- */
- var element = this.browserbot.findElement(locator);
- var clientXY = getClientXY(element, coordString)
- this.browserbot.triggerMouseEvent(element, 'mousemove', true, clientXY[0], clientXY[1]);
- };
- Selenium.prototype.doType = function(locator, value) {
- /**
- * Sets the value of an input field, as though you typed it in.
- *
- * <p>Can also be used to set the value of combo boxes, check boxes, etc. In these cases,
- * value should be the value of the option selected, not the visible text.</p>
- *
- * @param locator an <a href="#locators">element locator</a>
- * @param value the value to type
- */
- if (this.browserbot.controlKeyDown || this.browserbot.altKeyDown || this.browserbot.metaKeyDown) {
- throw new SeleniumError("type not supported immediately after call to controlKeyDown() or altKeyDown() or metaKeyDown()");
- }
- // TODO fail if it can't be typed into.
- var element = this.browserbot.findElement(locator);
- if (this.browserbot.shiftKeyDown) {
- value = new String(value).toUpperCase();
- }
- this.browserbot.replaceText(element, value);
- };
- Selenium.prototype.doTypeKeys = function(locator, value) {
- /**
- * Simulates keystroke events on the specified element, as though you typed the value key-by-key.
- *
- * <p>This is a convenience method for calling keyDown, keyUp, keyPress for every character in the specified string;
- * this is useful for dynamic UI widgets (like auto-completing combo boxes) that require explicit key events.</p>
- *
- * <p>Unlike the simple "type" command, which forces the specified value into the page directly, this command
- * may or may not have any visible effect, even in cases where typing keys would normally have a visible effect.
- * For example, if you use "typeKeys" on a form element, you may or may not see the results of what you typed in
- * the field.</p>
- * <p>In some cases, you may need to use the simple "type" command to set the value of the field and then the "typeKeys" command to
- * send the keystroke events corresponding to what you just typed.</p>
- *
- * @param locator an <a href="#locators">element locator</a>
- * @param value the value to type
- */
- var keys = new String(value).split("");
- for (var i = 0; i < keys.length; i++) {
- var c = keys[i];
- this.doKeyDown(locator, c);
- this.doKeyUp(locator, c);
- this.doKeyPress(locator, c);
- }
- };
- Selenium.prototype.doSetSpeed = function(value) {
- /**
- * Set execution speed (i.e., set the millisecond length of a delay which will follow each selenium operation). By default, there is no such delay, i.e.,
- * the delay is 0 milliseconds.
- *
- * @param value the number of milliseconds to pause after operation
- */
- throw new SeleniumError("this operation is only implemented in selenium-rc, and should never result in a request making it across the wire");
- };
- Selenium.prototype.getSpeed = function() {
- /**
- * Get execution speed (i.e., get the millisecond length of the delay following each selenium operation). By default, there is no such delay, i.e.,
- * the delay is 0 milliseconds.
- *
- * See also setSpeed.
- *
- * @return string the execution speed in milliseconds.
- */
- throw new SeleniumError("this operation is only implemented in selenium-rc, and should never result in a request making it across the wire");
- };
- Selenium.prototype.findToggleButton = function(locator) {
- var element = this.browserbot.findElement(locator);
- if (element.checked == null) {
- Assert.fail("Element " + locator + " is not a toggle-button.");
- }
- return element;
- }
- Selenium.prototype.doCheck = function(locator) {
- /**
- * Check a toggle-button (checkbox/radio)
- *
- * @param locator an <a href="#locators">element locator</a>
- */
- this.findToggleButton(locator).checked = true;
- };
- Selenium.prototype.doUncheck = function(locator) {
- /**
- * Uncheck a toggle-button (checkbox/radio)
- *
- * @param locator an <a href="#locators">element locator</a>
- */
- this.findToggleButton(locator).checked = false;
- };
- Selenium.prototype.doSelect = function(selectLocator, optionLocator) {
- /**
- * Select an option from a drop-down using an option locator.
- *
- * <p>
- * Option locators provide different ways of specifying options of an HTML
- * Select element (e.g. for selecting a specific option, or for asserting
- * that the selected option satisfies a specification). There are several
- * forms of Select Option Locator.
- * </p>
- * <ul>
- * <li><strong>label</strong>=<em>labelPattern</em>:
- * matches options based on their labels, i.e. the visible text. (This
- * is the default.)
- * <ul class="first last simple">
- * <li>label=regexp:^[Oo]ther</li>
- * </ul>
- * </li>
- * <li><strong>value</strong>=<em>valuePattern</em>:
- * matches options based on their values.
- * <ul class="first last simple">
- * <li>value=other</li>
- * </ul>
- *
- *
- * </li>
- * <li><strong>id</strong>=<em>id</em>:
- *
- * matches options based on their ids.
- * <ul class="first last simple">
- * <li>id=option1</li>
- * </ul>
- * </li>
- * <li><strong>index</strong>=<em>index</em>:
- * matches an option based on its index (offset from zero).
- * <ul class="first last simple">
- *
- * <li>index=2</li>
- * </ul>
- * </li>
- * </ul>
- * <p>
- * If no option locator prefix is provided, the default behaviour is to match on <strong>label</strong>.
- * </p>
- *
- *
- * @param selectLocator an <a href="#locators">element locator</a> identifying a drop-down menu
- * @param optionLocator an option locator (a label by default)
- */
- var element = this.browserbot.findElement(selectLocator);
- if (!("options" in element)) {
- throw new SeleniumError("Specified element is not a Select (has no options)");
- }
- var locator = this.optionLocatorFactory.fromLocatorString(optionLocator);
- var option = locator.findOption(element);
- this.browserbot.selectOption(element, option);
- };
- Selenium.prototype.doAddSelection = function(locator, optionLocator) {
- /**
- * Add a selection to the set of selected options in a multi-select element using an option locator.
- *
- * @see #doSelect for details of option locators
- *
- * @param locator an <a href="#locators">element locator</a> identifying a multi-select box
- * @param optionLocator an option locator (a label by default)
- */
- var element = this.browserbot.findElement(locator);
- if (!("options" in element)) {
- throw new SeleniumError("Specified element is not a Select (has no options)");
- }
- var locator = this.optionLocatorFactory.fromLocatorString(optionLocator);
- var option = locator.findOption(element);
- this.browserbot.addSelection(element, option);
- };
- Selenium.prototype.doRemoveSelection = function(locator, optionLocator) {
- /**
- * Remove a selection from the set of selected options in a multi-select element using an option locator.
- *
- * @see #doSelect for details of option locators
- *
- * @param locator an <a href="#locators">element locator</a> identifying a multi-select box
- * @param optionLocator an option locator (a label by default)
- */
- var element = this.browserbot.findElement(locator);
- if (!("options" in element)) {
- throw new SeleniumError("Specified element is not a Select (has no options)");
- }
- var locator = this.optionLocatorFactory.fromLocatorString(optionLocator);
- var option = locator.findOption(element);
- this.browserbot.removeSelection(element, option);
- };
- Selenium.prototype.doRemoveAllSelections = function(locator) {
- /**
- * Unselects all of the selected options in a multi-select element.
- *
- * @param locator an <a href="#locators">element locator</a> identifying a multi-select box
- */
- var element = this.browserbot.findElement(locator);
- if (!("options" in element)) {
- throw new SeleniumError("Specified element is not a Select (has no options)");
- }
- for (var i = 0; i < element.options.length; i++) {
- this.browserbot.removeSelection(element, element.options[i]);
- }
- }
- Selenium.prototype.doSubmit = function(formLocator) {
- /**
- * Submit the specified form. This is particularly useful for forms without
- * submit buttons, e.g. single-input "Search" forms.
- *
- * @param formLocator an <a href="#locators">element locator</a> for the form you want to submit
- */
- var form = this.browserbot.findElement(formLocator);
- return this.browserbot.submit(form);
- };
- Selenium.prototype.makePageLoadCondition = function(timeout) {
- if (timeout == null) {
- timeout = this.defaultTimeout;
- }
- // if the timeout is zero, we won't wait for the page to load before returning
- if (timeout == 0) {
- return;
- }
- return Selenium.decorateFunctionWithTimeout(fnBind(this._isNewPageLoaded, this), timeout);
- };
- Selenium.prototype.doOpen = function(url) {
- /**
- * Opens an URL in the test frame. This accepts both relative and absolute
- * URLs.
- *
- * The "open" command waits for the page to load before proceeding,
- * ie. the "AndWait" suffix is implicit.
- *
- * <em>Note</em>: The URL must be on the same domain as the runner HTML
- * due to security restrictions in the browser (Same Origin Policy). If you
- * need to open an URL on another domain, use the Selenium Server to start a
- * new browser session on that domain.
- *
- * @param url the URL to open; may be relative or absolute
- */
- this.browserbot.openLocation(url);
- if (window["proxyInjectionMode"] == null || !window["proxyInjectionMode"]) {
- return this.makePageLoadCondition();
- } // in PI mode, just return "OK"; the server will waitForLoad
- };
- Selenium.prototype.doOpenWindow = function(url, windowID) {
- /**
- * Opens a popup window (if a window with that ID isn't already open).
- * After opening the window, you'll need to select it using the selectWindow
- * command.
- *
- * <p>This command can also be a useful workaround for bug SEL-339. In some cases, Selenium will be unable to intercept a call to window.open (if the call occurs during or before the "onLoad" event, for example).
- * In those cases, you can force Selenium to notice the open window's name by using the Selenium openWindow command, using
- * an empty (blank) url, like this: openWindow("", "myFunnyWindow").</p>
- *
- * @param url the URL to open, which can be blank
- * @param windowID the JavaScript window ID of the window to select
- */
- this.browserbot.openWindow(url, windowID);
- };
- Selenium.prototype.doSelectWindow = function(windowID) {
- /**
- * Selects a popup window using a window locator; once a popup window has been selected, all
- * commands go to that window. To select the main window again, use null
- * as the target.
- *
- * <p>
- *
- * Window locators provide different ways of specifying the window object:
- * by title, by internal JavaScript "name," or by JavaScript variable.
- * </p>
- * <ul>
- * <li><strong>title</strong>=<em>My Special Window</em>:
- * Finds the window using the text that appears in the title bar. Be careful;
- * two windows can share the same title. If that happens, this locator will
- * just pick one.
- * </li>
- * <li><strong>name</strong>=<em>myWindow</em>:
- * Finds the window using its internal JavaScript "name" property. This is the second
- * parameter "windowName" passed to the JavaScript method window.open(url, windowName, windowFeatures, replaceFlag)
- * (which Selenium intercepts).
- * </li>
- * <li><strong>var</strong>=<em>variableName</em>:
- * Some pop-up windows are unnamed (anonymous), but are associated with a JavaScript variable name in the current
- * application window, e.g. "window.foo = window.open(url);". In those cases, you can open the window using
- * "var=foo".
- * </li>
- * </ul>
- * <p>
- * If no window locator prefix is provided, we'll try to guess what you mean like this:</p>
- * <p>1.) if windowID is null, (or the string "null") then it is assumed the user is referring to the original window instantiated by the browser).</p>
- * <p>2.) if the value of the "windowID" parameter is a JavaScript variable name in the current application window, then it is assumed
- * that this variable contains the return value from a call to the JavaScript window.open() method.</p>
- * <p>3.) Otherwise, selenium looks in a hash it maintains that maps string names to window "names".</p>
- * <p>4.) If <em>that</em> fails, we'll try looping over all of the known windows to try to find the appropriate "title".
- * Since "title" is not necessarily unique, this may have unexpected behavior.</p>
- *
- * <p>If you're having trouble figuring out the name of a window that you want to manipulate, look at the Selenium log messages
- * which identify the names of windows created via window.open (and therefore intercepted by Selenium). You will see messages
- * like the following for each window as it is opened:</p>
- *
- * <p><code>debug: window.open call intercepted; window ID (which you can use with selectWindow()) is "myNewWindow"</code></p>
- *
- * <p>In some cases, Selenium will be unable to intercept a call to window.open (if the call occurs during or before the "onLoad" event, for example).
- * (This is bug SEL-339.) In those cases, you can force Selenium to notice the open window's name by using the Selenium openWindow command, using
- * an empty (blank) url, like this: openWindow("", "myFunnyWindow").</p>
- *
- * @param windowID the JavaScript window ID of the window to select
- */
- this.browserbot.selectWindow(windowID);
- };
- Selenium.prototype.doSelectFrame = function(locator) {
- /**
- * Selects a frame within the current window. (You may invoke this command
- * multiple times to select nested frames.) To select the parent frame, use
- * "relative=parent" as a locator; to select the top frame, use "relative=top".
- * You can also select a frame by its 0-based index number; select the first frame with
- * "index=0", or the third frame with "index=2".
- *
- * <p>You may also use a DOM expression to identify the frame you want directly,
- * like this: <code>dom=frames["main"].frames["subframe"]</code></p>
- *
- * @param locator an <a href="#locators">element locator</a> identifying a frame or iframe
- */
- this.browserbot.selectFrame(locator);
- };
- Selenium.prototype.getWhetherThisFrameMatchFrameExpression = function(currentFrameString, target) {
- /**
- * Determine whether current/locator identify the frame containing this running code.
- *
- * <p>This is useful in proxy injection mode, where this code runs in every
- * browser frame and window, and sometimes the selenium server needs to identify
- * the "current" frame. In this case, when the test calls selectFrame, this
- * routine is called for each frame to figure out which one has been selected.
- * The selected frame will return true, while all others will return false.</p>
- *
- * @param currentFrameString starting frame
- * @param target new frame (which might be relative to the current one)
- * @return boolean true if the new frame is this code's window
- */
- return this.browserbot.doesThisFrameMatchFrameExpression(currentFrameString, target);
- };
- Selenium.prototype.getWhetherThisWindowMatchWindowExpression = function(currentWindowString, target) {
- /**
- * Determine whether currentWindowString plus target identify the window containing this running code.
- *
- * <p>This is useful in proxy injection mode, where this code runs in every
- * browser frame and window, and sometimes the selenium server needs to identify
- * the "current" window. In this case, when the test calls selectWindow, this
- * routine is called for each window to figure out which one has been selected.
- * The selected window will return true, while all others will return false.</p>
- *
- * @param currentWindowString starting window
- * @param target new window (which might be relative to the current one, e.g., "_parent")
- * @return boolean true if the new window is this code's window
- */
- if (window.opener!=null && window.opener[target]!=null && window.opener[target]==window) {
- return true;
- }
- return false;
- };
- Selenium.prototype.doWaitForPopUp = function(windowID, timeout) {
- /**
- * Waits for a popup window to appear and load up.
- *
- * @param windowID the JavaScript window "name" of the window that will appear (not the text of the title bar)
- * @param timeout a timeout in milliseconds, after which the action will return with an error
- */
- var popupLoadedPredicate = function () {
- var targetWindow = selenium.browserbot.getWindowByName(windowID, true);
- if (!targetWindow) return false;
- if (!targetWindow.location) return false;
- if ("about:blank" == targetWindow.location) return false;
- if (browserVersion.isKonqueror) {
- if ("/" == targetWindow.location.href) {
- // apparently Konqueror uses this as the temporary location, instead of about:blank
- return false;
- }
- }
- if (browserVersion.isSafari) {
- if(targetWindow.location.href == selenium.browserbot.buttonWindow.location.href) {
- // Apparently Safari uses this as the temporary location, instead of about:blank
- // what a world!
- LOG.debug("DGF what a world!");
- return false;
- }
- }
- if (!targetWindow.document) return false;
- if (!selenium.browserbot.getCurrentWindow().document.readyState) {
- // This is Firefox, with no readyState extension
- return true;
- }
- if ('complete' != targetWindow.document.readyState) return false;
- return true;
- };
- return Selenium.decorateFunctionWithTimeout(popupLoadedPredicate, timeout);
- }
- Selenium.prototype.doWaitForPopUp.dontCheckAlertsAndConfirms = true;
- Selenium.prototype.doChooseCancelOnNextConfirmation = function() {
- /**
- * <p>
- * By default, Selenium's overridden window.confirm() function will
- * return true, as if the user had manually clicked OK; after running
- * this command, the next call to confirm() will return false, as if
- * the user had clicked Cancel. Selenium will then resume using the
- * default behavior for future confirmations, automatically returning
- * true (OK) unless/until you explicitly call this command for each
- * confirmation.
- * </p>
- * <p>
- * Take note - every time a confirmation comes up, you must
- * consume it with a corresponding getConfirmation, or else
- * the next selenium operation will fail.
- * </p>
- */
- this.browserbot.cancelNextConfirmation(false);
- };
- Selenium.prototype.doChooseOkOnNextConfirmation = function() {
- /**
- * <p>
- * Undo the effect of calling chooseCancelOnNextConfirmation. Note
- * that Selenium's overridden window.confirm() function will normally automatically
- * return true, as if the user had manually clicked OK, so you shouldn't
- * need to use this command unless for some reason you need to change
- * your mind prior to the next confirmation. After any confirmation, Selenium will resume using the
- * default behavior for future confirmations, automatically returning
- * true (OK) unless/until you explicitly call chooseCancelOnNextConfirmation for each
- * confirmation.
- * </p>
- * <p>
- * Take note - every time a confirmation comes up, you must
- * consume it with a corresponding getConfirmation, or else
- * the next selenium operation will fail.
- * </p>
- *
- */
- this.browserbot.cancelNextConfirmation(true);
- };
- Selenium.prototype.doAnswerOnNextPrompt = function(answer) {
- /**
- * Instructs Selenium to return the specified answer string in response to
- * the next JavaScript prompt [window.prompt()].
- *
- *
- * @param answer the answer to give in response to the prompt pop-up
- */
- this.browserbot.setNextPromptResult(answer);
- };
- Selenium.prototype.doGoBack = function() {
- /**
- * Simulates the user clicking the "back" button on their browser.
- *
- */
- this.browserbot.goBack();
- };
- Selenium.prototype.doRefresh = function() {
- /**
- * Simulates the user clicking the "Refresh" button on their browser.
- *
- */
- this.browserbot.refresh();
- };
- Selenium.prototype.doClose = function() {
- /**
- * Simulates the user clicking the "close" button in the titlebar of a popup
- * window or tab.
- */
- this.browserbot.close();
- };
- Selenium.prototype.ensureNoUnhandledPopups = function() {
- if (this.browserbot.hasAlerts()) {
- throw new SeleniumError("There was an unexpected Alert! [" + this.browserbot.getNextAlert() + "]");
- }
- if ( this.browserbot.hasConfirmations() ) {
- throw new SeleniumError("There was an unexpected Confirmation! [" + this.browserbot.getNextConfirmation() + "]");
- }
- };
- Selenium.prototype.isAlertPresent = function() {
- /**
- * Has an alert occurred?
- *
- * <p>
- * This function never throws an exception
- * </p>
- * @return boolean true if there is an alert
- */
- return this.browserbot.hasAlerts();
- };
- Selenium.prototype.isPromptPresent = function() {
- /**
- * Has a prompt occurred?
- *
- * <p>
- * This function never throws an exception
- * </p>
- * @return boolean true if there is a pending prompt
- */
- return this.browserbot.hasPrompts();
- };
- Selenium.prototype.isConfirmationPresent = function() {
- /**
- * Has confirm() been called?
- *
- * <p>
- * This function never throws an exception
- * </p>
- * @return boolean true if there is a pending confirmation
- */
- return this.browserbot.hasConfirmations();
- };
- Selenium.prototype.getAlert = function() {
- /**
- * Retrieves the message of a JavaScript alert generated during the previous action, or fail if there were no alerts.
- *
- * <p>Getting an alert has the same effect as manually clicking OK. If an
- * alert is generated but you do not consume it with getAlert, the next Selenium action
- * will fail.</p>
- *
- * <p>Under Selenium, JavaScript alerts will NOT pop up a visible alert
- * dialog.</p>
- *
- * <p>Selenium does NOT support JavaScript alerts that are generated in a
- * page's onload() event handler. In this case a visible dialog WILL be
- * generated and Selenium will hang until someone manually clicks OK.</p>
- * @return string The message of the most recent JavaScript alert
- */
- if (!this.browserbot.hasAlerts()) {
- Assert.fail("There were no alerts");
- }
- return this.browserbot.getNextAlert();
- };
- Selenium.prototype.getAlert.dontCheckAlertsAndConfirms = true;
- Selenium.prototype.getConfirmation = function() {
- /**
- * Retrieves the message of a JavaScript confirmation dialog generated during
- * the previous action.
- *
- * <p>
- * By default, the confirm function will return true, having the same effect
- * as manually clicking OK. This can be changed by prior execution of the
- * chooseCancelOnNextConfirmation command.
- * </p>
- * <p>
- * If an confirmation is generated but you do not consume it with getConfirmation,
- * the next Selenium action will fail.
- * </p>
- *
- * <p>
- * NOTE: under Selenium, JavaScript confirmations will NOT pop up a visible
- * dialog.
- * </p>
- *
- * <p>
- * NOTE: Selenium does NOT support JavaScript confirmations that are
- * generated in a page's onload() event handler. In this case a visible
- * dialog WILL be generated and Selenium will hang until you manually click
- * OK.
- * </p>
- *
- * @return string the message of the most recent JavaScript confirmation dialog
- */
- if (!this.browserbot.hasConfirmations()) {
- Assert.fail("There were no confirmations");
- }
- return this.browserbot.getNextConfirmation();
- };
- Selenium.prototype.getConfirmation.dontCheckAlertsAndConfirms = true;
- Selenium.prototype.getPrompt = function() {
- /**
- * Retrieves the message of a JavaScript question prompt dialog generated during
- * the previous action.
- *
- * <p>Successful handling of the prompt requires prior execution of the
- * answerOnNextPrompt command. If a prompt is generated but you
- * do not get/verify it, the next Selenium action will fail.</p>
- *
- * <p>NOTE: under Selenium, JavaScript prompts will NOT pop up a visible
- * dialog.</p>
- *
- * <p>NOTE: Selenium does NOT support JavaScript prompts that are generated in a
- * page's onload() event handler. In this case a visible dialog WILL be
- * generated and Selenium will hang until someone manually clicks OK.</p>
- * @return string the message of the most recent JavaScript question prompt
- */
- if (! this.browserbot.hasPrompts()) {
- Assert.fail("There were no prompts");
- }
- return this.browserbot.getNextPrompt();
- };
- Selenium.prototype.getLocation = function() {
- /** Gets the absolute URL of the current page.
- *
- * @return string the absolute URL of the current page
- */
- return this.browserbot.getCurrentWindow().location.href;
- };
- Selenium.prototype.getTitle = function() {
- /** Gets the title of the current page.
- *
- * @return string the title of the current page
- */
- return this.browserbot.getTitle();
- };
- Selenium.prototype.getBodyText = function() {
- /**
- * Gets the entire text of the page.
- * @return string the entire text of the page
- */
- return this.browserbot.bodyText();
- };
- Selenium.prototype.getValue = function(locator) {
- /**
- * Gets the (whitespace-trimmed) value of an input field (or anything else with a value parameter).
- * For checkbox/radio elements, the value will be "on" or "off" depending on
- * whether the element is checked or not.
- *
- * @param locator an <a href="#locators">element locator</a>
- * @return string the element value, or "on/off" for checkbox/radio elements
- */
- var element = this.browserbot.findElement(locator)
- return getInputValue(element).trim();
- }
- Selenium.prototype.getText = function(locator) {
- /**
- * Gets the text of an element. This works for any element that contains
- * text. This command uses either the textContent (Mozilla-like browsers) or
- * the innerText (IE-like browsers) of the element, which is the rendered
- * text shown to the user.
- *
- * @param locator an <a href="#locators">element locator</a>
- * @return string the text of the element
- */
- var element = this.browserbot.findElement(locator);
- return getText(element).trim();
- };
- Selenium.prototype.doHighlight = function(locator) {
- /**
- * Briefly changes the backgroundColor of the specified element yellow. Useful for debugging.
- *
- * @param locator an <a href="#locators">element locator</a>…