56
Introduction Locators Waiting Examples Other Selenium Andrew Johnstone November 13, 2011 Andrew Johnstone Selenium

Selenium

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Selenium

Andrew Johnstone

November 13, 2011

Andrew Johnstone Selenium

Page 2: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Outline

1 Introduction

2 Locators

3 Waiting

4 Examples

5 Other

Andrew Johnstone Selenium

Page 3: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Outline

1 Introduction

2 Locators

3 Waiting

4 Examples

5 Other

Andrew Johnstone Selenium

Page 4: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Outline

1 Introduction

2 Locators

3 Waiting

4 Examples

5 Other

Andrew Johnstone Selenium

Page 5: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Outline

1 Introduction

2 Locators

3 Waiting

4 Examples

5 Other

Andrew Johnstone Selenium

Page 6: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Outline

1 Introduction

2 Locators

3 Waiting

4 Examples

5 Other

Andrew Johnstone Selenium

Page 7: Selenium

IntroductionLocatorsWaiting

ExamplesOther

What is functional testing

Functional testing is a type of black box testing that bases itstest cases on the specifications of the software componentunder test.Functional testing typically involves five steps

The identification of functions that the software is expectedto performThe creation of input data based on the function’sspecificationsThe determination of output based on the function’sspecificationsThe execution of the test caseThe comparison of actual and expected outputs

Andrew Johnstone Selenium

Page 8: Selenium

IntroductionLocatorsWaiting

ExamplesOther

What is functional testing

Functional testing is a type of black box testing that bases itstest cases on the specifications of the software componentunder test.Functional testing typically involves five steps

The identification of functions that the software is expectedto performThe creation of input data based on the function’sspecificationsThe determination of output based on the function’sspecificationsThe execution of the test caseThe comparison of actual and expected outputs

Andrew Johnstone Selenium

Page 9: Selenium

IntroductionLocatorsWaiting

ExamplesOther

What is functional testing

Functional testing is a type of black box testing that bases itstest cases on the specifications of the software componentunder test.Functional testing typically involves five steps

The identification of functions that the software is expectedto performThe creation of input data based on the function’sspecificationsThe determination of output based on the function’sspecificationsThe execution of the test caseThe comparison of actual and expected outputs

Andrew Johnstone Selenium

Page 10: Selenium

IntroductionLocatorsWaiting

ExamplesOther

What is functional testing

Functional testing is a type of black box testing that bases itstest cases on the specifications of the software componentunder test.Functional testing typically involves five steps

The identification of functions that the software is expectedto performThe creation of input data based on the function’sspecificationsThe determination of output based on the function’sspecificationsThe execution of the test caseThe comparison of actual and expected outputs

Andrew Johnstone Selenium

Page 11: Selenium

IntroductionLocatorsWaiting

ExamplesOther

What is functional testing

Functional testing is a type of black box testing that bases itstest cases on the specifications of the software componentunder test.Functional testing typically involves five steps

The identification of functions that the software is expectedto performThe creation of input data based on the function’sspecificationsThe determination of output based on the function’sspecificationsThe execution of the test caseThe comparison of actual and expected outputs

Andrew Johnstone Selenium

Page 12: Selenium

IntroductionLocatorsWaiting

ExamplesOther

What is functional testing

Functional testing is a type of black box testing that bases itstest cases on the specifications of the software componentunder test.Functional testing typically involves five steps

The identification of functions that the software is expectedto performThe creation of input data based on the function’sspecificationsThe determination of output based on the function’sspecificationsThe execution of the test caseThe comparison of actual and expected outputs

Andrew Johnstone Selenium

Page 13: Selenium

IntroductionLocatorsWaiting

ExamplesOther

What is functional testing

Functional testing is a type of black box testing that bases itstest cases on the specifications of the software componentunder test.Functional testing typically involves five steps

The identification of functions that the software is expectedto performThe creation of input data based on the function’sspecificationsThe determination of output based on the function’sspecificationsThe execution of the test caseThe comparison of actual and expected outputs

