Selenese Commands
A command refers to what Selenium has to do and commands in selenium are of three types. Click on each one of them to know more about the commands.
Actions:
Actions are commands that manipulate the state of the application. Upon execution, if an action fails the execution of the current test is stopped. For Example, "click a link" and "select an option".
The following table lists the Selenium action commands that are used very frequently, however the list is note exhaustive.
| Command/Syntax | Description |
|---|---|
| click (locator) | Clicks on a link, button, checkbox or radio button |
| clickAt (locator,coordString) | Clicks on an element with the help of locator and co-ordinates |
| close() | Simulates the user clicking the "close" button in the title bar of a popup window or tab. |
| contextMenuAt (locator,coordString) | Simulates opening the context menu of the specified element from a specified location |
| doubleClick (locator) | Double clicks on a webelement based on the specified element. |
| dragAndDrop (locator,movementsString) | Drags an element and then drops it based on specified distance. |
| dragAndDropToObject (Dragobject,dropobject) | Drags an element and drops it on another element. |
| echo (message) | Prints the specified message on console which is used for debugging. |
| fireEvent (locator,eventName) | Explicitly simulate an event, to trigger the corresponding "onevent" handler |
| focus (locator) | Move the focus to the specified element |
| highlight (locator) | Changes the background Color of the specified element to yellow Briefly which is useful for debugging purposes. |
| mouseDown (locator) | Simulates a user pressing the left mouse button on the specified element. |
| mouseDownAt (locator,coordString) | Simulates a user pressing the left mouse button at the specified location on the specified element. |
| mouseUp (locator) | Simulates the event that occurs when the user releases the mouse button |
| mouseUpAt (locator,coordString) | Simulates the event that occurs when the user releases the mouse button at the specified location. |
| open (url) | Opens an URL in the specified browser and it accepts both relative and absolute URLs. |
| openWindow (url,windowID) | Opens a popup window. After opening the window, user need to activate it using the selectWindow command. |
| pause (waitTime) | Waits for the specified amount of time (in milliseconds) |
| refresh() | Simulates the user clicking the "Refresh" button on their browser. |
| select (selectLocator,optionLocator) | Select an option from a drop-down using an option locator. |
| selectWindow (windowID) | Selects a popup window using a window locator; once a popup window has been selected, all focus shifts to that window. |
| store (expression,variableName) | The name of a variable in which the result is to be stored and expression is the value to store |
| type (locator,value) | Sets the value of an input field, similar to user typing action. |
| typeKeys (locator,value) | Simulates keystroke events on the specified element, as though you typed the value key-by-key. |
| waitForCondition (script,timeout) | Executes the specified JavaScript snippet repeatedly until it evaluates to "true". |
| waitForPageToLoad (timeout) | Waits for a new page to load. |
| waitForPopUp (windowID,timeout) | Waits for a popup window to appear and load. |
| windowFocus() | Gives focus to the currently selected window |
| windowMaximize() | Resize currently selected window to take up the entire screen |
Accessors:
Accessors evaluate the state of the application and store the results in a variable which are used in assertions. For Example, "storeTitle".
The following table lists the Selenium accessors that are used very frequently, however the list is not exhaustive.
| Command/Syntax | Description |
|---|---|
| assertErrorOnNext (message) | Pings Selenium to expect an error on the next command execution with an expected message. |
| storeAllButtons (variableName) | Returns the IDs of all buttons on the page. |
| storeAllFields (variableName) | Returns the IDs of all input fields on the page. |
| storeAllLinks (variableName) | Returns the IDs of all links on the page. |
| storeAllWindowIds (variableName) | Returns the IDs of all windows that the browser knows about in an array. |
| storeAllWindowTitles (variableName) | Returns the names of all windows that the browser knows about in an array. |
| storeAllWindowNames (variableName) | Returns the titles of all windows that the browser knows about in an array. |
| storeAttribute (attributeLocator, variableName) | Gets the value of an element attribute. The value of the attribute may differ across browsers. |
| storeBodyText (variableName) | Gets the entire text of the page. |
| storeConfirmation (variableName) | Retrieves the message of a JavaScript confirmation dialog generated during the previous action. |
| storeElementIndex (locator, variableName) | Get the relative index of an element to its parent (starting from 0). |
| storeLocation (variableName) | Gets the absolute URL of the current page. |
| storeSelectedIds (selectLocator,variableName) | Gets all element IDs for selected options in the specified select or multi-select element. |
| storeSelectedIndex (selectLocator, variableName) | Gets index (option number, starting at 0) for selected option in the specified select element. |
| storeSelectedLabel (selectLocator, variableName) | Gets label (visible text) for selected option in the specified select element.. |
| storeSelectedValue (selectLocator,variableName) | Gets value (value attribute) for selected option in the specified select element. |
| storeSelectOptions (selectLocator,variableName) | Gets all labels in the specified select drop-down. |
| storeTable (tableCellAddress, variableName) | Gets the text from a cell of a table. The cellAddress syntax: tableLocator.row.column, where row and column start at 0. |
| storeText (locator, variableName) | Gets the text of an element. This works for any element that contains text. |
| storeTitle (variableName) | Gets the title of the current page. |
| storeValue (locator,variableName) | Gets the (whitespace-trimmed) value of an input field. |
| storeChecked (locator, variableName) | Gets whether a toggle-button (checkbox/radio) is checked. |
| storeElementPresent (locator, variableName) | Verifies that the specified element is somewhere on the page. |
| storeTextPresent (pattern, variableName) | Verifies that the specified text pattern appears somewhere on the rendered page shown to the user. |
| storeVisible (locator, variableName) | Determines if the specified element is visible. |
Assertions:
Assertions enable us to verify the state of an application and compares against the expected. It is used in 3 modes, viz. - "assert", "verify", and "waitfor". For Example, "verify if an item form the dropdown is selected".
The following table lists the Selenium assertions that are used very frequently, however the list is not exhaustive.
| Command/Syntax | Description |
|---|---|
| waitForErrorOnNext (message) | Waits for error; used with the accessor assertErrorOnNext. |
| verifySelected(selectLocator, optionLocator) | Verifies that the selected option of a drop-down satisfies the optionSpecifier. |
| waitForSelected (selectLocator, optionLocator) | Waits for getting the option selected; used with the accessor assertSelected. |
| waitForNotSelected (selectLocator, optionLocator) | Waits for not getting the option selected; used with accessor the assertSelected. |
| verifyAlert (pattern) | Verifies the alert text; used the with accessor storeAlert. |
| waitForAlert (pattern) | Waits for the alert; used with the accessor storeAlert. |
| verifyAllButtons (pattern) | Verifies the button; used with the accessor storeAllButtons. |
| waitForAllButtons (pattern) | Waits for the button to load; used with the accessor storeAllButtons. |
| verifyAllLinks (pattern) | Verifies all links; used with the accessor storeAllLinks. |
| waitForAllLinks (pattern) | Waits for all links; used with the accessor storeAllLinks. |
| verifyAllWindowIds (pattern) | Verifies the window id; used with the accessor storeAllWindowIds. |
| waitForAllWindowIds (pattern ) | Waits the window id; used with the accessor storeAllWindowIds. |
| verifyAttribute(attributeLocator, pattern) | Verifies an attribute of an element; used with the accessor storeAttribute. |
| waitForAttribute(attributeLocator, pattern) | Waits for an attribute of an element; used with accessor storeAttribute. |
| verifyBodyText(pattern) | Verifies the body text; used with the accessor storeBodyText. |
| waitForBodyText(pattern) | Waits for the body text; used with the accessor storeBodyText. |
| waitForConfirmation(pattern) | Waits for confirmation; used with the accessor storeConfirmationPresent. |
Locators
Element Locators help Selenium to identify the HTML element the command refers to. All these locators can be identified with the help of FirePath and FireBug plugin of Mozilla. Please refer the Environment Setup chapter for details.
- identifier=id Select the element with the specified "id" attribute and if there is no match, select the first element whose @name attribute is id.
- id=id Select the element with the specified "id" attribute.
- name=name Select the first element with the specified "name" attribute
- dom=javascriptExpression Selenium finds an element by evaluating the specified string that allows us to traverse through the HTML Document Object Model using JavaScript. Users cannot return a value but can evaluate as an expression in the block.
- xpath=xpathExpression Locate an element using an XPath expression.
- link=textPattern Select the link element (within anchor tags) which contains text matching the specified pattern.
- css=cssSelectorSyntax Select the element using css selector.