Selenium Commands
Selenium Commands
Selenium Commands
html
search selenium: Go
Navigation
Selenium Commands
Selenium Documentation Selenium commands, often called selenese, are the set of commands that run your tests. A
» sequence of these commands is a test script. Here we explain those commands in detail, and we
present the many choices you have in testing your web application when using Selenium.
previous | next
verifyTextPresent
The command verifyTextPresent is used to verify specific text exists somewhere on the
page. It takes a single argument–the text pattern to be verified. For example:
verifyElementPresent
Use this command when you must test for the presence of a specific UI element, rather
then its content. This verification does not check the text, only the HTML tag. One common
use is to check for the presence of an image.
1 of 10 10/7/2010 12:53 PM
Selenium Commands — Selenium Documentation http://seleniumhq.org/docs/04_selenese_commands.html
verifyElementPresent can be used to check the existence of any HTML tag within the page.
You can check the existence of links, paragraphs, divisions <div>, etc. Here are a few more
examples.
verifyText
Use verifyText when both the text and its UI element must be tested. verifyText must use
a locator. If you choose an XPath or DOM locator, you can verify that specific text appears
at a specific location on the page relative to other UI components on the page.
Locating Elements
For many Selenium commands, a target is required. This target identifies an element in the
content of the web application, and consists of the location strategy followed by the location in
the format locatorType=location. The locator type can be omitted in many cases. The various
locator types are explained below with examples for each.
Locating by Identifier
This is probably the most common method of locating elements and is the catch-all default
when no recognised locator type is used. With this strategy, the first element with the id
attribute value matching the location will be used. If no element has a matching id
attribute, then the first element with a name attribute matching the location will be used.
For instance, your page source could have id and name attributes as follows:
1 <html>
2 <body>
3 <form id="loginForm">
4 <input name="username" type="text" />
5 <input name="password" type="password" />
6 <input name="continue" type="submit" value="Login" />
7 </form>
8 </body>
9 <html>
The following locator strategies would return the elements from the HTML snippet above
indicated by line number:
identifier=loginForm (3)
identifier=username (4)
identifier=continue (5)
continue (5)
Since the identifier type of locator is the default, the identifier= in the first three
examples above is not necessary.
Locating by Id
This type of locator is more limited than the identifier locator type, but also more explicit.
Use this when you know an element’s id attribute.
2 of 10 10/7/2010 12:53 PM
Selenium Commands — Selenium Documentation http://seleniumhq.org/docs/04_selenese_commands.html
1 <html>
2 <body>
3 <form id="loginForm">
4 <input name="username" type="text" />
5 <input name="password" type="password" />
6 <input name="continue" type="submit" value="Login" />
7 <input name="continue" type="button" value="Clear" />
8 </form>
9 </body>
10 <html>
id=loginForm (3)
Locating by Name
The name locator type will locate the first element with a matching name attribute. If
multiple elements have the same value for a name attribute, then you can use filters to
further refine your location strategy. The default filter type is value (matching the value
attribute).
1 <html>
2 <body>
3 <form id="loginForm">
4 <input name="username" type="text" />
5 <input name="password" type="password" />
6 <input name="continue" type="submit" value="Login" />
7 <input name="continue" type="button" value="Clear" />
8 </form>
9 </body>
10 <html>
name=username (4)
name=continue value=Clear (7)
name=continue Clear (7)
name=continue type=button (7)
Note
Unlike some types of XPath and DOM locators, the three types of locators above allow
Selenium to test a UI element independent of its location on the page. So if the page
structure and organization is altered, the test will still pass. You may or may not want to
also test whether the page structure changes. In the case where web designers frequently
alter the page, but its functionality must be regression tested, testing via id and name
attributes, or really via any HTML property, becomes very important.
Locating by XPath
XPath is the language used for locating nodes in an XML document. As HTML can be an
implementation of XML (XHTML), Selenium users can leverage this powerful language to
target elements in their web applications. XPath extends beyond (as well as supporting) the
simple methods of locating by id or name attributes, and opens up all sorts of new
possibilities such as locating the third checkbox on the page.
One of the main reasons for using XPath is when you don’t have a suitable id or name
attribute for the element you wish to locate. You can use XPath to either locate the element
in absolute terms (not advised), or relative to an element that does have an id or name
attribute. XPath locators can also be used to specify elements via attributes other than id
and name.
Absolute XPaths contain the location of all elements from the root (html) and as a result
are likely to fail with only the slightest adjustment to the application. By finding a nearby
element with an id or name attribute (ideally a parent element) you can locate your target
element based on the relationship. This is much less likely to change and can make your
tests more robust.
Since only xpath locators start with “//”, it is not necessary to include the xpath= label when
specifying an XPath locator.
1 <html>
2 <body>
3 <form id="loginForm">
4 <input name="username" type="text" />
5 <input name="password" type="password" />
6 <input name="continue" type="submit" value="Login" />
7 <input name="continue" type="button" value="Clear" />
8 </form>
9 </body>
10 <html>
3 of 10 10/7/2010 12:53 PM
Selenium Commands — Selenium Documentation http://seleniumhq.org/docs/04_selenese_commands.html
xpath=/html/body/form[1] (3) - Absolute path (would break if the HTML was changed
only slightly)
//form[1] (3) - First form element in the HTML
xpath=//form[@id='loginForm'] (3) - The form element with attribute named ‘id’ and
the value ‘loginForm’
xpath=//form[input/\@name='username'] (4) - First form element with an input child
element with attribute named ‘name’ and the value ‘username’
//input[@name='username'] (4) - First input element with attribute named ‘name’ and
the value ‘username’
//form[@id='loginForm']/input[1] (4) - First input child element of the form element
with attribute named ‘id’ and the value ‘loginForm’
//input[@name='continue'][@type='button'] (7) - Input with attribute named ‘name’
and the value ‘continue’ and attribute named ‘type’ and the value ‘button’
//form[@id='loginForm']/input[4] (7) - Fourth input child element of the form
element with attribute named ‘id’ and value ‘loginForm’
These examples cover some basics, but in order to learn more, the following references are
recommended:
There are also a couple of very useful Firefox Add-ons that can assist in discovering the
XPath of an element:
XPath Checker - suggests XPath and can be used to test XPath results.
Firebug - XPath suggestions are just one of the many powerful features of this very
useful add-on.
1 <html>
2 <body>
3 <p>Are you sure you want to do this?</p>
4 <a href="continue.html">Continue</a>
5 <a href="cancel.html">Cancel</a>
6 </body>
7 <html>
link=Continue (4)
link=Cancel (5)
Locating by DOM
The Document Object Model represents an HTML document and can be accessed using
JavaScript. This location strategy takes JavaScript that evaluates to an element on the
page, which can be simply the element’s location using the hierarchical dotted notation.
Since only dom locators start with “document”, it is not necessary to include the dom= label
when specifying a DOM locator.
1 <html>
2 <body>
3 <form id="loginForm">
4 <input name="username" type="text" />
5 <input name="password" type="password" />
6 <input name="continue" type="submit" value="Login" />
7 <input name="continue" type="button" value="Clear" />
8 </form>
9 </body>
10 <html>
dom=document.getElementById('loginForm') (3)
dom=document.forms['loginForm'] (3)
dom=document.forms[0] (3)
document.forms[0].username (4)
document.forms[0].elements['username'] (4)
document.forms[0].elements[0] (4)
document.forms[0].elements[3] (7)
You can use Selenium itself as well as other sites and extensions to explore the DOM of
your web application. A good reference exists on W3Schools.
4 of 10 10/7/2010 12:53 PM
Selenium Commands — Selenium Documentation http://seleniumhq.org/docs/04_selenese_commands.html
Locating by CSS
CSS (Cascading Style Sheets) is a language for describing the rendering of HTML and XML
documents. CSS uses Selectors for binding style properties to elements in the document.
These Selectors can be used by Selenium as another locating strategy.
1 <html>
2 <body>
3 <form id="loginForm">
4 <input class="required" name="username" type="text" />
5 <input class="required passfield" name="password" type="password" />
6 <input name="continue" type="submit" value="Login" />
7 <input name="continue" type="button" value="Clear" />
8 </form>
9 </body>
10 <html>
css=form#loginForm (3)
css=input[name="username"] (4)
css=input.required[type="text"] (4)
css=input.passfield (5)
css=#loginForm input[type="button"] (4)
css=#loginForm input:nth-child(2) (5)
For more information about CSS Selectors, the best place to go is the W3C publication.
You’ll find additional references there.
Note
Most experienced Selenium users recommend CSS as their locating strategy of choice as it’s
considerably faster than XPath and can find the most complicated objects in an intrinsic
HTML document.
Implicit Locators
You can choose to omit the locator type in the following situations:
There are three types of patterns: globbing, regular expressions, and exact.
Globbing Patterns
Most people are familiar with globbing as it is utilized in filename expansion at a DOS or
Unix/Linux command line such as ls *.c. In this case, globbing is used to display all the
files ending with a .c extension that exist in the current directory. Globbing is fairly limited.
Only two special characters are supported in the Selenium implementation:
In most other contexts, globbing includes a third special character, the ?. However,
Selenium globbing patterns only support the asterisk and character class.
To specify a globbing pattern parameter for a Selenese command, you can prefix the
pattern with a glob: label. However, because globbing patterns are the default, you can
also omit the label and specify just the pattern itself.
5 of 10 10/7/2010 12:53 PM
Selenium Commands — Selenium Documentation http://seleniumhq.org/docs/04_selenese_commands.html
Below is an example of two commands that use globbing patterns. The actual link text on
the page being tested was “Film/Television Department”; by using a pattern rather than the
exact text, the click command will work even if the link text is changed to “Film &
Television Department” or “Film and Television Department”. The glob pattern’s asterisk
will match “anything or nothing” between the word “Film” and the word “Television”.
Whereas Selenese globbing patterns support only the * and [ ] (character class) features,
Selenese regular expression patterns offer the same wide array of special characters that
exist in JavaScript. Below are a subset of those special characters:
PATTERN MATCH
. any single character
[] character class: any single character that appears inside the brackets
* quantifier: 0 or more of the preceding character (or group)
+ quantifier: 1 or more of the preceding character (or group)
? quantifier: 0 or 1 of the preceding character (or group)
{1,5} quantifier: 1 through 5 of the preceding character (or group)
| alternation: the character/group on the left or the character/group on the
right
() grouping: often used with alternation and/or quantifier
Regular expression patterns in Selenese need to be prefixed with either regexp: or
regexpi:. The former is case-sensitive; the latter is case-insensitive.
A few examples will help clarify how regular expression patterns can be used with Selenese
commands. The first one uses what is probably the most commonly used regular expression
pattern–.* (“dot star”). This two-character sequence can be translated as “0 or more
occurrences of any character” or more simply, “anything or nothing.” It is the equivalent of
the one-character globbing pattern * (a single asterisk).
The more complex example below tests that the Yahoo! Weather page for Anchorage,
Alaska contains info on the sunrise time:
Exact Patterns
The exact type of Selenium pattern is of marginal usefulness. It uses no special characters
at all. So, if you needed to look for an actual asterisk character (which is special for both
6 of 10 10/7/2010 12:53 PM
Selenium Commands — Selenium Documentation http://seleniumhq.org/docs/04_selenese_commands.html
globbing and regular expression patterns), the exact pattern would be one way to do that.
For example, if you wanted to select an item labeled “Real *” from a dropdown, the
following code might work or it might not. The asterisk in the glob:Real * pattern will
match anything or nothing. So, if there was an earlier select option labeled “Real
Numbers,” it would be the option selected rather than the “Real *” option.
The AndWait alternative is always used when the action causes the browser to navigate to
another page or reload the present one.
Be aware, if you use an AndWait command for an action that does not trigger a
navigation/refresh, your test will fail. This happens because Selenium will reach the AndWait‘s
timeout without seeing any navigation or refresh being made, causing Selenium to raise a
timeout exception.
Selenese, by itself, does not support condition statements (if-else, etc.) or iteration (for, while,
etc.). Many useful tests can be conducted without flow control. However, for a functional test
of dynamic content, possibly involving multiple pages, programming logic is often needed.
1. Run the script using Selenium-RC and a client library such as Java or PHP to utilize the
programming language’s flow control features.
2. Run a small JavaScript snippet from within the script using the storeEval command.
3. Install the goto_sel_ide.js extension.
Most testers will export the test script into a programming language file that uses the
Selenium-RC API (see the Selenium-IDE chapter). However, some organizations prefer to run
their scripts from Selenium-IDE whenever possible (for instance, when they have many
junior-level people running tests for them, or when programming skills are lacking). If this is
your case, consider a JavaScript snippet or the goto_sel_ide.js extension.
The plain store command is the most basic of the many store commands and can be used to
simply store a constant value in a selenium variable. It takes two parameters, the text value
to be stored and a selenium variable. Use the standard variable naming conventions of only
alphanumeric characters when choosing a name for your variable.
7 of 10 10/7/2010 12:53 PM
Selenium Commands — Selenium Documentation http://seleniumhq.org/docs/04_selenese_commands.html
An equivalent store command exists for each verify and assert command. Here are a couple
more commonly used store commands.
storeElementPresent
This corresponds to verifyElementPresent. It simply stores a boolean value–”true” or
“false”–depending on whether the UI element is found.
storeText
StoreText corresponds to verifyText. It uses a locater to identify specific page text. The
text, if found, is stored in the variable. StoreText can be used to extract text from the page
being tested.
storeEval
This command takes a script as its first parameter. Embedding JavaScript within Selenese is
covered in the next section. StoreEval allows the test to store the result of running the
script in a variable.
The example below illustrates how a JavaScript snippet can be used to perform a simple
numerical calculation:
8 of 10 10/7/2010 12:53 PM
Selenium Commands — Selenium Documentation http://seleniumhq.org/docs/04_selenese_commands.html
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <script type="text/javascript">
5 function output(resultText){
6 document.getElementById('output').childNodes[0].nodeValue=resultText;
7 }
8
9 function show_confirm(){
10 var confirmation=confirm("Chose an option.");
11 if (confirmation==true){
12 output("Confirmed.");
13 }
14 else{
15 output("Rejected!");
16 }
17 }
18
19 function show_alert(){
20 alert("I'm blocking!");
21 output("Alert is gone.");
22 }
23 function show_prompt(){
24 var response = prompt("What's the best web QA tool?","Selenium");
25 output(response);
26 }
27 function open_window(windowName){
28 window.open("newWindow.html",windowName);
29 }
30 </script>
31 </head>
32 <body>
33
34 <input type="button" id="btnConfirm" onclick="show_confirm()" value="Show confirm box" />
35 <input type="button" id="btnAlert" onclick="show_alert()" value="Show alert" />
36 <input type="button" id="btnPrompt" onclick="show_prompt()" value="Show prompt" />
37 <a href="newWindow.html" id="lnkNewWindow" target="_blank">New Window Link</a>
38 <input type="button" id="btnNewNamelessWindow" onclick="open_window()" value="Open Nameless Window"
39 <input type="button" id="btnNewNamedWindow" onclick="open_window('Mike')" value="Open Named Window"
40
41 <br />
42 <span id="output">
43 </span>
44 </body>
45 </html>
The user must respond to alert/confirm boxes, as well as moving focus to newly opened popup
windows. Fortunately, Selenium can cover JavaScript pop-ups.
Command Description
assertFoo(pattern) throws error if pattern doesn’t match the text of the pop-up
assertFooPresent throws error if pop-up is not available
assertFooNotPresent throws error if any pop-up is present
storeFoo(variable) stores the text of the pop-up in a variable
storeFooPresent(variable) stores the text of the pop-up in a variable and returns true or false
When running under Selenium, JavaScript pop-ups will not appear. This is because the
9 of 10 10/7/2010 12:53 PM
Selenium Commands — Selenium Documentation http://seleniumhq.org/docs/04_selenese_commands.html
function calls are actually being overridden at runtime by Selenium’s own JavaScript.
However, just because you cannot see the pop-up doesn’t mean you don’t have do deal with it.
To handle a pop-up, you must call it’s assertFoo(pattern) function. If you fail to assert the
presence of a pop-up your next command will be blocked and you will get an error similar to
the following [error] Error: There was an unexpected Confirmation! [Chose an option.]
Alerts
Let’s start with asserts because they are the simplest pop-up to handle. To begin, open the
HTML sample above in a browser and click on the “Show alert” button. You’ll notice that
after you close the alert the text “Alert is gone.” is displayed on the page. Now run through
the same steps with Selenium IDE recording, and verify the text is added after you close
the alert. Your test will look something like this:
If you just want to assert that an alert is present but either don’t know or don’t care what
text it contains, you can use assertAlertPresent. This will return true or false, with false
halting the test.
Confirmations
Confirmations behave in much the same way as alerts, with assertConfirmation and
assertConfirmationPresent offering the same characteristics as their alert counterparts.
However, by default Selenium will select OK when a confirmation pops up. Try recording
clicking on the “Show confirm box” button in the sample page, but click on the “Cancel”
button in the popup, then assert the output text. Your test may look something like this:
You may notice that you cannot replay this test, because Selenium complains that there is
an unhandled confirmation. This is because the order of events Selenium-IDE records
causes the click and chooseCancelOnNextConfirmation to be put in the wrong order (it
makes sense if you think about it, Selenium can’t know that you’re cancelling before you
open a confirmation) Simply switch these two commands and your test will run fine.
10 of 10 10/7/2010 12:53 PM