Andrew Johnstone Selenium

Page 14: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Selenium

Selenium is a tool to automate browsers.It consists of four projects

Selenium IDE - Firefox add-on that makes it easy to recordand playback testsSelenium Core - Runs the tests directly in the browserSelenium RC - Allows you to control web browsers locallyor on other computersSelenium Grid - Runs tests on many servers at the sametime

Andrew Johnstone Selenium

Page 15: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Selenium

Selenium is a tool to automate browsers.It consists of four projects

Selenium IDE - Firefox add-on that makes it easy to recordand playback testsSelenium Core - Runs the tests directly in the browserSelenium RC - Allows you to control web browsers locallyor on other computersSelenium Grid - Runs tests on many servers at the sametime

Andrew Johnstone Selenium

Page 16: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Selenium

Selenium is a tool to automate browsers.It consists of four projects

Selenium IDE - Firefox add-on that makes it easy to recordand playback testsSelenium Core - Runs the tests directly in the browserSelenium RC - Allows you to control web browsers locallyor on other computersSelenium Grid - Runs tests on many servers at the sametime

Andrew Johnstone Selenium

Page 17: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Selenium

Selenium is a tool to automate browsers.It consists of four projects

Selenium IDE - Firefox add-on that makes it easy to recordand playback testsSelenium Core - Runs the tests directly in the browserSelenium RC - Allows you to control web browsers locallyor on other computersSelenium Grid - Runs tests on many servers at the sametime

Andrew Johnstone Selenium

Page 18: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Why Selenium

Selenium - functional testing for web and mobile basedapplications. (Drivers NOW exist for mobiles/tablets)Multi browser supportWrite test cases in many languages.Simply record interactions with a website from Firefoxusing an IDETests are repeatable

Difficulties/issues with Selenium

Handling ajax request/responses on pagesIE test execution with locatorsFlash/Flex automationRich text editors (CKEditor)

Andrew Johnstone Selenium

Page 19: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators

identifier=id - Locates @id and falls back to @nameid=id - Locates @id onlyname=name - Locates @name onlydom=javascriptExpression -document.getElementById(’el’);xpath=xpathExpressionlink=textPatterncss=cssSelectorSyntaxui=uiSpecifierString - A map of aliases for commonelements. Known as a GUI Map

Andrew Johnstone Selenium

Page 20: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators

identifier=id - Locates @id and falls back to @nameid=id - Locates @id onlyname=name - Locates @name onlydom=javascriptExpression -document.getElementById(’el’);xpath=xpathExpressionlink=textPatterncss=cssSelectorSyntaxui=uiSpecifierString - A map of aliases for commonelements. Known as a GUI Map

Andrew Johnstone Selenium

Page 21: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators

identifier=id - Locates @id and falls back to @nameid=id - Locates @id onlyname=name - Locates @name onlydom=javascriptExpression -document.getElementById(’el’);xpath=xpathExpressionlink=textPatterncss=cssSelectorSyntaxui=uiSpecifierString - A map of aliases for commonelements. Known as a GUI Map

Andrew Johnstone Selenium

Page 22: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators

identifier=id - Locates @id and falls back to @nameid=id - Locates @id onlyname=name - Locates @name onlydom=javascriptExpression -document.getElementById(’el’);xpath=xpathExpressionlink=textPatterncss=cssSelectorSyntaxui=uiSpecifierString - A map of aliases for commonelements. Known as a GUI Map

Andrew Johnstone Selenium

Page 23: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators

identifier=id - Locates @id and falls back to @nameid=id - Locates @id onlyname=name - Locates @name onlydom=javascriptExpression -document.getElementById(’el’);xpath=xpathExpressionlink=textPatterncss=cssSelectorSyntaxui=uiSpecifierString - A map of aliases for commonelements. Known as a GUI Map

Andrew Johnstone Selenium

