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.
Selenium provides a rich set of commands for
fully testing your web-app in virtually any way you can imagine. The command
set is often called selenese. These commands essentially create a testing
language.
In selenese, one can test the existence of UI
elements based on their HTML tags, test for specific content, test for broken
links, input fields, selection list options, submitting forms, and table data
among other things. In addition Selenium commands support testing of window
size, mouse position, alerts, Ajax functionality, pop up windows, event handling,
and many other web-application features. The Command Reference lists all the
available commands.
A command is what tells Selenium what to do. Selenium
commands come in three “flavors”: Actions,
Accessors,
and Assertions.
Actions
are commands that generally manipulate the state of the application. They do
things like “click this link” and “select that option”. If an Action fails, or
has an error, the execution of the current test is stopped.
Many Actions can be called with the “AndWait”
suffix, e.g. “clickAndWait”. This suffix tells Selenium that the action will
cause the browser to make a call to the server, and that Selenium should wait
for a new page to load.
Accessors
examine the state of the application and store the results in variables, e.g.
“storeTitle”. They are also used to automatically generate Assertions.
Assertions
are like Accessors, but they verify that the state of the application conforms
to what is expected. Examples include “make sure the page title is X” and
“verify that this checkbox is checked”.
All Selenium Assertions can be used in 3
modes: “assert”, “verify”, and ” waitFor”. For example, you can “assertText”,
“verifyText” and “waitForText”. When an “assert” fails, the test is aborted.
When a “verify” fails, the test will continue execution, logging the failure.
This allows a single “assert” to ensure that the application is on the correct
page, followed by a bunch of “verify” assertions to test form field values,
labels, etc.
“waitFor” commands wait for some condition to
become true (which can be useful for testing Ajax applications). They will
succeed immediately if the condition is already true. However, they will fail
and halt the test if the condition does not become true within the current
timeout setting (see the setTimeout action below).
Script Syntax
Selenium commands are simple, they consist of
the command and two parameters. For example:
verifyText
|
//div//a[2]
|
Login
|
The parameters are not always required; it
depends on the command. In some cases both are required, in others one
parameter is required, and in still others the command may take no parameters
at all. Here are a couple more examples:
goBackAndWait
|
|
|
verifyTextPresent
|
|
Welcome to My Home Page
|
type
|
id=phone
|
(555) 666-7066
|
type
|
id=address1
|
${myVariableAddress}
|
The command reference describes the parameter
requirements for each command.
Parameters vary, however they are typically:
a locator for identifying a UI element within a page.
a text pattern for verifying or asserting expected page
content
a text pattern or a selenium variable for entering text
in an input field or for selecting an option from an option list.
Locators, text patterns, selenium variables,
and the commands themselves are described in considerable detail in the section
on Selenium Commands.
Selenium scripts that will be run from
Selenium-IDE will be be stored in an HTML text file format. This consists of an
HTML table with three columns. The first column identifies the Selenium command,
the second is a target, and the final column contains a value. The second and
third columns may not require values depending on the chosen Selenium command,
but they should be present. Each table row represents a new Selenium command.
Here is an example of a test that opens a page, asserts the page title and then
verifies some content on the page:
<table>
<tr><td>open</td><td>/download/</td><td></td></tr>
<tr><td>assertTitle</td><td></td><td>Downloads</td></tr>
<tr><td>verifyText</td><td>//h2</td><td>Downloads</td></tr>
</table>
Rendered as a table in a browser this would
look like the following:
open
|
/download/
|
|
assertTitle
|
|
Downloads
|
verifyText
|
//h2
|
Downloads
|
The Selenese HTML syntax can be used to write
and run tests without requiring knowledge of a programming language. With a
basic knowledge of selenese and Selenium-IDE you can quickly produce and run
testcases.
Test Suites
A test suite is a collection of tests. Often
one will run all the tests in a test suite as one continuous batch-job.
When using Selenium-IDE, test suites also can
be defined using a simple HTML file. The syntax again is simple. An HTML table
defines a list of tests where each row defines the filesystem path to each
test. An example tells it all.
<html>
<head>
<title>Test Suite Function Tests - Priority
1</title>
</head>
<body>
<table>
<tr><td><b>Suite
Of Tests</b></td></tr>
<tr><td><a
href="./Login.html">Login</a></td></tr>
<tr><td><a
href="./SearchValues.html">Test Searching for Values</a></td></tr>
<tr><td><a
href="./SaveValues.html">Test Save</a></td></tr>
</table>
</body>
</html>
A file similar to this would allow running the
tests all at once, one after another, from the Selenium-IDE.
Test suites can also be maintained when using
Selenium-RC. This is done via programming and can be done a number of ways.
Commonly Junit is used to maintain a test suite if one is using Selenium-RC
with Java. Additionally, if C# is the chosen language, Nunit could be employed.
If using an interpreted language like Python with Selenium-RC than some simple
programming would be involved in setting up a test suite. Since the whole
reason for using Selenium-RC is to make use of programming logic for your
testing this usually isn’t a problem.
Commonly Used Selenium Commands
To conclude our introduction of Selenium,
we’ll show you a few typical Selenium commands. These are probably the most
commonly used commands for building tests.
open
opens a page using a URL.
click/clickAndWait
performs a click operation, and optionally waits for a new page to load.
verifyTitle/assertTitle
verifies an expected page title.
verifyTextPresent
verifies expected text is somewhere on the page.
verifyElementPresent
verifies an expected UI element, as defined by its HTML tag, is present on
the page.
verifyText
verifies expected text and its corresponding HTML tag are present on the
page.
verifyTable
verifies a table’s expected contents.
waitForPageToLoad
pauses execution until an expected new page loads. Called automatically
when clickAndWait is used.
waitForElementPresent
pauses execution until an expected UI element, as defined by its HTML tag,
is present on the page.
Verifying Page Elements
Verifying UI elements on a web page is probably the most common feature of
your automated tests. Selenese allows multiple ways of checking for UI
elements. It is important that you understand these different methods because
these methods define what you are actually testing.
For example, will you test that...
an element is present somewhere on the page?
specific text is somewhere on the page?
specific text is at a
specific location on the page?
For example, if you are testing a text
heading, the text and its position at the top of the page are probably relevant
for your test. If, however, you are testing for the existence of an image on
the home page, and the web designers frequently change the specific image file
along with its position on the page, then you only want to test that an image
(as opposed to the specific image file) exists somewhere on the page.
Assertion or
Verification?
Choosing between “assert” and “verify” comes
down to convenience and management of failures. There’s very little point
checking that the first paragraph on the page is the correct one if your test
has already failed when checking that the browser is displaying the expected
page. If you’re not on the correct page, you’ll probably want to abort your
test case so that you can investigate the cause and fix the issue(s) promptly.
On the other hand, you may want to check many attributes of a page without
aborting the test case on the first failure as this will allow you to review
all failures on the page and take the appropriate action. Effectively an
“assert” will fail the test and abort the current test case, whereas a “verify”
will fail the test and continue to run the test case.
The best use of this feature is to logically
group your test commands, and start each group with an “assert” followed by one
or more “verify” test commands. An example follows:
Command
|
Target
|
Value
|
open
|
/download/
|
|
assertTitle
|
Downloads
|
|
verifyText
|
//h2
|
Downloads
|
assertTable
|
1.2.1
|
Selenium IDE
|
verifyTable
|
1.2.2
|
June 3, 2008
|
verifyTable
|
1.2.3
|
1.0 beta 2
|
The above example first opens a page and then
“asserts” that the correct page is loaded by comparing the title with the
expected value. Only if this passes will the following command run and “verify”
that the text is present in the expected location. The test case then “asserts”
the first column in the second row of the first table contains the expected
value, and only if this passed will the remaining cells in that row be
“verified”.
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:
Command
|
Target
|
Value
|
verifyTextPresent
|
Marketing Analysis
|
|
This would cause Selenium to search for, and
verify, that the text string “Marketing Analysis” appears somewhere on the page
currently being tested. Use verifyTextPresent when you
are interested in only the text itself being present on the page. Do not use
this when you also need to test where the text occurs on the page.
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.
Command
|
Target
|
Value
|
verifyElementPresent
|
//div/p/img
|
|
This command verifies that an image, specified
by the existence of an <img> HTML tag, is present on the page, and that
it follows a <div> tag and a <p> tag. The first (and only)
parameter is a locator
for telling the Selenese command how to find the element. Locators are
explained in the next section.
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.
Command
|
Target
|
Value
|
verifyElementPresent
|
//div/p
|
|
verifyElementPresent
|
//div/a
|
|
verifyElementPresent
|
id=Login
|
|
verifyElementPresent
|
link=Go to Marketing Research
|
|
verifyElementPresent
|
//a[2]
|
|
verifyElementPresent
|
//head/title
|
|
These examples illustrate the variety of ways
a UI element may be tested. Again, locators are explained in the next section.
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.
Command
|
Target
|
Value
|
verifyText
|
//table/tr/td/div/p
|
This is my text and it occurs right after the div inside
the table.
|
No comments:
Post a Comment