Selenium
Selenium
Software Quality
Assurance
Telerik Software Academy
http://academy.telerik.com
The Lectors
Snejina Lazarova
Project Manager
BI & Reporting Team
Dimo Mitev
QA Architect
Backend Services Team
2
Table of Contents
Selenium
What is Selenium?
Selenium IDE
Limitations
Features
Test Cases
Test Suits
Commands
Locating elements
Some Final Considerations 3
Selenium
Origins of Selenium
History:
Firstly developed as a JavaScript
library by Thought Works to
automatically rerun tests against
multiple browsers
Selenium is the key mineral to
protect body from mercury toxicity
5
What is Selenium?
What is Selenium?
A JavaScript based web testing tool
Very popular Open Source tool
Supports testing Web 2.0
applications
On multiple browsers
And multiple Operating Systems
Source:
http://seleniumhq.org/
6
What is Selenium? (2)
Tests run directly in browser
Implemented entirely using
browser technologies
JavaScript
DHTML
Frames
7
Selenium’s Tool Suite
Selenium is set of different
software tools
Each has a specific role and a
different approach to supporting
test automation:
Selenium IDE (Integrated
Development Environment)
Selenium 1 (Selenium RC or Remote
Control)
Selenium-Grid
Selenium 2 (Selenium WebDriver)
8
Areas of Application
Selenium can be used for:
Acceptance / Functional testing
Reproducing bugs
Unit-testing
Regression testing
Smoke-testing
9
Selenium
Quick Demo
http://seleniumhq.org/
Selenium IDE
Test Record and
Playback
Selenium IDE
Selenium IDE is a prototyping tool
for building test scripts
It is a Firefox plugin
Provides an easy-to-use interface
for developing automated tests
Selenium IDE has a recording
feature
Records user actions as they are
performed
Exports them as a reusable script in
12
Selenium IDE
Limitations
Selenium IDE is simply intended as
a rapid prototyping tool
Not designed to run test passes
Not designed to build all the
automated tests you would need
Doesn’t provide iteration or
conditional statements for test
scripts
For serious test automation either
Selenium 2 or Selenium 1 should
be used 13
Building Test Cases
There are three primary methods
for developing test cases:
Recording
Adding Verifications and Asserts
With the Context Menu
Editing
Editing commands and comments
Test cases can be grouped in test
suites
14
Recording Test Cases
Useful for beginner users
Selenium-IDE automatically inserts
commands into your test case
based on your actions
Clicking a link - click or
clickAndWait commands
Entering values - type command
Selecting options from a drop-down
listbox - select command
Clicking checkboxes or radio
buttons - click command 15
IDE Main Features
Demo
Menu
Bar Test Case
Toolbar
Pane
Log/
Reference/
UI-Element/
Rollup Pane
16
Base URL
The Selenium IDE allows pointing a
base URL
Allowing test cases to be run across
different domains
17
Selenium Commands –
“Selenese”
Selenium commands, often called
Selenese, are the set of commands
that run your tests
These commands essentially create
a testing language
A sequence of these commands is a
test script
nput[name="username"]
18
What Can We Test
In selenese we can test:
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
Window size, mouse position,
alerts, Ajax functionality, pop up
windows, event handling
Etc. 19
Script Syntax
Selenium commands are simple,
they consist of the command and
two parameters
verifyTex //div//
Login
t a[2]
The parameters are not always
required - it depends on the
command
goBackAndWa
it Some cases require both,toone
Welcome or no
My Home
verifyTextPres id=phone Page
entparameters id=addres (555) 666-7066
type s1 $
type {myVariableAddress}
20
Selenium Parameters
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
21
Storing Selenium
Scripts
Scripts for Selenium-IDE are stored
in HTML text file format
This consists of an HTML table with
three columns:
Command
Target
Value
The second and third columns may
not require values but they should
be present
22
Storing Selenium
Scripts (2)
Here is an example of a test:
<table>
<tr>
<td>open</td>
<td></td>
<td>/download/</td> Rendered as a table in
</tr>
<tr>
a browser this would
<td>assertTitle</td> look like the following:
<td></td>
<td>Downloads</td> open /
</tr> assertTit download/
<tr> le //h2 Download
<td>verifyText</td> verifyTex s
<td>//h2</td> t Download
<td>Downloads</td> s
</tr>
</table>
23
Test Suites
A test suite is a collection of tests
Test suites allow running the tests
all at once, one after another as a
one continuous batch-job
24
Test Suites (2)
Test suites also can be defined using
a simple HTML file
<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> 25
Selenium Commands
A command is what tells Selenium
"what to do"
Selenium commands come in three
“flavors”:
Actions
Accessors
Assertions
26
Actions
Actions are commands that
generally manipulate the state of
the application
They do things like:
“click this link” (click command)
“select that option” (select
command)
Etc.
If an Action fails, or has an error,
the execution of the current test is
27
The "AndWait" Actions
Many Actions can be called with
the “AndWait” suffix,
E.g. “clickAndWait”
When the browser makes a call to
the server
Forces Selenium to wait for a new
page to load
Continuing to run commands before
the page has loaded all its UI
elements causes unexpected test
case failures
28
Accessors
Accessors examine the state of the
application and store the results in
variables
E.g. “storeTitle”
They are also used to automatically
generate Assertions
29
Assertions
Assertions are like Accessors, but
they verify that the state of the
application conforms to what is
expected
E.g., “make sure the page title is X”
(assertTitle) and “verify that this
checkbox is checked” (verifyText)
30
Assertion Modes
All Selenium Assertions can be
used in 3 modes:
“assert”
When fails the test is aborted
“verify”
When fails, the test continues
execution, logging the failure
”waitFor”
Waits for some condition to become
true
31
Assertion or
Verification?
Choosing between “assert” and
“verify” comes down to
convenience and management of
failures
There is no point checking a
paragraph if you are not on the
correct page
On the other hand, you may want to
check many attributes of a page
without aborting the test case
Usually each command group is
32
Some Common
Selenium Commands
These are some of the most
commonly used commands:
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
33
Some Common
Selenium Commands
(2)
verifyElementPresent - verifies an
expected UI element, as defined by
its HTML tag, is present on the page
verifyText - verifies expected text
and it’s corresponding HTML tag are
present on the page
verifyTable - verifies a table’s
expected contents
34
Some Common
Selenium Commands
(3)
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
35
Verifying Page
Elements
Verifying UI elements on a web
page is probably the most common
feature
Selenese allows multiple ways of
checking for UI present
Is an element elements. E.g.:
somewhere on the
page?
Is a specific text -
somewhere on the
page?
Is a specific text at a 36
"verifyTextPresent"
The command verifyTextPresent
Used to verify specific text exists
somewhere on the page
It takes a single argument - the text
pattern to be verified
Command Target Value
verifyTextPres Marketing
ent Analysis
37
"verifyElementPresent"
The command
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
verifyElementPre //div/p/img
sent 38
"verifyText"
The command verifyText
Verifies both the text and its UI
element
It must use a locator
39
Locating Elements
For many Selenium commands, a
target is required
Identifies an element in the content
of the web application
Consists of the location strategy
followed by the location in the
format:
locatorType=location
The locator type can be omitted
in many cases
40
Locating by Identifier
Locating by identifier is the most
common method of locating
elements
A catch-all default when no
recognized locator type is used
The first element with the id
attribute value matching the
location is used
If no element has a matching id
attribute, then the first element
with a name attribute matching the
41
Locating by Identifier -
Source code: Example
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>
Locator strategy Row of
returned
Since identifier= element
is the default identifier=login 3
locator, it can be Form
ommited identifier=pass 5
word
42
identifier=conti 6
Locating by Id
Locating by Id is more limited than
the identifier locator type, but also
more explicit
Use it when you know an element's
id attribute
43
Locating by Id -
Example
Source code:
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>
Locator strategy Row of
returned
element
id=loginForm 3
44
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)
45
Locating by Name -
Example
Source code:
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> Locator strategy Row of returned
value= is element
omitted name=username 4
as a name=continue 7
default value=Clear
filter name=continue Clear 7
name=continue 7 46
Locating by XPath
XPath is the language used for
locating nodes in an XML (XHTML)
document
Useful when we don’t have a
suitable id or name attribute for the
element
XPath locators can also be used to
specify elements via attributes
other than id and name
t h =
xp a
Since only xpath locators start with
“//”, the xpath= label can be
47
Absolute vs. Relative
Location
Types of XPath location:
Absolute
Contains the location of all elements
from the root (html) (E.g.,
xpath=/html/body/form)
Very likely to fail after adjustments
Relative
Relative to an element that does
have an id or name attribute
E.g., //input[@name=’username’] -
First input element with attribute
48
Locating by Xpath -
1 <html>
Example
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>
10Locator
<html> strategy Row of returned
element
xpath=/html/body/form[1] 3
//form[1] 3
xpath=//form[@id=’loginForm’] 3
xpath=//form[input/\ 4
@name=’username’]
49
Locating by Xpath –
1 <html>
Example (2)
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>
10Locator
<html> strategy Row of returned
element
//input[@name=’username’] 4
//form[@id=’loginForm’]/input[1] 4
//input[@name=’continue’] 7
[@type=’button’]
//form[@id=’loginForm’]/input[4] 7
50
Resources for XPath
Some useful links:
http://www.w3schools.com/Xpath/
http://www.w3.org/TR/xpath/
http://
www.zvon.org/xxl/XPathTutorial/Gen
eral/examples.html
Useful Firefox Add-on that can
assist in discovering the XPath of
an element:
https://addons.mozilla.org/en-US/fir
efox/addon/firebug/ 51
Locating Hyperlinks by
Link Text
Hyperlink can be located in the web
page by using the text of the link
If two links with the same text are
present, then the first match will be
used
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>
55
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
Faster than Xpath 56
Locating by CSS -
1 <html>
Example
2 <body>
3 <form id= "loginForm" >
4 <input class= "required" name= "username" type=
"text"/>
5 <input class= "required passfield" name= "password"
57
Locating by CSS – Example
1 <html>
(2)
2 <body>
3 <form id= "loginForm" >
4 <input class= "required" name= "username" type=
"text"/>
5 <input class= "required passfield" name= "password"
61
Globbing Patterns
Selenium globbing patterns
support only two special
characters:
"*" (asterisk) - translates to “match
anything,” i.e., nothing, a single
character, or many characters
[ ] (character class) - translates to
“match any single character found
inside the square brackets” – e.g.,
[aeiou]
A dash (hyphen) can be used as a
62
Globbing Patterns
Prefix
Globbing patterns are prefixed
with a glob: label
Since globbing patterns are the
default pattern – the label can be
omitted
Comma Target Value
nd
verifyTi glob:Film*Television*
tle
67
Exact Patterns
Exact patterns use no special
characters
They are used with exact: prefix
Used when a special character need
to be used as a literal
Example: looking for an item
Matches anything or
//
sele
labeled
selec"Real *" *
glob:Real nothing after "Real"
ct
t
//
sele exact:Real Matches the exact
selec
ct * pattern
t
// Escaping is also
sele regexp:Re
selec
ct al \* possible 68
"waitFor" Commands
for AJAX Applications
Using andWait commands will not
work In AJAX driven web
applications
Data is retrieved from server
without refreshing the page
We can use waitFor,
waitForElementPresent or
waitForVisible commands
They check for the desired
condition every second and
continue as soon as the condition is 69
Store Commands and
Selenium Variables
Selenium variables can be used to
store constants at the beginning of
a script
Can be used to store values passed
to your test program from the
command-line, from another
program, or from a file
Comma Target Value
Storing
nd values with the store
command
store [email protected] userName
g
The text value to be The selenium variable
stored 70
Using The Stored
Values
Accessing the stored value
Done by enclosing the variable in
curly brackets ({}) and preceded by
a dollar sign - ${ … }
Comma Target Value
nd
verifyTe //div/p $
xt {userName
A common use of variables
} is for
storing input for an input field
Comma Target Value
nd
type id=login $
{userName
} 71
Other Store Commands
storeElementPresent
Corresponds to
verifyElementPresent
Stores a boolean value–“true” or
“false”–depending on whether the
UI element is found
storeText
Corresponds to verifyText
Uses a locater to identify specific
page text - if found, it is stored in
the variable 72
Other Store Commands
(2)
storeEval
Takes a script as its first parameter
Allows the test to store the result of
running the script in a variable
An equivalent store command
exists for each verify and assert
command
73
JavaScript Usage with
Script Parameters
Several Selenese commands
specify a script parameter
assertEval, verifyEval, storeEval,
and waitForEval
A snippet of JavaScript code can is
simply put into the appropriate field
Command Target Value
store 10 hits
storeXpathC //blockquote blockquo
ount tes
storeEval storedVars[’hits’]- paragrap
storedVars[’blockquotes’] hs
74
Methods Calling With
JavaScript
A JavaScript snippet can include
calls to methods
E.g., JavaScript String object’s
toUpperCase method and
toLowerCase method:
Comma Target Valu
nd e
store Edith Wharton hits
storeEv storedVars[’name’].toUpp uc
al erCase()
storeEv storedVars[’name’].toLow lc
al erCase()
75
Using JavaScript With
Non-Script Parameters
JavaScript can also be used even
when the parameter is not
specified to be of type script
The JavaScript snippet must be
enclosed inside curly braces
preceded by the label javascript
javascript{*yourCodeHere*}
Comma Target Value
nd
store league searchString
of
nations
type q javascript{storedVars[’searchString’].t
oUpperCase()}
76
echo - The Selenese
Print Command
Selenese has a simple command
that allows printing text to the
test’s output
Useful for providing informational
progress notes in your test
Displays on the console as the test
is running
Comma Target Valu
nd e
Echo Testing page footer
now.
Echo Username is $
{userName}
77
Debugging
Selenium supports some useful
debugging features
Breakpoints and startpoints
Stepping through a testcase
Executing a test case one command
at a time
Find button
Seeing which UI element on the
currently displayed webpage (in the
browser) is used in the currently
selected Selenium command 78
Sequence of Evaluation
and Flow Control
Selenium scripts runs in sequence,
one command after another
Selenese, by itself, does not
support condition statements (if-
else, etc.) or iteration (for, while,
etc.)
79
Sequence of Evaluation
and Flow Control (2)
When flow control is needed, there
are a couple of options:
Run the script using Selenium-RC
and a client library such as C# or
PHP
Run a small JavaScript snippet from
within the script using the storeEval
command
Install the goto_sel_ide.js extension
Install Selenium IDE Flow Control
80
How to Add User
Extensions
Open
Selenium
Go to Menu
Options/Optio
ns
Browse your
extension
and select it
in Selenium
Core
extensions or
Selenium IDE
extension 81
Flow Control Extension
When do we use it:
some steps of the test should be
skipped if condition is satisfied
If we’re logged in, we can skip log in
steps
If we need to add iterations
Command
while loopTarget
can be used
Value as a command
store 0 loop1
while storedVars.loop
1<2
…
endWhile 82
Flow Control Extension
(2)
Adding conditions to the tests
Before you start: add goto_sel_ide.js
to Selenium as user extension
Labels in the tests
Used as markers in the tests
label labelName
gotoif command
gotoif storedVars[‘Name']
labelName
==1
83
Some Final
Considerations
84
Problems With HTML
Tests
Selenium is sensitive to the format
of the table
Duplication is a major issue
Tests need to be deployed with AUT
(Application Under Test)
85
Table or Driver Based?
Table based approach is fine for
simple tests
No programming required
Doesn't scale – duplication is a
major issue
Driver approach better for 'Real'
test suites
Can develop tests in language of
choice:
C#, Java, Ruby, Python, Perl
86
Ajax Support
Selenium supports testing Web 2.0
applications
Monitor the DOM for completion of
Async calls
waitForCondition()
waitForValue()
87
Continuous Integration
Run Selenium tests as part of the
build
Works with both Core and Driven
modes
Each time a developer checks in, if
necessary
Can generate HTML reports,
published to entire team
Helps catch bugs ASAP
Addresses risk of catching bugs late
in the cycle 88
Selenium
?
?
?
Questions
?
?
?
?
?
? ?
Exercises
1.Using the Selenium IDE record a test
case that performs the following
actions:
Opens the Telerik Academy web page:
http://academy.telerik.com/
Navigates to the Start page
Ensures that the text “Предстоящи
курсове” is present on the page
90
Exercises (2)
2.Using the Selenium IDE record a test
case that performs the following
actions:
Navigates to the site: http://
www.worldwidemetric.com/cal.html
Asserts that the header "Length
Conversion Calculator" is present on
the page
Inserts value 10 in the Meter field
Presses the respective "Calculate"
button
Asserts that the Centimeters field gets 91
Exercises (3)
3.Using the Selenium IDE record a test
case that performs the following
actions:
Navigates to http://www.google.com in
Firefox
Types “Testing Geek” in the Google
search bar and click Search
On the search results verify that
“Software Testing Geek” is present at
the first place
Save the test case as google_ide.html
Run the saved test case back again 92
Exercises (4)
4.Read the Selenium Documentation at
address:
http://docs.seleniumhq.org/docs/
5.Find more information about Selenium
in the Internet
93
Free Trainings @ Telerik
Academy
C# Programming @ Telerik
Academy
csharpfundamentals.telerik.com
Telerik Software Academy
academy.telerik.com
Telerik Academy @ Facebook
facebook.com/TelerikAcademy
Telerik Software Academy Forums
forums.academy.telerik.com