Page 24: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators

identifier=id - Locates @id and falls back to @nameid=id - Locates @id onlyname=name - Locates @name onlydom=javascriptExpression -document.getElementById(’el’);xpath=xpathExpressionlink=textPatterncss=cssSelectorSyntaxui=uiSpecifierString - A map of aliases for commonelements. Known as a GUI Map

Andrew Johnstone Selenium

Page 25: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators

identifier=id - Locates @id and falls back to @nameid=id - Locates @id onlyname=name - Locates @name onlydom=javascriptExpression -document.getElementById(’el’);xpath=xpathExpressionlink=textPatterncss=cssSelectorSyntaxui=uiSpecifierString - A map of aliases for commonelements. Known as a GUI Map

Andrew Johnstone Selenium

Page 26: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators

identifier=id - Locates @id and falls back to @nameid=id - Locates @id onlyname=name - Locates @name onlydom=javascriptExpression -document.getElementById(’el’);xpath=xpathExpressionlink=textPatterncss=cssSelectorSyntaxui=uiSpecifierString - A map of aliases for commonelements. Known as a GUI Map

Andrew Johnstone Selenium

Page 27: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators

identifier=id - Locates @id and falls back to @nameid=id - Locates @id onlyname=name - Locates @name onlydom=javascriptExpression -document.getElementById(’el’);xpath=xpathExpressionlink=textPatterncss=cssSelectorSyntaxui=uiSpecifierString - A map of aliases for commonelements. Known as a GUI Map

Andrew Johnstone Selenium

Page 28: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators - Xpath/CSS

XPATH is SLOW on IE as such use CSS selectors for locatingsimple elements.

They’re fasterThey’re more readableCSS is jQuery’s locating strategyNo one else uses XPATH anyway

Performance comparison of CSS vs XPATH

Andrew Johnstone Selenium

Page 29: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators CSS - Direct child

A direct child in XPATH is defined by the use of a "/", while onCSS, it’s defined using ">"Xpath

//div/a

CSS

css=div > a

Andrew Johnstone Selenium

Page 30: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators CSS - Child or subchild

If an element could be inside another or one it’s childs, it’sdefined in XPATH using "//" and in CSS just by a whitespaceXpath

//div/a

CSS

div a

Andrew Johnstone Selenium

Page 31: Selenium

IntroductionLocatorsWaiting

ExamplesOther

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

Page 32: Selenium

IntroductionLocatorsWaiting

ExamplesOther

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

Page 33: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators CSS - More examples

Not all CSS 3 selectors are supported by Selenium 2.Next sibling - css=form input.username + inputAttribute 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

Page 34: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators XPATH - Examples

XPATH Selectors can easily become horrible andunmanageable//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

Page 35: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Locators XPATH - The Good, the bad and the ugly

An example of a bad XPath - it will break as soon as the pagelayout 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 awayfrom it

//td[contains(text()),’My Label’]/following-sibling::td[2]/input

Andrew Johnstone Selenium

Page 36: Selenium

IntroductionLocatorsWaiting

ExamplesOther

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 | 60000assertText css=h2.titles_class | Manta

All wait commands will use a delay within a loop until acondition is met.

Andrew Johnstone Selenium

Page 37: Selenium

IntroductionLocatorsWaiting

ExamplesOther

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 | 60000assertText css=h2.titles_class | Manta

All wait commands will use a delay within a loop until acondition is met.

Andrew Johnstone Selenium

Page 38: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Waiting...

Selenium 2.0 - Use implicit waits over explicit waitsExplicit Waits

An explicit waits is code you define to wait for a certaincondition to occur before proceeding further in the code.There are some convenience methods provided that help youwrite code that will wait only as long as required.WebDriverWait in combination with ExpectedCondition is oneway this can be accomplished.

Implicit Waits

An implicit wait is to tell WebDriver to poll the DOM for acertain amount of time when trying to find an element orelements if they are not immediately available.The default setting is 0.

