A. INTRODUCTION

1. Locating element

  • Locating by Id (name=btnSubmit)
  • Locating by Name (name=username)
  • Locating by XPath
  • Locating Hyperlinks by Link Text (link=Continue)
  • Locating by DOM (document.forms[0].username)
  • Locating by CSS (css=#loginForm input[type=”button”])
  • Regular Expression Patterns

2. Supported Platform

  • Google Chrome
  • Internet Explorer 6, 7, 8, 9, 10 – 32 and 64-bit where applicable
  • Firefox: latest ESR, previous ESR, current release, one previous release
  • Safari
  • Opera
  • HtmlUnit
  • phantomjs
  • Android (with Selendroid or appium)
  • iOS (with ios-driver or appium)

 3. Specific advantages

  • Frequent regression testing
  • Rapid feedback to developers
  • Virtually unlimited iterations of test case execution
  • Support for Agile and extreme development methodologies
  • Disciplined documentation of test cases
  • Customized defect reporting
  • Finding defects missed by manual testing

4. When not to automate?

  • UI will change considerably in the near future
  • Not enough time to build test automation
  • For the short term
  • A very tight deadline

5.  Brief History of The Selenium Project

  •  2004 by Jason Huggins: Javascript library (Selenium Core) –> underlies  RC+IDE
  • 2006 by Simon Stewart: WebDriver (Google)
  • 2008: WebDriver and Selenium were merged.

6. Selenium’s Tool Suite

  •  Selenium IDE: is a Firefox add-on that makes it easy to record and playback tests in Firefox 2+. You can even use it to generate code to run the tests with Selenium Remote Control
  • Selenium 1 (aka. Selenium RC or Remote Control)a client/server system that allows you to control web browsers locally or on other computers, using almost any programming language and testing framework.
  • Selenium 2 (aka. Selenium Webdriver)can drive a browser natively either locally or on remote machines.
  • Selenium-Gridmultiple environments ; different tests run in parallel

B. SELENIUM IDE

1. Download

– Selenium IDE

– ui-doc.html

2. Selenium Commands

A command tells Selenium what to do. Selenium commands come in three “flavors”:

  • Actions: are commands that generally manipulate the state of the application
  • Accessors: examine the state and store in variables, e.g. “storeTitle”
  • Assertions: verify that the state, verify checkbox is checked? (“assert“, “verify“, and “waitFor“)
    • assert: fails –> is aborted ; ensure on the correct page
    • verify: fails –> continue ; value field, label, …
    • waitFor: useful for testing Ajax (setTimeOut)

3. Test Suites

An HTML table defines a list of tests where each row defines the file system 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>

4. Locating Elements

Capture

a) Locating by Id

  • id=loginForm

b) Locating by Name

  • name=username
  • name=continue type=button

c) Locating by XPath

  • Start with //, don’t need xpath=
  • /html/body/form[1]
  • //form[1]
  • //form[@id='loginForm']
  • xpath=//form[input/@name='username']
  • //form[@id='loginForm']/input[1]
  • //input[@name='continue'][@type='button']
  •  Reference:

d) Locating Hyperlinks by Link Text

  • link=Click here
  • link=Download

e) Locating by DOM (Document Object Model):

  • Start with document. don’t need dom=, JavaScript 
  • document.getElementById('loginForm')
  • document.forms[0].username
  • document.forms[0].elements['username']

f) Locating by CSS

  • css=form#loginForm
  • css=input[name="username"]
  • css=input.required[type="text"]
  • css=#loginForm input[type="button"]
  • css=#loginForm input:nth-child(2)
  • Referencethe W3C publication

5. Matching Text Patterns

– Examples of commands:

  • verifyTextPresent
  • verifyTitle
  • verifyAlert
  • assertConfirmation
  • verifyText
  • verifyPrompt

– There are three types of patterns: globbing, regular expressions, and exact

  • * – anything
  • [ ] – any single character
    • [aeiou] matches any lowercase vowel
    • [0-9] matches any digit
    • [a-zA-Z0-9] matches any alphanumeric character

