Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Selenium
1. Introduction
Locators
Waiting
Examples
Other
Selenium
Andrew Johnstone
November 13, 2011
Andrew Johnstone Selenium
2. Introduction
Locators
Waiting
Examples
Other
Outline
1 Introduction
2 Locators
3 Waiting
4 Examples
5 Other
Andrew Johnstone Selenium
3. Introduction
Locators
Waiting
Examples
Other
Outline
1 Introduction
2 Locators
3 Waiting
4 Examples
5 Other
Andrew Johnstone Selenium
4. Introduction
Locators
Waiting
Examples
Other
Outline
1 Introduction
2 Locators
3 Waiting
4 Examples
5 Other
Andrew Johnstone Selenium
5. Introduction
Locators
Waiting
Examples
Other
Outline
1 Introduction
2 Locators
3 Waiting
4 Examples
5 Other
Andrew Johnstone Selenium
6. Introduction
Locators
Waiting
Examples
Other
Outline
1 Introduction
2 Locators
3 Waiting
4 Examples
5 Other
Andrew Johnstone Selenium
7. Introduction
Locators
Waiting
Examples
Other
What is functional testing
Functional testing is a type of black box testing that bases its
test cases on the specifications of the software component
under test.
Functional testing typically involves five steps
The identification of functions that the software is expected
to perform
The creation of input data based on the function’s
specifications
The determination of output based on the function’s
specifications
The execution of the test case
The comparison of actual and expected outputs
Andrew Johnstone Selenium
8. Introduction
Locators
Waiting
Examples
Other
What is functional testing
Functional testing is a type of black box testing that bases its
test cases on the specifications of the software component
under test.
Functional testing typically involves five steps
The identification of functions that the software is expected
to perform
The creation of input data based on the function’s
specifications
The determination of output based on the function’s
specifications
The execution of the test case
The comparison of actual and expected outputs
Andrew Johnstone Selenium
9. Introduction
Locators
Waiting
Examples
Other
What is functional testing
Functional testing is a type of black box testing that bases its
test cases on the specifications of the software component
under test.
Functional testing typically involves five steps
The identification of functions that the software is expected
to perform
The creation of input data based on the function’s
specifications
The determination of output based on the function’s
specifications
The execution of the test case
The comparison of actual and expected outputs
Andrew Johnstone Selenium
10. Introduction
Locators
Waiting
Examples
Other
What is functional testing
Functional testing is a type of black box testing that bases its
test cases on the specifications of the software component
under test.
Functional testing typically involves five steps
The identification of functions that the software is expected
to perform
The creation of input data based on the function’s
specifications
The determination of output based on the function’s
specifications
The execution of the test case
The comparison of actual and expected outputs
Andrew Johnstone Selenium
11. Introduction
Locators
Waiting
Examples
Other
What is functional testing
Functional testing is a type of black box testing that bases its
test cases on the specifications of the software component
under test.
Functional testing typically involves five steps
The identification of functions that the software is expected
to perform
The creation of input data based on the function’s
specifications
The determination of output based on the function’s
specifications
The execution of the test case
The comparison of actual and expected outputs
Andrew Johnstone Selenium
12. Introduction
Locators
Waiting
Examples
Other
What is functional testing
Functional testing is a type of black box testing that bases its
test cases on the specifications of the software component
under test.
Functional testing typically involves five steps
The identification of functions that the software is expected
to perform
The creation of input data based on the function’s
specifications
The determination of output based on the function’s
specifications
The execution of the test case
The comparison of actual and expected outputs
Andrew Johnstone Selenium
13. Introduction
Locators
Waiting
Examples
Other
What is functional testing
Functional testing is a type of black box testing that bases its
test cases on the specifications of the software component
under test.
Functional testing typically involves five steps
The identification of functions that the software is expected
to perform
The creation of input data based on the function’s
specifications
The determination of output based on the function’s
specifications
The execution of the test case
The comparison of actual and expected outputs
Andrew Johnstone Selenium
14. Introduction
Locators
Waiting
Examples
Other
Selenium
Selenium is a tool to automate browsers.
It consists of four projects
Selenium IDE - Firefox add-on that makes it easy to record
and playback tests
Selenium Core - Runs the tests directly in the browser
Selenium RC - Allows you to control web browsers locally
or on other computers
Selenium Grid - Runs tests on many servers at the same
time
Andrew Johnstone Selenium
15. Introduction
Locators
Waiting
Examples
Other
Selenium
Selenium is a tool to automate browsers.
It consists of four projects
Selenium IDE - Firefox add-on that makes it easy to record
and playback tests
Selenium Core - Runs the tests directly in the browser
Selenium RC - Allows you to control web browsers locally
or on other computers
Selenium Grid - Runs tests on many servers at the same
time
Andrew Johnstone Selenium
16. Introduction
Locators
Waiting
Examples
Other
Selenium
Selenium is a tool to automate browsers.
It consists of four projects
Selenium IDE - Firefox add-on that makes it easy to record
and playback tests
Selenium Core - Runs the tests directly in the browser
Selenium RC - Allows you to control web browsers locally
or on other computers
Selenium Grid - Runs tests on many servers at the same
time
Andrew Johnstone Selenium
17. Introduction
Locators
Waiting
Examples
Other
Selenium
Selenium is a tool to automate browsers.
It consists of four projects
Selenium IDE - Firefox add-on that makes it easy to record
and playback tests
Selenium Core - Runs the tests directly in the browser
Selenium RC - Allows you to control web browsers locally
or on other computers
Selenium Grid - Runs tests on many servers at the same
time
Andrew Johnstone Selenium
18. Introduction
Locators
Waiting
Examples
Other
Why Selenium
Selenium - functional testing for web and mobile based
applications. (Drivers NOW exist for mobiles/tablets)
Multi browser support
Write test cases in many languages.
Simply record interactions with a website from Firefox
using an IDE
Tests are repeatable
Difficulties/issues with Selenium
Handling ajax request/responses on pages
IE test execution with locators
Flash/Flex automation
Rich text editors (CKEditor)
Andrew Johnstone Selenium
19. Introduction
Locators
Waiting
Examples
Other
Locators
identifier=id - Locates @id and falls back to @name
id=id - Locates @id only
name=name - Locates @name only
dom=javascriptExpression -
document.getElementById(’el’);
xpath=xpathExpression
link=textPattern
css=cssSelectorSyntax
ui=uiSpecifierString - A map of aliases for common
elements. Known as a GUI Map
Andrew Johnstone Selenium
20. Introduction
Locators
Waiting
Examples
Other
Locators
identifier=id - Locates @id and falls back to @name
id=id - Locates @id only
name=name - Locates @name only
dom=javascriptExpression -
document.getElementById(’el’);
xpath=xpathExpression
link=textPattern
css=cssSelectorSyntax
ui=uiSpecifierString - A map of aliases for common
elements. Known as a GUI Map
Andrew Johnstone Selenium
21. Introduction
Locators
Waiting
Examples
Other
Locators
identifier=id - Locates @id and falls back to @name
id=id - Locates @id only
name=name - Locates @name only
dom=javascriptExpression -
document.getElementById(’el’);
xpath=xpathExpression
link=textPattern
css=cssSelectorSyntax
ui=uiSpecifierString - A map of aliases for common
elements. Known as a GUI Map
Andrew Johnstone Selenium
22. Introduction
Locators
Waiting
Examples
Other
Locators
identifier=id - Locates @id and falls back to @name
id=id - Locates @id only
name=name - Locates @name only
dom=javascriptExpression -
document.getElementById(’el’);
xpath=xpathExpression
link=textPattern
css=cssSelectorSyntax
ui=uiSpecifierString - A map of aliases for common
elements. Known as a GUI Map
Andrew Johnstone Selenium
23. Introduction
Locators
Waiting
Examples
Other
Locators
identifier=id - Locates @id and falls back to @name
id=id - Locates @id only
name=name - Locates @name only
dom=javascriptExpression -
document.getElementById(’el’);
xpath=xpathExpression
link=textPattern
css=cssSelectorSyntax
ui=uiSpecifierString - A map of aliases for common
elements. Known as a GUI Map
Andrew Johnstone Selenium
24. Introduction
Locators
Waiting
Examples
Other
Locators
identifier=id - Locates @id and falls back to @name
id=id - Locates @id only
name=name - Locates @name only
dom=javascriptExpression -
document.getElementById(’el’);
xpath=xpathExpression
link=textPattern
css=cssSelectorSyntax
ui=uiSpecifierString - A map of aliases for common
elements. Known as a GUI Map
Andrew Johnstone Selenium
25. Introduction
Locators
Waiting
Examples
Other
Locators
identifier=id - Locates @id and falls back to @name
id=id - Locates @id only
name=name - Locates @name only
dom=javascriptExpression -
document.getElementById(’el’);
xpath=xpathExpression
link=textPattern
css=cssSelectorSyntax
ui=uiSpecifierString - A map of aliases for common
elements. Known as a GUI Map
Andrew Johnstone Selenium
26. Introduction
Locators
Waiting
Examples
Other
Locators
identifier=id - Locates @id and falls back to @name
id=id - Locates @id only
name=name - Locates @name only
dom=javascriptExpression -
document.getElementById(’el’);
xpath=xpathExpression
link=textPattern
css=cssSelectorSyntax
ui=uiSpecifierString - A map of aliases for common
elements. Known as a GUI Map
Andrew Johnstone Selenium
27. Introduction
Locators
Waiting
Examples
Other
Locators
identifier=id - Locates @id and falls back to @name
id=id - Locates @id only
name=name - Locates @name only
dom=javascriptExpression -
document.getElementById(’el’);
xpath=xpathExpression
link=textPattern
css=cssSelectorSyntax
ui=uiSpecifierString - A map of aliases for common
elements. Known as a GUI Map
Andrew Johnstone Selenium
28. Introduction
Locators
Waiting
Examples
Other
Locators - Xpath/CSS
XPATH is SLOW on IE as such use CSS selectors for locating
simple elements.
They’re faster
They’re more readable
CSS is jQuery’s locating strategy
No one else uses XPATH anyway
Performance comparison of CSS vs XPATH
Andrew Johnstone Selenium
29. Introduction
Locators
Waiting
Examples
Other
Locators CSS - Direct child
A direct child in XPATH is defined by the use of a "/", while on
CSS, it’s defined using ">"
Xpath
//div/a
CSS
css=div > a
Andrew Johnstone Selenium
30. Introduction
Locators
Waiting
Examples
Other
Locators CSS - Child or subchild
If an element could be inside another or one it’s childs, it’s
defined in XPATH using "//" and in CSS just by a whitespace
Xpath
//div/a
CSS
div a
Andrew Johnstone Selenium
31. Introduction
Locators
Waiting
Examples
Other
Locators CSS - Id
An element’s id in XPATH is defined using: "[@id=’example’]"
and in CSS using: "#"
Xpath
//div[@id=’example’]//a
CSS
css=div#example a
Andrew Johnstone Selenium
32. Introduction
Locators
Waiting
Examples
Other
Locators CSS - Class
For class, things are pretty similar in XPATH:
"[@class=’example’]" while in CSS it’s just "."
Xpath
//div[@class=’example’]//a
CSS
css=div.example a
Andrew Johnstone Selenium
33. Introduction
Locators
Waiting
Examples
Other
Locators CSS - More examples
Not all CSS 3 selectors are supported by Selenium 2.
Next sibling - css=form input.username + input
Attribute values - css=form input[name=’username’]
Attribute values - css=input[name=’continue’][type=’button’]
Matching by text - css=a:contains(’Log Out’)
Match li by nth child - css=ul#recordlist li:nth-of-type(4)
Andrew Johnstone Selenium
34. Introduction
Locators
Waiting
Examples
Other
Locators XPATH - Examples
XPATH Selectors can easily become horrible and
unmanageable
//div[text() = "Click here"] //div[contains(text(), "Click here")]
xpath=//label[text()=’Full Name:’]/following-sibling::input
//nobr[contains(text(), ’Message Sent’)]
//div[contains(@class, ’x-combo-list’) and contains(@style,
’visibility: visible’)]
Andrew Johnstone Selenium
35. Introduction
Locators
Waiting
Examples
Other
Locators XPATH - The Good, the bad and the ugly
An example of a bad XPath - it will break as soon as the page
layout changes
/html/body/div[1]/div[5]/div/table/tbody/tr/td/p/a[3]
An un-named input box that has a known label 2 table cells away
from it
//td[contains(text()),’My Label’]/following-sibling::td[2]/input
Andrew Johnstone Selenium
36. Introduction
Locators
Waiting
Examples
Other
Waiting...
WaitForElementPresent | css=h2:contains(’Manta’) | 60000
This waits for the title "Manta" to appear.
If your app fails and the title’s text loaded is incorrect?
It will wait for the time out to be exceeded before failing.
A better way...
WaitForElementPresent | css=h2.titles_class | 60000
assertText css=h2.titles_class | Manta
All wait commands will use a delay within a loop until a
condition is met.
Andrew Johnstone Selenium
37. Introduction
Locators
Waiting
Examples
Other
Waiting...
WaitForElementPresent | css=h2:contains(’Manta’) | 60000
This waits for the title "Manta" to appear.
If your app fails and the title’s text loaded is incorrect?
It will wait for the time out to be exceeded before failing.
A better way...
WaitForElementPresent | css=h2.titles_class | 60000
assertText css=h2.titles_class | Manta
All wait commands will use a delay within a loop until a
condition is met.
Andrew Johnstone Selenium
38. Introduction
Locators
Waiting
Examples
Other
Waiting...
Selenium 2.0 - Use implicit waits over explicit waits
Explicit Waits
An explicit waits is code you define to wait for a certain
condition to occur before proceeding further in the code.
There are some convenience methods provided that help you
write code that will wait only as long as required.
WebDriverWait in combination with ExpectedCondition is one
way this can be accomplished.
Implicit Waits
An implicit wait is to tell WebDriver to poll the DOM for a
certain amount of time when trying to find an element or
elements if they are not immediately available.
The default setting is 0.
Andrew Johnstone Selenium
39. Introduction
Locators
Waiting
Examples
Other
saucelabs
Selenium RC in the cloud with support for all major browsers
Sauceconnect allows access to internal firewalled infrastructure
from saucelabs
Andrew Johnstone Selenium
41. Introduction
Locators
Waiting
Examples
Other
Example Selenium test case with PHPUnit
<?php
r e q u i r e _ o n c e ’ PHPUnit / E x te n s i o ns / SeleniumTestCase / ’ .
’ SauceOnDemandTestCase . php ’ ;
c l a s s ExampleTest
extends PHPUnit_Extensions_SeleniumTestCase_SauceOnDemandTestCase {
f u n c t i o n setUp ( ) {
$ t h i s −>setOs ( ’ Windows 2003 ’ ) ;
$ t h i s −>setBrowser ( ’ f i r e f o x ’ ) ;
$ t h i s −>se tB ro ws er Ve rs io n ( ’ 3 . 6 . ’ ) ;
$ t h i s −>s e t B r o w s e r U r l ( ’ h t t p : / / example . saucelabs . com ’ ) ;
}
f u n c t i o n test_example ( ) {
$ t h i s −>open ( ’ / ’ ) ;
$ t h i s −>a s s e r t T i t l e ( ’ Cross browser t e s t i n g w i t h Selenium − Sauce Labs ’ ) ;
}
}
Andrew Johnstone Selenium
42. Introduction
Locators
Waiting
Examples
Other
Example Selenium test case with PHPUnit
Example running against 3 different browsers.
Tests can be parallelized to speed execution.
Andrew Johnstone Selenium
43. Introduction
Locators
Waiting
Examples
Other
Selenium versions
Selenium 1
Recommended timeouts 90000ms - the default is set to
selenium.setTimeout(30000);
Generate application states with test handles.
As an example if a login fails certain paths of execution
will not be acheivable, as such skip them.
Ignore Open and waitForPageToLoad failures -
common failures are css stylesheets not loading properly.
In particular onIE6 + IE7 will cause random failures
Selenium 2
Ignoring Open and waitForPageToLoad failures
Andrew Johnstone Selenium
44. Introduction
Locators
Waiting
Examples
Other
Structuring tests...
Always use Annotations to indicate the group of the tests to
perform. Examples of these...
always - Always executed
sanity - The sanity group is used to annotate a select
group of test methods that can be used as a sanity suite.
smoke - Provide some assurance that the system under
test will not catastrophically fail.
functional - The functional group is used to annotate a
group of test methods that can be used as a full regression
suite.
Andrew Johnstone Selenium
45. Introduction
Locators
Waiting
Examples
Other
Structuring tests...
Always use Annotations to indicate the group of the tests to
perform. Examples of these...
always - Always executed
sanity - The sanity group is used to annotate a select
group of test methods that can be used as a sanity suite.
smoke - Provide some assurance that the system under
test will not catastrophically fail.
functional - The functional group is used to annotate a
group of test methods that can be used as a full regression
suite.
Andrew Johnstone Selenium
46. Introduction
Locators
Waiting
Examples
Other
Structuring tests...
Always use Annotations to indicate the group of the tests to
perform. Examples of these...
always - Always executed
sanity - The sanity group is used to annotate a select
group of test methods that can be used as a sanity suite.
smoke - Provide some assurance that the system under
test will not catastrophically fail.
functional - The functional group is used to annotate a
group of test methods that can be used as a full regression
suite.
Andrew Johnstone Selenium
47. Introduction
Locators
Waiting
Examples
Other
Structuring tests...
Always use Annotations to indicate the group of the tests to
perform. Examples of these...
always - Always executed
sanity - The sanity group is used to annotate a select
group of test methods that can be used as a sanity suite.
smoke - Provide some assurance that the system under
test will not catastrophically fail.
functional - The functional group is used to annotate a
group of test methods that can be used as a full regression
suite.
Andrew Johnstone Selenium
48. Introduction
Locators
Waiting
Examples
Other
Structuring tests...
Always use Annotations to indicate the group of the tests to
perform. Examples of these...
always - Always executed
sanity - The sanity group is used to annotate a select
group of test methods that can be used as a sanity suite.
smoke - Provide some assurance that the system under
test will not catastrophically fail.
functional - The functional group is used to annotate a
group of test methods that can be used as a full regression
suite.
Andrew Johnstone Selenium
49. Introduction
Locators
Waiting
Examples
Other
Structuring tests...
Always use Annotations to indicate the group of the tests to
perform. Examples of these...
deprecated - The deprecated group is used to mark
individual test cases that are no longer active, due to
removed or changed functionality.
unstable - The unstable group is used to mark individual
test cases that are unstable. Some test cases may block
the harness from executing, and these test methods must
be removed from the active test suites to reduce false
negative results.
Andrew Johnstone Selenium
50. Introduction
Locators
Waiting
Examples
Other
Structuring tests...
Always use Annotations to indicate the group of the tests to
perform. Examples of these...
deprecated - The deprecated group is used to mark
individual test cases that are no longer active, due to
removed or changed functionality.
unstable - The unstable group is used to mark individual
test cases that are unstable. Some test cases may block
the harness from executing, and these test methods must
be removed from the active test suites to reduce false
negative results.
Andrew Johnstone Selenium
51. Introduction
Locators
Waiting
Examples
Other
Structuring tests...
Always use Annotations to indicate the group of the tests to
perform. Examples of these...
deprecated - The deprecated group is used to mark
individual test cases that are no longer active, due to
removed or changed functionality.
unstable - The unstable group is used to mark individual
test cases that are unstable. Some test cases may block
the harness from executing, and these test methods must
be removed from the active test suites to reduce false
negative results.
Andrew Johnstone Selenium
52. Introduction
Locators
Waiting
Examples
Other
Alerts
Alerts are a problem with Selenium 1.0, alerts and confirms are
blocking.
Selenium 2.0 overrides the alert, confirm functions within
javascript.
Version 1.0 could not handle them.
AssertAlertPresent - to verify an alert is present
AssertAlert - will add an extra step of verifyTextPresent
"Alert is gone". Do not remove this condition.
Andrew Johnstone Selenium
53. Introduction
Locators
Waiting
Examples
Other
Ajax
This will check whether ajax requests are presently active.
Dojo Javascript Framework
w a i t F o r C o n d i t i o n ( ’ selenium . browserbot . getCurrentWindow ( )
. d o j o . i o . XMLHTTPTransport . i n F l i g h t . l e n g t h == 0 ; ’ ) ;
Prototype Javascript Framework
w a i t F o r C o n d i t i o n ( ’ selenium . browserbot . getCurrentWindow ( )
. Ajax . activeRequestCount == 0 ; ’ ) ;
Jquery Javascript Framework
w a i t F o r C o n d i t i o n ( ’ selenium . browserbot . getUserWindow ( ) . $ . ajaxRequests == 0 ; ’ ) ;
Andrew Johnstone Selenium
54. Introduction
Locators
Waiting
Examples
Other
Advanced examples
Testing navigation on Gorkana with dropdown menus
$ t h i s −>w a i t F o r C o n d i t i o n ( ’ selenium . browserbot . getCurrentWindow ( ) . jQuery ( .sf-with-
ul:visible ) . l e n g t h > 0 ’ , 5 0 0 0 ) ;
$dom = DOMDocument : : loadHTML ( $html = $ t h i s −>getHtmlSource ( ) ) ;
$xpath = new DOMXpath ( $dom ) ;
$ r e t = $xpath−>e v a l u a t e ( ’ / / d i v [ @id=menuBar ] / u l / l i ’ ) ;
$i = 0;
f o r e a c h ( $ r e t as $ r ) {
$xml = $dom−>saveXML ( $ r ) ;
i f ( i s _ i n t ( s t r i p o s ( $xml , ’ s f−sub−i n d i c a t o r ’ ) ) ) {
$xpath = xpath=id(’menuBar’)/ul/li[ . ( $ i +1) . ]/a ;
$ t h i s −>mouseOver ( $xp ) ;
usleep ( 3 0 0 0 0 0 ) ;
$ t h i s −>a s s e r t V i s i b l e ( $xp ) ;
}
$ i ++;
}
Andrew Johnstone Selenium
55. Introduction
Locators
Waiting
Examples
Other
Advanced examples
I will send around separate examples of the following.
Handling emails - such as forgotten passwords
Handling rich text editors - such as CKEditor
Handling Ajax
Handling Excel as a data provider
Andrew Johnstone Selenium
56. Introduction
Locators
Waiting
Examples
Other
Summary
Questions?
Andrew Johnstone Selenium