Andrew Johnstone Selenium

Page 39: Selenium

IntroductionLocatorsWaiting

ExamplesOther

saucelabs

Selenium RC in the cloud with support for all major browsersSauceconnect allows access to internal firewalled infrastructurefrom saucelabs

Andrew Johnstone Selenium

Page 40: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Installation of Selenium

suapt-get updateapt-get install php-pearpear upgrade pearpear channel-discover pear.phpunit.depear channel-update pear.phpunit.depear channel-discover components.ez.nopear channel-update components.ez.nopear channel-discover pear.symfony-project.compear channel-update pear.symfony-project.com

pear channel-discover saucelabs.github.com/pearpear channel-update saucelabs.github.com/pear

pear install -a saucelabs/PHPUnit_Selenium_SauceOnDemand

sauce configure <username> <key>

Andrew Johnstone Selenium

Page 41: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Example Selenium test case with PHPUnit

<?php

require_once ’ PHPUnit / Extensions / SeleniumTestCase / ’ .’ SauceOnDemandTestCase . php ’ ;

c lass ExampleTestextends PHPUnit_Extensions_SeleniumTestCase_SauceOnDemandTestCase {

f u n c t i o n setUp ( ) {$ th i s−>setOs ( ’ Windows 2003 ’ ) ;$ th i s−>setBrowser ( ’ f i r e f o x ’ ) ;$ th i s−>setBrowserVersion ( ’ 3 . 6 . ’ ) ;$ th i s−>setBrowserUr l ( ’ h t t p : / / example . saucelabs . com ’ ) ;

}

f u n c t i o n test_example ( ) {$ th i s−>open ( ’ / ’ ) ;$ th i s−>a s s e r t T i t l e ( ’ Cross browser t e s t i n g wi th Selenium − Sauce Labs ’ ) ;

}}

Andrew Johnstone Selenium

Page 42: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Example Selenium test case with PHPUnit

Example running against 3 different browsers.Tests can be parallelized to speed execution.

Andrew Johnstone Selenium

Page 43: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Selenium versions

Selenium 1

Recommended timeouts 90000ms - the default is set toselenium.setTimeout(30000);

Generate application states with test handles.As an example if a login fails certain paths of executionwill 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

Page 44: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Structuring tests...

Always use Annotations to indicate the group of the tests toperform. Examples of these...

always - Always executedsanity - The sanity group is used to annotate a selectgroup of test methods that can be used as a sanity suite.smoke - Provide some assurance that the system undertest will not catastrophically fail.functional - The functional group is used to annotate agroup of test methods that can be used as a full regressionsuite.

Andrew Johnstone Selenium

Page 45: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Structuring tests...

Always use Annotations to indicate the group of the tests toperform. Examples of these...

always - Always executedsanity - The sanity group is used to annotate a selectgroup of test methods that can be used as a sanity suite.smoke - Provide some assurance that the system undertest will not catastrophically fail.functional - The functional group is used to annotate agroup of test methods that can be used as a full regressionsuite.

Andrew Johnstone Selenium

Page 46: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Structuring tests...

Always use Annotations to indicate the group of the tests toperform. Examples of these...

always - Always executedsanity - The sanity group is used to annotate a selectgroup of test methods that can be used as a sanity suite.smoke - Provide some assurance that the system undertest will not catastrophically fail.functional - The functional group is used to annotate agroup of test methods that can be used as a full regressionsuite.

Andrew Johnstone Selenium

Page 47: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Structuring tests...

Always use Annotations to indicate the group of the tests toperform. Examples of these...

always - Always executedsanity - The sanity group is used to annotate a selectgroup of test methods that can be used as a sanity suite.smoke - Provide some assurance that the system undertest will not catastrophically fail.functional - The functional group is used to annotate agroup of test methods that can be used as a full regressionsuite.

Andrew Johnstone Selenium

Page 48: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Structuring tests...

