/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

  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. */
  17. // TODO: stop navigating this.browserbot.document() ... it breaks encapsulation
  18. var storedVars = new Object();
  19. function Selenium(browserbot) {
  20. /**
  21. * Defines an object that runs Selenium commands.
  22. *
  23. * <h3><a name="locators"></a>Element Locators</h3>
  24. * <p>
  25. * Element Locators tell Selenium which HTML element a command refers to.
  26. * The format of a locator is:</p>
  27. * <blockquote>
  28. * <em>locatorType</em><strong>=</strong><em>argument</em>
  29. * </blockquote>
  30. *
  31. * <p>
  32. * We support the following strategies for locating elements:
  33. * </p>
  34. *
  35. * <ul>
  36. * <li><strong>identifier</strong>=<em>id</em>:
  37. * Select the element with the specified &#064;id attribute. If no match is
  38. * found, select the first element whose &#064;name attribute is <em>id</em>.
  39. * (This is normally the default; see below.)</li>
  40. * <li><strong>id</strong>=<em>id</em>:
  41. * Select the element with the specified &#064;id attribute.</li>
  42. *
  43. * <li><strong>name</strong>=<em>name</em>:
  44. * Select the first element with the specified &#064;name attribute.
  45. * <ul class="first last simple">
  46. * <li>username</li>
  47. * <li>name=username</li>
  48. * </ul>
  49. *
  50. * <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>
  51. *
  52. * <ul class="first last simple">
  53. * <li>name=flavour value=chocolate</li>
  54. * </ul>
  55. * </li>
  56. * <li><strong>dom</strong>=<em>javascriptExpression</em>:
  57. *
  58. * Find an element by evaluating the specified string. This allows you to traverse the HTML Document Object
  59. * Model using JavaScript. Note that you must not return a value in this string; simply make it the last expression in the block.
  60. * <ul class="first last simple">
  61. * <li>dom=document.forms['myForm'].myDropdown</li>
  62. * <li>dom=document.images[56]</li>
  63. * <li>dom=function foo() { return document.links[1]; }; foo();</li>
  64. * </ul>
  65. *
  66. * </li>
  67. *
  68. * <li><strong>xpath</strong>=<em>xpathExpression</em>:
  69. * Locate an element using an XPath expression.
  70. * <ul class="first last simple">
  71. * <li>xpath=//img[&#064;alt='The image alt text']</li>
  72. * <li>xpath=//table[&#064;id='table1']//tr[4]/td[2]</li>
  73. * <li>xpath=//a[contains(&#064;href,'#id1')]</li>
  74. * <li>xpath=//a[contains(&#064;href,'#id1')]/&#064;class</li>
  75. * <li>xpath=(//table[&#064;class='stylee'])//th[text()='theHeaderText']/../td</li>
  76. * <li>xpath=//input[&#064;name='name2' and &#064;value='yes']</li>
  77. * <li>xpath=//*[text()="right"]</li>
  78. *
  79. * </ul>
  80. * </li>
  81. * <li><strong>link</strong>=<em>textPattern</em>:
  82. * Select the link (anchor) element which contains text matching the
  83. * specified <em>pattern</em>.
  84. * <ul class="first last simple">
  85. * <li>link=The link text</li>
  86. * </ul>
  87. *
  88. * </li>
  89. *
  90. * <li><strong>css</strong>=<em>cssSelectorSyntax</em>:
  91. * 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.
  92. * <ul class="first last simple">
  93. * <li>css=a[href="#id3"]</li>
  94. * <li>css=span#firstChild + span</li>
  95. * </ul>
  96. * <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>
  97. * </li>
  98. *
  99. * <li><strong>ui</strong>=<em>uiSpecifierString</em>:
  100. * 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.
  101. * <ul class="first last simple">
  102. * <li>ui=loginPages::loginButton()</li>
  103. * <li>ui=settingsPages::toggle(label=Hide Email)</li>
  104. * <li>ui=forumPages::postBody(index=2)//a[2]</li>
  105. * </ul>
  106. * </li>
  107. *
  108. * </ul>
  109. *
  110. * <p>
  111. * Without an explicit locator prefix, Selenium uses the following default
  112. * strategies:
  113. * </p>
  114. *
  115. * <ul class="simple">
  116. * <li><strong>dom</strong>, for locators starting with &quot;document.&quot;</li>
  117. * <li><strong>xpath</strong>, for locators starting with &quot;//&quot;</li>
  118. * <li><strong>identifier</strong>, otherwise</li>
  119. * </ul>
  120. *
  121. * <h3><a name="element-filters">Element Filters</a></h3>
  122. * <blockquote>
  123. * <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>
  124. * <p>Filters look much like locators, ie.</p>
  125. * <blockquote>
  126. * <em>filterType</em><strong>=</strong><em>argument</em></blockquote>
  127. *
  128. * <p>Supported element-filters are:</p>
  129. * <p><strong>value=</strong><em>valuePattern</em></p>
  130. * <blockquote>
  131. * Matches elements based on their values. This is particularly useful for refining a list of similarly-named toggle-buttons.</blockquote>
  132. * <p><strong>index=</strong><em>index</em></p>
  133. * <blockquote>
  134. * Selects a single element based on its position in the list (offset from zero).</blockquote>
  135. * </blockquote>
  136. *
  137. * <h3><a name="patterns"></a>String-match Patterns</h3>
  138. *
  139. * <p>
  140. * Various Pattern syntaxes are available for matching string values:
  141. * </p>
  142. * <ul>
  143. * <li><strong>glob:</strong><em>pattern</em>:
  144. * Match a string against a "glob" (aka "wildmat") pattern. "Glob" is a
  145. * kind of limited regular-expression syntax typically used in command-line
  146. * shells. In a glob pattern, "*" represents any sequence of characters, and "?"
  147. * represents any single character. Glob patterns match against the entire
  148. * string.</li>
  149. * <li><strong>regexp:</strong><em>regexp</em>:
  150. * Match a string using a regular-expression. The full power of JavaScript
  151. * regular-expressions is available.</li>
  152. * <li><strong>regexpi:</strong><em>regexpi</em>:
  153. * Match a string using a case-insensitive regular-expression.</li>
  154. * <li><strong>exact:</strong><em>string</em>:
  155. *
  156. * Match a string exactly, verbatim, without any of that fancy wildcard
  157. * stuff.</li>
  158. * </ul>
  159. * <p>
  160. * If no pattern prefix is specified, Selenium assumes that it's a "glob"
  161. * pattern.
  162. * </p>
  163. * <p>
  164. * For commands that return multiple values (such as verifySelectOptions),
  165. * the string being matched is a comma-separated list of the return values,
  166. * where both commas and backslashes in the values are backslash-escaped.
  167. * When providing a pattern, the optional matching syntax (i.e. glob,
  168. * regexp, etc.) is specified once, as usual, at the beginning of the
  169. * pattern.
  170. * </p>
  171. */
  172. this.browserbot = browserbot;
  173. this.optionLocatorFactory = new OptionLocatorFactory();
  174. // DGF for backwards compatibility
  175. this.page = function() {
  176. return browserbot;
  177. };
  178. this.defaultTimeout = Selenium.DEFAULT_TIMEOUT;
  179. this.mouseSpeed = 10;
  180. }
  181. Selenium.DEFAULT_TIMEOUT = 30 * 1000;
  182. Selenium.DEFAULT_MOUSE_SPEED = 10;
  183. Selenium.RIGHT_MOUSE_CLICK = 2;
  184. Selenium.decorateFunctionWithTimeout = function(f, timeout) {
  185. if (f == null) {
  186. return null;
  187. }
  188. var timeoutValue = parseInt(timeout);
  189. if (isNaN(timeoutValue)) {
  190. throw new SeleniumError("Timeout is not a number: '" + timeout + "'");
  191. }
  192. var now = new Date().getTime();
  193. var timeoutTime = now + timeoutValue;
  194. return function() {
  195. if (new Date().getTime() > timeoutTime) {
  196. throw new SeleniumError("Timed out after " + timeoutValue + "ms");
  197. }
  198. return f();
  199. };
  200. }
  201. Selenium.createForWindow = function(window, proxyInjectionMode) {
  202. if (!window.location) {
  203. throw "error: not a window!";
  204. }
  205. return new Selenium(BrowserBot.createForWindow(window, proxyInjectionMode));
  206. };
  207. Selenium.prototype.reset = function() {
  208. this.defaultTimeout = Selenium.DEFAULT_TIMEOUT;
  209. // todo: this.browserbot.reset()
  210. this.browserbot.selectWindow("null");
  211. this.browserbot.resetPopups();
  212. };
  213. Selenium.prototype.doClick = function(locator) {
  214. /**
  215. * Clicks on a link, button, checkbox or radio button. If the click action
  216. * causes a new page to load (like a link usually does), call
  217. * waitForPageToLoad.
  218. *
  219. * @param locator an element locator
  220. *
  221. */
  222. var element = this.browserbot.findElement(locator);
  223. this.browserbot.clickElement(element);
  224. };
  225. Selenium.prototype.doDoubleClick = function(locator) {
  226. /**
  227. * Double clicks on a link, button, checkbox or radio button. If the double click action
  228. * causes a new page to load (like a link usually does), call
  229. * waitForPageToLoad.
  230. *
  231. * @param locator an element locator
  232. *
  233. */
  234. var element = this.browserbot.findElement(locator);
  235. this.browserbot.doubleClickElement(element);
  236. };
  237. Selenium.prototype.doContextMenu = function(locator) {
  238. /**
  239. * Simulates opening the context menu for the specified element (as might happen if the user "right-clicked" on the element).
  240. *
  241. * @param locator an element locator
  242. *
  243. */
  244. var element = this.browserbot.findElement(locator);
  245. this.browserbot.contextMenuOnElement(element);
  246. };
  247. Selenium.prototype.doClickAt = function(locator, coordString) {
  248. /**
  249. * Clicks on a link, button, checkbox or radio button. If the click action
  250. * causes a new page to load (like a link usually does), call
  251. * waitForPageToLoad.
  252. *
  253. * @param locator an element locator
  254. * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
  255. * event relative to the element returned by the locator.
  256. *
  257. */
  258. var element = this.browserbot.findElement(locator);
  259. var clientXY = getClientXY(element, coordString)
  260. this.doMouseMove(locator);
  261. this.doMouseDown(locator);
  262. this.browserbot.clickElement(element, clientXY[0], clientXY[1]);
  263. this.doMouseUp(locator);
  264. };
  265. Selenium.prototype.doDoubleClickAt = function(locator, coordString) {
  266. /**
  267. * Doubleclicks on a link, button, checkbox or radio button. If the action
  268. * causes a new page to load (like a link usually does), call
  269. * waitForPageToLoad.
  270. *
  271. * @param locator an element locator
  272. * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
  273. * event relative to the element returned by the locator.
  274. *
  275. */
  276. var element = this.browserbot.findElement(locator);
  277. var clientXY = getClientXY(element, coordString)
  278. this.doMouseMove(locator);
  279. this.doMouseDown(locator);
  280. this.browserbot.doubleClickElement(element, clientXY[0], clientXY[1]);
  281. this.doMouseUp(locator);
  282. };
  283. Selenium.prototype.doContextMenuAt = function(locator, coordString) {
  284. /**
  285. * Simulates opening the context menu for the specified element (as might happen if the user "right-clicked" on the element).
  286. *
  287. * @param locator an element locator
  288. * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
  289. * event relative to the element returned by the locator.
  290. *
  291. */
  292. var element = this.browserbot.findElement(locator);
  293. var clientXY = getClientXY(element, coordString)
  294. this.browserbot.contextMenuOnElement(element, clientXY[0], clientXY[1]);
  295. };
  296. Selenium.prototype.doFireEvent = function(locator, eventName) {
  297. /**
  298. * Explicitly simulate an event, to trigger the corresponding &quot;on<em>event</em>&quot;
  299. * handler.
  300. *
  301. * @param locator an <a href="#locators">element locator</a>
  302. * @param eventName the event name, e.g. "focus" or "blur"
  303. */
  304. var element = this.browserbot.findElement(locator);
  305. triggerEvent(element, eventName, false);
  306. };
  307. Selenium.prototype.doFocus = function(locator) {
  308. /** Move the focus to the specified element; for example, if the element is an input field, move the cursor to that field.
  309. *
  310. * @param locator an <a href="#locators">element locator</a>
  311. */
  312. var element = this.browserbot.findElement(locator);
  313. if (element.focus) {
  314. element.focus();
  315. } else {
  316. triggerEvent(element, "focus", false);
  317. }
  318. }
  319. Selenium.prototype.doKeyPress = function(locator, keySequence) {
  320. /**
  321. * Simulates a user pressing and releasing a key.
  322. *
  323. * @param locator an <a href="#locators">element locator</a>
  324. * @param keySequence Either be a string("\" followed by the numeric keycode
  325. * of the key to be pressed, normally the ASCII value of that key), or a single
  326. * character. For example: "w", "\119".
  327. */
  328. var element = this.browserbot.findElement(locator);
  329. triggerKeyEvent(element, 'keypress', keySequence, true,
  330. this.browserbot.controlKeyDown,
  331. this.browserbot.altKeyDown,
  332. this.browserbot.shiftKeyDown,
  333. this.browserbot.metaKeyDown);
  334. };
  335. Selenium.prototype.doShiftKeyDown = function() {
  336. /**
  337. * Press the shift key and hold it down until doShiftUp() is called or a new page is loaded.
  338. *
  339. */
  340. this.browserbot.shiftKeyDown = true;
  341. };
  342. Selenium.prototype.doShiftKeyUp = function() {
  343. /**
  344. * Release the shift key.
  345. *
  346. */
  347. this.browserbot.shiftKeyDown = false;
  348. };
  349. Selenium.prototype.doMetaKeyDown = function() {
  350. /**
  351. * Press the meta key and hold it down until doMetaUp() is called or a new page is loaded.
  352. *
  353. */
  354. this.browserbot.metaKeyDown = true;
  355. };
  356. Selenium.prototype.doMetaKeyUp = function() {
  357. /**
  358. * Release the meta key.
  359. *
  360. */
  361. this.browserbot.metaKeyDown = false;
  362. };
  363. Selenium.prototype.doAltKeyDown = function() {
  364. /**
  365. * Press the alt key and hold it down until doAltUp() is called or a new page is loaded.
  366. *
  367. */
  368. this.browserbot.altKeyDown = true;
  369. };
  370. Selenium.prototype.doAltKeyUp = function() {
  371. /**
  372. * Release the alt key.
  373. *
  374. */
  375. this.browserbot.altKeyDown = false;
  376. };
  377. Selenium.prototype.doControlKeyDown = function() {
  378. /**
  379. * Press the control key and hold it down until doControlUp() is called or a new page is loaded.
  380. *
  381. */
  382. this.browserbot.controlKeyDown = true;
  383. };
  384. Selenium.prototype.doControlKeyUp = function() {
  385. /**
  386. * Release the control key.
  387. *
  388. */
  389. this.browserbot.controlKeyDown = false;
  390. };
  391. Selenium.prototype.doKeyDown = function(locator, keySequence) {
  392. /**
  393. * Simulates a user pressing a key (without releasing it yet).
  394. *
  395. * @param locator an <a href="#locators">element locator</a>
  396. * @param keySequence Either be a string("\" followed by the numeric keycode
  397. * of the key to be pressed, normally the ASCII value of that key), or a single
  398. * character. For example: "w", "\119".
  399. */
  400. var element = this.browserbot.findElement(locator);
  401. triggerKeyEvent(element, 'keydown', keySequence, true,
  402. this.browserbot.controlKeyDown,
  403. this.browserbot.altKeyDown,
  404. this.browserbot.shiftKeyDown,
  405. this.browserbot.metaKeyDown);
  406. };
  407. Selenium.prototype.doKeyUp = function(locator, keySequence) {
  408. /**
  409. * Simulates a user releasing a key.
  410. *
  411. * @param locator an <a href="#locators">element locator</a>
  412. * @param keySequence Either be a string("\" followed by the numeric keycode
  413. * of the key to be pressed, normally the ASCII value of that key), or a single
  414. * character. For example: "w", "\119".
  415. */
  416. var element = this.browserbot.findElement(locator);
  417. triggerKeyEvent(element, 'keyup', keySequence, true,
  418. this.browserbot.controlKeyDown,
  419. this.browserbot.altKeyDown,
  420. this.browserbot.shiftKeyDown,
  421. this.browserbot.metaKeyDown);
  422. };
  423. function getClientXY(element, coordString) {
  424. // Parse coordString
  425. var coords = null;
  426. var x;
  427. var y;
  428. if (coordString) {
  429. coords = coordString.split(/,/);
  430. x = Number(coords[0]);
  431. y = Number(coords[1]);
  432. }
  433. else {
  434. x = y = 0;
  435. }
  436. // Get position of element,
  437. // Return 2 item array with clientX and clientY
  438. return [Selenium.prototype.getElementPositionLeft(element) + x, Selenium.prototype.getElementPositionTop(element) + y];
  439. }
  440. Selenium.prototype.doMouseOver = function(locator) {
  441. /**
  442. * Simulates a user hovering a mouse over the specified element.
  443. *
  444. * @param locator an <a href="#locators">element locator</a>
  445. */
  446. var element = this.browserbot.findElement(locator);
  447. this.browserbot.triggerMouseEvent(element, 'mouseover', true);
  448. };
  449. Selenium.prototype.doMouseOut = function(locator) {
  450. /**
  451. * Simulates a user moving the mouse pointer away from the specified element.
  452. *
  453. * @param locator an <a href="#locators">element locator</a>
  454. */
  455. var element = this.browserbot.findElement(locator);
  456. this.browserbot.triggerMouseEvent(element, 'mouseout', true);
  457. };
  458. Selenium.prototype.doMouseDown = function(locator) {
  459. /**
  460. * Simulates a user pressing the left mouse button (without releasing it yet) on
  461. * the specified element.
  462. *
  463. * @param locator an <a href="#locators">element locator</a>
  464. */
  465. var element = this.browserbot.findElement(locator);
  466. this.browserbot.triggerMouseEvent(element, 'mousedown', true);
  467. };
  468. Selenium.prototype.doMouseDownRight = function(locator) {
  469. /**
  470. * Simulates a user pressing the right mouse button (without releasing it yet) on
  471. * the specified element.
  472. *
  473. * @param locator an <a href="#locators">element locator</a>
  474. */
  475. var element = this.browserbot.findElement(locator);
  476. this.browserbot.triggerMouseEvent(element, 'mousedown', true, undefined, undefined, Selenium.RIGHT_MOUSE_CLICK);
  477. };
  478. Selenium.prototype.doMouseDownAt = function(locator, coordString) {
  479. /**
  480. * Simulates a user pressing the left mouse button (without releasing it yet) at
  481. * the specified location.
  482. *
  483. * @param locator an <a href="#locators">element locator</a>
  484. * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
  485. * event relative to the element returned by the locator.
  486. */
  487. var element = this.browserbot.findElement(locator);
  488. var clientXY = getClientXY(element, coordString)
  489. this.browserbot.triggerMouseEvent(element, 'mousedown', true, clientXY[0], clientXY[1]);
  490. };
  491. Selenium.prototype.doMouseDownRightAt = function(locator, coordString) {
  492. /**
  493. * Simulates a user pressing the right mouse button (without releasing it yet) at
  494. * the specified location.
  495. *
  496. * @param locator an <a href="#locators">element locator</a>
  497. * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
  498. * event relative to the element returned by the locator.
  499. */
  500. var element = this.browserbot.findElement(locator);
  501. var clientXY = getClientXY(element, coordString)
  502. this.browserbot.triggerMouseEvent(element, 'mousedown', true, clientXY[0], clientXY[1], Selenium.RIGHT_MOUSE_CLICK);
  503. };
  504. Selenium.prototype.doMouseUp = function(locator) {
  505. /**
  506. * Simulates the event that occurs when the user releases the mouse button (i.e., stops
  507. * holding the button down) on the specified element.
  508. *
  509. * @param locator an <a href="#locators">element locator</a>
  510. */
  511. var element = this.browserbot.findElement(locator);
  512. this.browserbot.triggerMouseEvent(element, 'mouseup', true);
  513. };
  514. Selenium.prototype.doMouseUpRight = function(locator) {
  515. /**
  516. * Simulates the event that occurs when the user releases the right mouse button (i.e., stops
  517. * holding the button down) on the specified element.
  518. *
  519. * @param locator an <a href="#locators">element locator</a>
  520. */
  521. var element = this.browserbot.findElement(locator);
  522. this.browserbot.triggerMouseEvent(element, 'mouseup', true, undefined, undefined, Selenium.RIGHT_MOUSE_CLICK);
  523. };
  524. Selenium.prototype.doMouseUpAt = function(locator, coordString) {
  525. /**
  526. * Simulates the event that occurs when the user releases the mouse button (i.e., stops
  527. * holding the button down) at the specified location.
  528. *
  529. * @param locator an <a href="#locators">element locator</a>
  530. * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
  531. * event relative to the element returned by the locator.
  532. */
  533. var element = this.browserbot.findElement(locator);
  534. var clientXY = getClientXY(element, coordString)
  535. this.browserbot.triggerMouseEvent(element, 'mouseup', true, clientXY[0], clientXY[1]);
  536. };
  537. Selenium.prototype.doMouseUpRightAt = function(locator, coordString) {
  538. /**
  539. * Simulates the event that occurs when the user releases the right mouse button (i.e., stops
  540. * holding the button down) at the specified location.
  541. *
  542. * @param locator an <a href="#locators">element locator</a>
  543. * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
  544. * event relative to the element returned by the locator.
  545. */
  546. var element = this.browserbot.findElement(locator);
  547. var clientXY = getClientXY(element, coordString)
  548. this.browserbot.triggerMouseEvent(element, 'mouseup', true, clientXY[0], clientXY[1], Selenium.RIGHT_MOUSE_CLICK);
  549. };
  550. Selenium.prototype.doMouseMove = function(locator) {
  551. /**
  552. * Simulates a user pressing the mouse button (without releasing it yet) on
  553. * the specified element.
  554. *
  555. * @param locator an <a href="#locators">element locator</a>
  556. */
  557. var element = this.browserbot.findElement(locator);
  558. this.browserbot.triggerMouseEvent(element, 'mousemove', true);
  559. };
  560. Selenium.prototype.doMouseMoveAt = function(locator, coordString) {
  561. /**
  562. * Simulates a user pressing the mouse button (without releasing it yet) on
  563. * the specified element.
  564. *
  565. * @param locator an <a href="#locators">element locator</a>
  566. * @param coordString specifies the x,y position (i.e. - 10,20) of the mouse
  567. * event relative to the element returned by the locator.
  568. */
  569. var element = this.browserbot.findElement(locator);
  570. var clientXY = getClientXY(element, coordString)
  571. this.browserbot.triggerMouseEvent(element, 'mousemove', true, clientXY[0], clientXY[1]);
  572. };
  573. Selenium.prototype.doType = function(locator, value) {
  574. /**
  575. * Sets the value of an input field, as though you typed it in.
  576. *
  577. * <p>Can also be used to set the value of combo boxes, check boxes, etc. In these cases,
  578. * value should be the value of the option selected, not the visible text.</p>
  579. *
  580. * @param locator an <a href="#locators">element locator</a>
  581. * @param value the value to type
  582. */
  583. if (this.browserbot.controlKeyDown || this.browserbot.altKeyDown || this.browserbot.metaKeyDown) {
  584. throw new SeleniumError("type not supported immediately after call to controlKeyDown() or altKeyDown() or metaKeyDown()");
  585. }
  586. // TODO fail if it can't be typed into.
  587. var element = this.browserbot.findElement(locator);
  588. if (this.browserbot.shiftKeyDown) {
  589. value = new String(value).toUpperCase();
  590. }
  591. this.browserbot.replaceText(element, value);
  592. };
  593. Selenium.prototype.doTypeKeys = function(locator, value) {
  594. /**
  595. * Simulates keystroke events on the specified element, as though you typed the value key-by-key.
  596. *
  597. * <p>This is a convenience method for calling keyDown, keyUp, keyPress for every character in the specified string;
  598. * this is useful for dynamic UI widgets (like auto-completing combo boxes) that require explicit key events.</p>
  599. *
  600. * <p>Unlike the simple "type" command, which forces the specified value into the page directly, this command
  601. * may or may not have any visible effect, even in cases where typing keys would normally have a visible effect.
  602. * For example, if you use "typeKeys" on a form element, you may or may not see the results of what you typed in
  603. * the field.</p>
  604. * <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
  605. * send the keystroke events corresponding to what you just typed.</p>
  606. *
  607. * @param locator an <a href="#locators">element locator</a>
  608. * @param value the value to type
  609. */
  610. var keys = new String(value).split("");
  611. for (var i = 0; i < keys.length; i++) {
  612. var c = keys[i];
  613. this.doKeyDown(locator, c);
  614. this.doKeyUp(locator, c);
  615. this.doKeyPress(locator, c);
  616. }
  617. };
  618. Selenium.prototype.doSetSpeed = function(value) {
  619. /**
  620. * 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.,
  621. * the delay is 0 milliseconds.
  622. *
  623. * @param value the number of milliseconds to pause after operation
  624. */
  625. throw new SeleniumError("this operation is only implemented in selenium-rc, and should never result in a request making it across the wire");
  626. };
  627. Selenium.prototype.getSpeed = function() {
  628. /**
  629. * 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.,
  630. * the delay is 0 milliseconds.
  631. *
  632. * See also setSpeed.
  633. *
  634. * @return string the execution speed in milliseconds.
  635. */
  636. throw new SeleniumError("this operation is only implemented in selenium-rc, and should never result in a request making it across the wire");
  637. };
  638. Selenium.prototype.findToggleButton = function(locator) {
  639. var element = this.browserbot.findElement(locator);
  640. if (element.checked == null) {
  641. Assert.fail("Element " + locator + " is not a toggle-button.");
  642. }
  643. return element;
  644. }
  645. Selenium.prototype.doCheck = function(locator) {
  646. /**
  647. * Check a toggle-button (checkbox/radio)
  648. *
  649. * @param locator an <a href="#locators">element locator</a>
  650. */
  651. this.findToggleButton(locator).checked = true;
  652. };
  653. Selenium.prototype.doUncheck = function(locator) {
  654. /**
  655. * Uncheck a toggle-button (checkbox/radio)
  656. *
  657. * @param locator an <a href="#locators">element locator</a>
  658. */
  659. this.findToggleButton(locator).checked = false;
  660. };
  661. Selenium.prototype.doSelect = function(selectLocator, optionLocator) {
  662. /**
  663. * Select an option from a drop-down using an option locator.
  664. *
  665. * <p>
  666. * Option locators provide different ways of specifying options of an HTML
  667. * Select element (e.g. for selecting a specific option, or for asserting
  668. * that the selected option satisfies a specification). There are several
  669. * forms of Select Option Locator.
  670. * </p>
  671. * <ul>
  672. * <li><strong>label</strong>=<em>labelPattern</em>:
  673. * matches options based on their labels, i.e. the visible text. (This
  674. * is the default.)
  675. * <ul class="first last simple">
  676. * <li>label=regexp:^[Oo]ther</li>
  677. * </ul>
  678. * </li>
  679. * <li><strong>value</strong>=<em>valuePattern</em>:
  680. * matches options based on their values.
  681. * <ul class="first last simple">
  682. * <li>value=other</li>
  683. * </ul>
  684. *
  685. *
  686. * </li>
  687. * <li><strong>id</strong>=<em>id</em>:
  688. *
  689. * matches options based on their ids.
  690. * <ul class="first last simple">
  691. * <li>id=option1</li>
  692. * </ul>
  693. * </li>
  694. * <li><strong>index</strong>=<em>index</em>:
  695. * matches an option based on its index (offset from zero).
  696. * <ul class="first last simple">
  697. *
  698. * <li>index=2</li>
  699. * </ul>
  700. * </li>
  701. * </ul>
  702. * <p>
  703. * If no option locator prefix is provided, the default behaviour is to match on <strong>label</strong>.
  704. * </p>
  705. *
  706. *
  707. * @param selectLocator an <a href="#locators">element locator</a> identifying a drop-down menu
  708. * @param optionLocator an option locator (a label by default)
  709. */
  710. var element = this.browserbot.findElement(selectLocator);
  711. if (!("options" in element)) {
  712. throw new SeleniumError("Specified element is not a Select (has no options)");
  713. }
  714. var locator = this.optionLocatorFactory.fromLocatorString(optionLocator);
  715. var option = locator.findOption(element);
  716. this.browserbot.selectOption(element, option);
  717. };
  718. Selenium.prototype.doAddSelection = function(locator, optionLocator) {
  719. /**
  720. * Add a selection to the set of selected options in a multi-select element using an option locator.
  721. *
  722. * @see #doSelect for details of option locators
  723. *
  724. * @param locator an <a href="#locators">element locator</a> identifying a multi-select box
  725. * @param optionLocator an option locator (a label by default)
  726. */
  727. var element = this.browserbot.findElement(locator);
  728. if (!("options" in element)) {
  729. throw new SeleniumError("Specified element is not a Select (has no options)");
  730. }
  731. var locator = this.optionLocatorFactory.fromLocatorString(optionLocator);
  732. var option = locator.findOption(element);
  733. this.browserbot.addSelection(element, option);
  734. };
  735. Selenium.prototype.doRemoveSelection = function(locator, optionLocator) {
  736. /**
  737. * Remove a selection from the set of selected options in a multi-select element using an option locator.
  738. *
  739. * @see #doSelect for details of option locators
  740. *
  741. * @param locator an <a href="#locators">element locator</a> identifying a multi-select box
  742. * @param optionLocator an option locator (a label by default)
  743. */
  744. var element = this.browserbot.findElement(locator);
  745. if (!("options" in element)) {
  746. throw new SeleniumError("Specified element is not a Select (has no options)");
  747. }
  748. var locator = this.optionLocatorFactory.fromLocatorString(optionLocator);
  749. var option = locator.findOption(element);
  750. this.browserbot.removeSelection(element, option);
  751. };
  752. Selenium.prototype.doRemoveAllSelections = function(locator) {
  753. /**
  754. * Unselects all of the selected options in a multi-select element.
  755. *
  756. * @param locator an <a href="#locators">element locator</a> identifying a multi-select box
  757. */
  758. var element = this.browserbot.findElement(locator);
  759. if (!("options" in element)) {
  760. throw new SeleniumError("Specified element is not a Select (has no options)");
  761. }
  762. for (var i = 0; i < element.options.length; i++) {
  763. this.browserbot.removeSelection(element, element.options[i]);
  764. }
  765. }
  766. Selenium.prototype.doSubmit = function(formLocator) {
  767. /**
  768. * Submit the specified form. This is particularly useful for forms without
  769. * submit buttons, e.g. single-input "Search" forms.
  770. *
  771. * @param formLocator an <a href="#locators">element locator</a> for the form you want to submit
  772. */
  773. var form = this.browserbot.findElement(formLocator);
  774. return this.browserbot.submit(form);
  775. };
  776. Selenium.prototype.makePageLoadCondition = function(timeout) {
  777. if (timeout == null) {
  778. timeout = this.defaultTimeout;
  779. }
  780. // if the timeout is zero, we won't wait for the page to load before returning
  781. if (timeout == 0) {
  782. return;
  783. }
  784. return Selenium.decorateFunctionWithTimeout(fnBind(this._isNewPageLoaded, this), timeout);
  785. };
  786. Selenium.prototype.doOpen = function(url) {
  787. /**
  788. * Opens an URL in the test frame. This accepts both relative and absolute
  789. * URLs.
  790. *
  791. * The &quot;open&quot; command waits for the page to load before proceeding,
  792. * ie. the &quot;AndWait&quot; suffix is implicit.
  793. *
  794. * <em>Note</em>: The URL must be on the same domain as the runner HTML
  795. * due to security restrictions in the browser (Same Origin Policy). If you
  796. * need to open an URL on another domain, use the Selenium Server to start a
  797. * new browser session on that domain.
  798. *
  799. * @param url the URL to open; may be relative or absolute
  800. */
  801. this.browserbot.openLocation(url);
  802. if (window["proxyInjectionMode"] == null || !window["proxyInjectionMode"]) {
  803. return this.makePageLoadCondition();
  804. } // in PI mode, just return "OK"; the server will waitForLoad
  805. };
  806. Selenium.prototype.doOpenWindow = function(url, windowID) {
  807. /**
  808. * Opens a popup window (if a window with that ID isn't already open).
  809. * After opening the window, you'll need to select it using the selectWindow
  810. * command.
  811. *
  812. * <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).
  813. * In those cases, you can force Selenium to notice the open window's name by using the Selenium openWindow command, using
  814. * an empty (blank) url, like this: openWindow("", "myFunnyWindow").</p>
  815. *
  816. * @param url the URL to open, which can be blank
  817. * @param windowID the JavaScript window ID of the window to select
  818. */
  819. this.browserbot.openWindow(url, windowID);
  820. };
  821. Selenium.prototype.doSelectWindow = function(windowID) {
  822. /**
  823. * Selects a popup window using a window locator; once a popup window has been selected, all
  824. * commands go to that window. To select the main window again, use null
  825. * as the target.
  826. *
  827. * <p>
  828. *
  829. * Window locators provide different ways of specifying the window object:
  830. * by title, by internal JavaScript "name," or by JavaScript variable.
  831. * </p>
  832. * <ul>
  833. * <li><strong>title</strong>=<em>My Special Window</em>:
  834. * Finds the window using the text that appears in the title bar. Be careful;
  835. * two windows can share the same title. If that happens, this locator will
  836. * just pick one.
  837. * </li>
  838. * <li><strong>name</strong>=<em>myWindow</em>:
  839. * Finds the window using its internal JavaScript "name" property. This is the second
  840. * parameter "windowName" passed to the JavaScript method window.open(url, windowName, windowFeatures, replaceFlag)
  841. * (which Selenium intercepts).
  842. * </li>
  843. * <li><strong>var</strong>=<em>variableName</em>:
  844. * Some pop-up windows are unnamed (anonymous), but are associated with a JavaScript variable name in the current
  845. * application window, e.g. "window.foo = window.open(url);". In those cases, you can open the window using
  846. * "var=foo".
  847. * </li>
  848. * </ul>
  849. * <p>
  850. * If no window locator prefix is provided, we'll try to guess what you mean like this:</p>
  851. * <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>
  852. * <p>2.) if the value of the "windowID" parameter is a JavaScript variable name in the current application window, then it is assumed
  853. * that this variable contains the return value from a call to the JavaScript window.open() method.</p>
  854. * <p>3.) Otherwise, selenium looks in a hash it maintains that maps string names to window "names".</p>
  855. * <p>4.) If <em>that</em> fails, we'll try looping over all of the known windows to try to find the appropriate "title".
  856. * Since "title" is not necessarily unique, this may have unexpected behavior.</p>
  857. *
  858. * <p>If you're having trouble figuring out the name of a window that you want to manipulate, look at the Selenium log messages
  859. * which identify the names of windows created via window.open (and therefore intercepted by Selenium). You will see messages
  860. * like the following for each window as it is opened:</p>
  861. *
  862. * <p><code>debug: window.open call intercepted; window ID (which you can use with selectWindow()) is "myNewWindow"</code></p>
  863. *
  864. * <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).
  865. * (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
  866. * an empty (blank) url, like this: openWindow("", "myFunnyWindow").</p>
  867. *
  868. * @param windowID the JavaScript window ID of the window to select
  869. */
  870. this.browserbot.selectWindow(windowID);
  871. };
  872. Selenium.prototype.doSelectFrame = function(locator) {
  873. /**
  874. * Selects a frame within the current window. (You may invoke this command
  875. * multiple times to select nested frames.) To select the parent frame, use
  876. * "relative=parent" as a locator; to select the top frame, use "relative=top".
  877. * You can also select a frame by its 0-based index number; select the first frame with
  878. * "index=0", or the third frame with "index=2".
  879. *
  880. * <p>You may also use a DOM expression to identify the frame you want directly,
  881. * like this: <code>dom=frames["main"].frames["subframe"]</code></p>
  882. *
  883. * @param locator an <a href="#locators">element locator</a> identifying a frame or iframe
  884. */
  885. this.browserbot.selectFrame(locator);
  886. };
  887. Selenium.prototype.getWhetherThisFrameMatchFrameExpression = function(currentFrameString, target) {
  888. /**
  889. * Determine whether current/locator identify the frame containing this running code.
  890. *
  891. * <p>This is useful in proxy injection mode, where this code runs in every
  892. * browser frame and window, and sometimes the selenium server needs to identify
  893. * the "current" frame. In this case, when the test calls selectFrame, this
  894. * routine is called for each frame to figure out which one has been selected.
  895. * The selected frame will return true, while all others will return false.</p>
  896. *
  897. * @param currentFrameString starting frame
  898. * @param target new frame (which might be relative to the current one)
  899. * @return boolean true if the new frame is this code's window
  900. */
  901. return this.browserbot.doesThisFrameMatchFrameExpression(currentFrameString, target);
  902. };
  903. Selenium.prototype.getWhetherThisWindowMatchWindowExpression = function(currentWindowString, target) {
  904. /**
  905. * Determine whether currentWindowString plus target identify the window containing this running code.
  906. *
  907. * <p>This is useful in proxy injection mode, where this code runs in every
  908. * browser frame and window, and sometimes the selenium server needs to identify
  909. * the "current" window. In this case, when the test calls selectWindow, this
  910. * routine is called for each window to figure out which one has been selected.
  911. * The selected window will return true, while all others will return false.</p>
  912. *
  913. * @param currentWindowString starting window
  914. * @param target new window (which might be relative to the current one, e.g., "_parent")
  915. * @return boolean true if the new window is this code's window
  916. */
  917. if (window.opener!=null && window.opener[target]!=null && window.opener[target]==window) {
  918. return true;
  919. }
  920. return false;
  921. };
  922. Selenium.prototype.doWaitForPopUp = function(windowID, timeout) {
  923. /**
  924. * Waits for a popup window to appear and load up.
  925. *
  926. * @param windowID the JavaScript window "name" of the window that will appear (not the text of the title bar)
  927. * @param timeout a timeout in milliseconds, after which the action will return with an error
  928. */
  929. var popupLoadedPredicate = function () {
  930. var targetWindow = selenium.browserbot.getWindowByName(windowID, true);
  931. if (!targetWindow) return false;
  932. if (!targetWindow.location) return false;
  933. if ("about:blank" == targetWindow.location) return false;
  934. if (browserVersion.isKonqueror) {
  935. if ("/" == targetWindow.location.href) {
  936. // apparently Konqueror uses this as the temporary location, instead of about:blank
  937. return false;
  938. }
  939. }
  940. if (browserVersion.isSafari) {
  941. if(targetWindow.location.href == selenium.browserbot.buttonWindow.location.href) {
  942. // Apparently Safari uses this as the temporary location, instead of about:blank
  943. // what a world!
  944. LOG.debug("DGF what a world!");
  945. return false;
  946. }
  947. }
  948. if (!targetWindow.document) return false;
  949. if (!selenium.browserbot.getCurrentWindow().document.readyState) {
  950. // This is Firefox, with no readyState extension
  951. return true;
  952. }
  953. if ('complete' != targetWindow.document.readyState) return false;
  954. return true;
  955. };
  956. return Selenium.decorateFunctionWithTimeout(popupLoadedPredicate, timeout);
  957. }
  958. Selenium.prototype.doWaitForPopUp.dontCheckAlertsAndConfirms = true;
  959. Selenium.prototype.doChooseCancelOnNextConfirmation = function() {
  960. /**
  961. * <p>
  962. * By default, Selenium's overridden window.confirm() function will
  963. * return true, as if the user had manually clicked OK; after running
  964. * this command, the next call to confirm() will return false, as if
  965. * the user had clicked Cancel. Selenium will then resume using the
  966. * default behavior for future confirmations, automatically returning
  967. * true (OK) unless/until you explicitly call this command for each
  968. * confirmation.
  969. * </p>
  970. * <p>
  971. * Take note - every time a confirmation comes up, you must
  972. * consume it with a corresponding getConfirmation, or else
  973. * the next selenium operation will fail.
  974. * </p>
  975. */
  976. this.browserbot.cancelNextConfirmation(false);
  977. };
  978. Selenium.prototype.doChooseOkOnNextConfirmation = function() {
  979. /**
  980. * <p>
  981. * Undo the effect of calling chooseCancelOnNextConfirmation. Note
  982. * that Selenium's overridden window.confirm() function will normally automatically
  983. * return true, as if the user had manually clicked OK, so you shouldn't
  984. * need to use this command unless for some reason you need to change
  985. * your mind prior to the next confirmation. After any confirmation, Selenium will resume using the
  986. * default behavior for future confirmations, automatically returning
  987. * true (OK) unless/until you explicitly call chooseCancelOnNextConfirmation for each
  988. * confirmation.
  989. * </p>
  990. * <p>
  991. * Take note - every time a confirmation comes up, you must
  992. * consume it with a corresponding getConfirmation, or else
  993. * the next selenium operation will fail.
  994. * </p>
  995. *
  996. */
  997. this.browserbot.cancelNextConfirmation(true);
  998. };
  999. Selenium.prototype.doAnswerOnNextPrompt = function(answer) {
  1000. /**
  1001. * Instructs Selenium to return the specified answer string in response to
  1002. * the next JavaScript prompt [window.prompt()].
  1003. *
  1004. *
  1005. * @param answer the answer to give in response to the prompt pop-up
  1006. */
  1007. this.browserbot.setNextPromptResult(answer);
  1008. };
  1009. Selenium.prototype.doGoBack = function() {
  1010. /**
  1011. * Simulates the user clicking the "back" button on their browser.
  1012. *
  1013. */
  1014. this.browserbot.goBack();
  1015. };
  1016. Selenium.prototype.doRefresh = function() {
  1017. /**
  1018. * Simulates the user clicking the "Refresh" button on their browser.
  1019. *
  1020. */
  1021. this.browserbot.refresh();
  1022. };
  1023. Selenium.prototype.doClose = function() {
  1024. /**
  1025. * Simulates the user clicking the "close" button in the titlebar of a popup
  1026. * window or tab.
  1027. */
  1028. this.browserbot.close();
  1029. };
  1030. Selenium.prototype.ensureNoUnhandledPopups = function() {
  1031. if (this.browserbot.hasAlerts()) {
  1032. throw new SeleniumError("There was an unexpected Alert! [" + this.browserbot.getNextAlert() + "]");
  1033. }
  1034. if ( this.browserbot.hasConfirmations() ) {
  1035. throw new SeleniumError("There was an unexpected Confirmation! [" + this.browserbot.getNextConfirmation() + "]");
  1036. }
  1037. };
  1038. Selenium.prototype.isAlertPresent = function() {
  1039. /**
  1040. * Has an alert occurred?
  1041. *
  1042. * <p>
  1043. * This function never throws an exception
  1044. * </p>
  1045. * @return boolean true if there is an alert
  1046. */
  1047. return this.browserbot.hasAlerts();
  1048. };
  1049. Selenium.prototype.isPromptPresent = function() {
  1050. /**
  1051. * Has a prompt occurred?
  1052. *
  1053. * <p>
  1054. * This function never throws an exception
  1055. * </p>
  1056. * @return boolean true if there is a pending prompt
  1057. */
  1058. return this.browserbot.hasPrompts();
  1059. };
  1060. Selenium.prototype.isConfirmationPresent = function() {
  1061. /**
  1062. * Has confirm() been called?
  1063. *
  1064. * <p>
  1065. * This function never throws an exception
  1066. * </p>
  1067. * @return boolean true if there is a pending confirmation
  1068. */
  1069. return this.browserbot.hasConfirmations();
  1070. };
  1071. Selenium.prototype.getAlert = function() {
  1072. /**
  1073. * Retrieves the message of a JavaScript alert generated during the previous action, or fail if there were no alerts.
  1074. *
  1075. * <p>Getting an alert has the same effect as manually clicking OK. If an
  1076. * alert is generated but you do not consume it with getAlert, the next Selenium action
  1077. * will fail.</p>
  1078. *
  1079. * <p>Under Selenium, JavaScript alerts will NOT pop up a visible alert
  1080. * dialog.</p>
  1081. *
  1082. * <p>Selenium does NOT support JavaScript alerts that are generated in a
  1083. * page's onload() event handler. In this case a visible dialog WILL be
  1084. * generated and Selenium will hang until someone manually clicks OK.</p>
  1085. * @return string The message of the most recent JavaScript alert
  1086. */
  1087. if (!this.browserbot.hasAlerts()) {
  1088. Assert.fail("There were no alerts");
  1089. }
  1090. return this.browserbot.getNextAlert();
  1091. };
  1092. Selenium.prototype.getAlert.dontCheckAlertsAndConfirms = true;
  1093. Selenium.prototype.getConfirmation = function() {
  1094. /**
  1095. * Retrieves the message of a JavaScript confirmation dialog generated during
  1096. * the previous action.
  1097. *
  1098. * <p>
  1099. * By default, the confirm function will return true, having the same effect
  1100. * as manually clicking OK. This can be changed by prior execution of the
  1101. * chooseCancelOnNextConfirmation command.
  1102. * </p>
  1103. * <p>
  1104. * If an confirmation is generated but you do not consume it with getConfirmation,
  1105. * the next Selenium action will fail.
  1106. * </p>
  1107. *
  1108. * <p>
  1109. * NOTE: under Selenium, JavaScript confirmations will NOT pop up a visible
  1110. * dialog.
  1111. * </p>
  1112. *
  1113. * <p>
  1114. * NOTE: Selenium does NOT support JavaScript confirmations that are
  1115. * generated in a page's onload() event handler. In this case a visible
  1116. * dialog WILL be generated and Selenium will hang until you manually click
  1117. * OK.
  1118. * </p>
  1119. *
  1120. * @return string the message of the most recent JavaScript confirmation dialog
  1121. */
  1122. if (!this.browserbot.hasConfirmations()) {
  1123. Assert.fail("There were no confirmations");
  1124. }
  1125. return this.browserbot.getNextConfirmation();
  1126. };
  1127. Selenium.prototype.getConfirmation.dontCheckAlertsAndConfirms = true;
  1128. Selenium.prototype.getPrompt = function() {
  1129. /**
  1130. * Retrieves the message of a JavaScript question prompt dialog generated during
  1131. * the previous action.
  1132. *
  1133. * <p>Successful handling of the prompt requires prior execution of the
  1134. * answerOnNextPrompt command. If a prompt is generated but you
  1135. * do not get/verify it, the next Selenium action will fail.</p>
  1136. *
  1137. * <p>NOTE: under Selenium, JavaScript prompts will NOT pop up a visible
  1138. * dialog.</p>
  1139. *
  1140. * <p>NOTE: Selenium does NOT support JavaScript prompts that are generated in a
  1141. * page's onload() event handler. In this case a visible dialog WILL be
  1142. * generated and Selenium will hang until someone manually clicks OK.</p>
  1143. * @return string the message of the most recent JavaScript question prompt
  1144. */
  1145. if (! this.browserbot.hasPrompts()) {
  1146. Assert.fail("There were no prompts");
  1147. }
  1148. return this.browserbot.getNextPrompt();
  1149. };
  1150. Selenium.prototype.getLocation = function() {
  1151. /** Gets the absolute URL of the current page.
  1152. *
  1153. * @return string the absolute URL of the current page
  1154. */
  1155. return this.browserbot.getCurrentWindow().location.href;
  1156. };
  1157. Selenium.prototype.getTitle = function() {
  1158. /** Gets the title of the current page.
  1159. *
  1160. * @return string the title of the current page
  1161. */
  1162. return this.browserbot.getTitle();
  1163. };
  1164. Selenium.prototype.getBodyText = function() {
  1165. /**
  1166. * Gets the entire text of the page.
  1167. * @return string the entire text of the page
  1168. */
  1169. return this.browserbot.bodyText();
  1170. };
  1171. Selenium.prototype.getValue = function(locator) {
  1172. /**
  1173. * Gets the (whitespace-trimmed) value of an input field (or anything else with a value parameter).
  1174. * For checkbox/radio elements, the value will be "on" or "off" depending on
  1175. * whether the element is checked or not.
  1176. *
  1177. * @param locator an <a href="#locators">element locator</a>
  1178. * @return string the element value, or "on/off" for checkbox/radio elements
  1179. */
  1180. var element = this.browserbot.findElement(locator)
  1181. return getInputValue(element).trim();
  1182. }
  1183. Selenium.prototype.getText = function(locator) {
  1184. /**
  1185. * Gets the text of an element. This works for any element that contains
  1186. * text. This command uses either the textContent (Mozilla-like browsers) or
  1187. * the innerText (IE-like browsers) of the element, which is the rendered
  1188. * text shown to the user.
  1189. *
  1190. * @param locator an <a href="#locators">element locator</a>
  1191. * @return string the text of the element
  1192. */
  1193. var element = this.browserbot.findElement(locator);
  1194. return getText(element).trim();
  1195. };
  1196. Selenium.prototype.doHighlight = function(locator) {
  1197. /**
  1198. * Briefly changes the backgroundColor of the specified element yellow. Useful for debugging.
  1199. *
  1200. * @param locator an <a href="#locators">element locator</a>…