Capture

  • verify Time:
    verifyTextPresent regexp:Sunrise: *[0-9]{1,2}:[0-9]{2} [ap]m

6. The “AndWait” Commands

– Wait for the page loading

– Test will fail if not trigger a navigation/refresh

7. The waitFor Commands

– In AJAX driven web applications, data is retrieved from server without refreshing the page, can’t use “AndWait”

  • waitForElementPresent
  • waitForVisible

8. JavaScript Usage with Script Parameters

Command Target Value
store 10 hits
storeXpathCount //blockquote blockquotes
storeEval storedVars[‘hits’]-storedVars[‘blockquotes’] paragraphs
Command Target Value
store Edith Wharton name
storeEval storedVars[‘name’].toUpperCase() uc
storeEval storedVars[‘name’].toLowerCase() lc

 

Command Target Value
store league of nations searchString
type q javascript{storedVars[‘searchString’].toUpperCase()}

9. Alerts, Popups, and Multiple Windows

  • assertAlert
  • verifyTextPresent
  • chooseCancelOnNextConfirmation
  • assertConfirmation
  • answerOnNextPrompt
  • assertPrompt

10.

 

 

 HOT NOTE

  1. waitForPageToLoad
  2. verifyElementPresent: often test an image //center/a/img
  3. Use Select button (Select | Find) in Target to have a suggested location

 

XPATH Locator

1. Full path of XML

  • xpath=//body/div[3]/form/fieldset/input[2]

2. last()

  • xpath=//body/div[3]/form/fieldset/input[last()-2] – the 3rd upper input from last input node.
  • xpath=//body/div[3]/form/fieldset/*[last()-3] – the 4th upper from last node.

3. @ and attribute

  • xpath=//body/div[3]/form/fieldset/input[@type=’search’] – having type=’search’ .
  • xpath=//input[@accesskey=’F’] – short way.

4. /descendant::

  • xpath=//div[@class=’search-container’]/descendant::input[@accesskey=’F’] – div start with class=’search-container’ and final input with accesskey=’F‘ attribute. (Grand-Child)

5. contains

  • xpath=//input[contains(@id, “searchInput”)] – id with “searchInput”.
  • xpath=//input[contains(@id, “searchInput”) and contains(@accesskey,”F”)]

6. position()

  • xpath=//div[@class=’search-container’]/descendant::input[position()=2] – on number 2 position.

7. starts-with

  • xpath=//input[starts-with(@type, “s”)] – ‘type’ starting with ‘s’.

8. OR (|) condition 

  • xpath=//input[@accesskey=’F’] | //input[@id=’searchInput’]
  • xpath=//input[@accesskey=’F’ or @id=’searchInput’] accesskey=’F’ or @id=’searchInput’.

9. Wildcard *

  • xpath=//*[@accesskey=’F’]

10. nth child element of parent

  • xpath=//body/*[3]/form/fieldset/*[2] – This xpath is for search text box. Here, /*[3] describes the 3rd child element of body which is div[3]. Same way *[2] describes the 2nd child element of fieldset which is input[2]

11. Link ‘url’

  • //a[@href=’//meta.wikimedia.org/wiki/List_of_Wikipedias’]

12. No child

  • xpath=//img[count(*)=0]

 

 CSS Locator

1. Tag and any Attribute

  • css=input[type=search] 
  • css=input[id=searchInput] 
  • css=form input[id=searchInput] – have inside (not continue)

2. ID & class

  • css=input#searchInput
  • css=input.formBtn

 3. Tag, class, and any attribute

  • css=input.formBtn[name=go]
  • css=input[type=search][name=search]

4. Sub-string matches(Start, end and containing text)

  • searchInput
  • css=input[id^=’search’] – start.
  • css=input[id$=’chInput’] – end.
  • css=input[id*=’archIn’] – contains.

5. Child Selectors

  • css=div.search-container>form>fieldset>input[id=searchInput] – continue

6. Adjacent selectors

  • css=input + input – locate “input” where another “input” is present before it on page.
  • css=input + select
  • css=input + input + select

7. Contain

  • css=strong:contains(“English”)

 

References: http://docs.seleniumhq.org/