Always use Annotations to indicate the group of the tests toperform. Examples of these...

always - Always executedsanity - The sanity group is used to annotate a selectgroup of test methods that can be used as a sanity suite.smoke - Provide some assurance that the system undertest will not catastrophically fail.functional - The functional group is used to annotate agroup of test methods that can be used as a full regressionsuite.

Andrew Johnstone Selenium

Page 49: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Structuring tests...

Always use Annotations to indicate the group of the tests toperform. Examples of these...

deprecated - The deprecated group is used to markindividual test cases that are no longer active, due toremoved or changed functionality.unstable - The unstable group is used to mark individualtest cases that are unstable. Some test cases may blockthe harness from executing, and these test methods mustbe removed from the active test suites to reduce falsenegative results.

Andrew Johnstone Selenium

Page 50: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Structuring tests...

Always use Annotations to indicate the group of the tests toperform. Examples of these...

deprecated - The deprecated group is used to markindividual test cases that are no longer active, due toremoved or changed functionality.unstable - The unstable group is used to mark individualtest cases that are unstable. Some test cases may blockthe harness from executing, and these test methods mustbe removed from the active test suites to reduce falsenegative results.

Andrew Johnstone Selenium

Page 51: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Structuring tests...

Always use Annotations to indicate the group of the tests toperform. Examples of these...

deprecated - The deprecated group is used to markindividual test cases that are no longer active, due toremoved or changed functionality.unstable - The unstable group is used to mark individualtest cases that are unstable. Some test cases may blockthe harness from executing, and these test methods mustbe removed from the active test suites to reduce falsenegative results.

Andrew Johnstone Selenium

Page 52: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Alerts

Alerts are a problem with Selenium 1.0, alerts and confirms areblocking.Selenium 2.0 overrides the alert, confirm functions withinjavascript.

Version 1.0 could not handle them.AssertAlertPresent - to verify an alert is presentAssertAlert - will add an extra step of verifyTextPresent"Alert is gone". Do not remove this condition.

Andrew Johnstone Selenium

Page 53: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Ajax

This will check whether ajax requests are presently active.Dojo Javascript Framework

wai tForCond i t ion ( ’ selenium . browserbot . getCurrentWindow ( ). dojo . i o . XMLHTTPTransport . i n F l i g h t . l eng th == 0; ’ ) ;

Prototype Javascript Framework

wai tForCond i t ion ( ’ selenium . browserbot . getCurrentWindow ( ). Ajax . act iveRequestCount == 0; ’ ) ;

Jquery Javascript Framework

wai tForCond i t ion ( ’ selenium . browserbot . getUserWindow ( ) . $ . ajaxRequests == 0; ’ ) ;

Andrew Johnstone Selenium

Page 54: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Advanced examples

Testing navigation on Gorkana with dropdown menus

$ th is−>wa i tForCond i t ion ( ’ selenium . browserbot . getCurrentWindow ( ) . jQuery ( .sf-with-ul:visible ) . leng th > 0 ’ , 5000) ;

$dom = DOMDocument : : loadHTML ( $html = $ th is−>getHtmlSource ( ) ) ;$xpath = new DOMXpath($dom ) ;$ re t = $xpath−>evaluate ( ’ / / d i v [ @id=menuBar ] / u l / l i ’ ) ;

$ i = 0 ;foreach ( $ re 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 ;$ th is−>mouseOver ( $xp ) ;usleep (300000) ;$ th is−>a s s e r t V i s i b l e ( $xp ) ;

}$ i ++;

}

Andrew Johnstone Selenium

Page 55: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Advanced examples

I will send around separate examples of the following.Handling emails - such as forgotten passwordsHandling rich text editors - such as CKEditorHandling AjaxHandling Excel as a data provider

Andrew Johnstone Selenium

Page 56: Selenium

IntroductionLocatorsWaiting

ExamplesOther

Summary

Questions?

Andrew Johnstone Selenium