SlideShare a Scribd company logo
1 of 86
Download to read offline
MD
Full Day Tutorial
10/13/2014 8:30:00 AM
"Introduction to Selenium and
WebDriver "
Presented by:
Alan Richardson
Compendium Developments Ltd
Brought to you by:
340 Corporate Way, Suite 300, Orange Park, FL 32073
888-268-8770 ∙ 904-278-0524 ∙ sqeinfo@sqe.com ∙ www.sqe.com
Alan Richardson
Compendium Developments
Alan Richardson has more than twenty years of professional IT experience, working as a
programmer and at every level of the testing hierarchy—from tester through head of testing.
Author of the books Selenium Simplified and Java For Testers, Alan also has created online
training courses to help people learn technical web testing and Selenium WebDriver with Java.
He now works as an independent consultant, helping companies improve their use of
automation, agile, and exploratory technical testing. Alan posts his writing and training videos
on SeleniumSimplified.com, EvilTester.com, JavaForTesters.com, and CompendiumDev.co.uk.
Speaker Pr
esentations
1
© Compendium Developments, 2014, CompendiumDev.co.uk
Hands On WebDriver
Alan Richardson
SeleniumSimplified.com
JavaForTesters.com
EvilTester.com
CompendiumDev.co.uk
© Compendium Developments, 2014, CompendiumDev.co.uk
Course Overview
●
Hints and Tips
●
Installation
●
Basic WebDriver API capabilities
●
Simple interrogation and navigation
●
Synchronization strategies
●
AJAX applications.
●
Tools
●
Location strategies using CSS and XPath.
●
Introduction to abstraction approaches
2
© Compendium Developments, 2014, CompendiumDev.co.uk
Approach
●
Presentation
●
Demos
●
Exercises
– Multiple Tracks depending on your level
© Compendium Developments, 2014, CompendiumDev.co.uk
Status Check
●
Java Experience?
●
Automation Experience?
●
Selenium Experience?
●
WebDriver Experience?
3
© Compendium Developments, 2014, CompendiumDev.co.uk
With Mixed Experience Levels...
●
If you get stuck, ask for help
●
You can keep doing the exercises, even when
we move on
●
Read the source code for the answers
●
Try and run the answer source code
●
Run the answer source code in debug mode
●
Expect some of this to go over your head
© Compendium Developments, 2014, CompendiumDev.co.uk
Demo – WebDriver in Action
mvn -Dtest=TheDemoTest test
●
A quick walkthrough of the demo code
– JUnit, @Test
– Java public static Config
– WebDriver driver
– New FirefoxDriver
– Driver.
●
navigate() .to() .back()
●
findElement By.id(...) By.cssSelector
●
switchTo alert
●
close, quit
4
© Compendium Developments, 2014, CompendiumDev.co.uk
Introduction & Overview
© Compendium Developments, 2014, CompendiumDev.co.uk
Ancient History
●
Selenium 1.0
– Javascript through a proxy
– Flat API “selenium.”
– Server based
●
WebDriver
– Object Oriented API, Interface which each driver
implements
– Driver calls browser directly
– Has a server component Remote WebDriver
5
© Compendium Developments, 2014, CompendiumDev.co.uk
Installation
●
Java SDK
●
Maven
●
IntelliJ IDE
●
Firefox
●
FirePath plugin
seleniumsimplified.com/get-started/
© Compendium Developments, 2014, CompendiumDev.co.uk
Exercises: Download, Run, Import
●
Exercise: Download
– Download the source code
●
Exercise: Run from Command Line
– Run “mvn test -Dtest=MyFirstTest” from
the command line
●
Exercise: Import into IDE
– Import the code into IntelliJ
●
Exercise: Run MyFirstTest from IDE
– Run the “MyFirstTest”
6
© Compendium Developments, 2014, CompendiumDev.co.uk
What is Maven?
●
A build tool
●
Dependency Management
●
Standard Folder Structure
●
Java project tasks simpler
– mvn clean compile
– mvn package
– mvn test
●
http://maven.apache.org
© Compendium Developments, 2014, CompendiumDev.co.uk
pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.42.2</version>
</dependency>
</dependencies>
7
© Compendium Developments, 2014, CompendiumDev.co.uk
Java Language Levels
●
Set in IntelliJ Project Settings
●
And in the pom.xml
<!-- I have added the build section to support importing into
IntelliJ automatically without throwing errors about wrong Java
Version. This basically says the source requires at least Java 1.7
and use a compiler that outputs Java 1.7 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
© Compendium Developments, 2014, CompendiumDev.co.uk
Benefits of Maven
●
Standard directory structure
●
Supported by Selenium and some of the drivers
●
Easy to update and install
●
Simple to add to Continuous Integration
– “mvn test”
8
© Compendium Developments, 2014, CompendiumDev.co.uk
Section: WebDriver is an API
© Compendium Developments, 2014, CompendiumDev.co.uk
API Overview
http://www.mindmeister.com/280141421/selenium-2-webdriver-commands
http://unow.be/at/apimindmap
9
© Compendium Developments, 2014, CompendiumDev.co.uk
Demos During This Section
●
Explain basic code sample of MyFirstTest
●
Inspect WebElements using Firebug
●
Various API constructs
●
Use of Code Completion, JavaDoc, Autoscroll,
params, view code
●
How to run a test Class and Method
●
How to debug a test Method, with breakpoints
●
How to use evaluate
© Compendium Developments, 2014, CompendiumDev.co.uk
MyFirstTest.javapackage com.eviltester.webdriver;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MyFirstTest {
@Test
public void startWebDriver(){
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://seleniumsimplified.com");
Assert.assertTrue("Expected Selenium Simplified",
driver.getTitle().
startsWith("Selenium Simplified"));
driver.close();
driver.quit();
}
}
10
© Compendium Developments, 2014, CompendiumDev.co.uk
Basic Knowledge
●
Open a browser
●
Navigate to page
●
Read the page title
●
Read the url
●
Get text from the page
●
Click on links
●
Fill in forms
●
Click on buttons
Navigate
Interrogate
Manipulate
& Synchronize
© Compendium Developments, 2014, CompendiumDev.co.uk
WebDriver Basics
●
WebDriver
– Think of the driver as the browser
– Interface with implementations – FirefoxDriver,
ChromeDriver etc.
– High Level commands
●
get, getTitle, getCurrentUrl, close, quit
– Functional Groupings
●
Navigate, Manage
– Locate
●
FindElement, FindElements
11
© Compendium Developments, 2014, CompendiumDev.co.uk
WebElement Basics
●
The DOM elements
– <a>, <p>, <ul>, etc.
●
Found By.Id, By.Name, etc.
●
Interrogate
– .getAttribute, .getValue, isEnabled etc.
●
Manipulate
– SendKeys, click, etc.
© Compendium Developments, 2014, CompendiumDev.co.uk
JUnit Basics
●
Test Execution Framework
●
@Test
– Annotate a method with @Test
●
Can Run @Test methods from the IDE
●
Use Assertions to check results
– Import org.junit.Assert;
– Assert.assertTrue(true);
– Assert.assertEquals(1+1, 2);
12
© Compendium Developments, 2014, CompendiumDev.co.uk
WebDriver API Learning Tips
●
Use Code Completion (ctrl+space)
●
ctrl+Q for Documentation of Commands (ctrl+j
on mac)
●
ctrl+click to view the actual WebDriver code
(cmd+click on mac)
●
AutoScroll
© Compendium Developments, 2014, CompendiumDev.co.uk
IntelliJ Navigation Shortcut Keys
●
Find Symbol
– Shift + Cntrl + Alt + N
●
Find Class
– Cntrl + N
●
Find File
– Shift + Cntrl + N
on mac use Cmd instead of Cntrl
13
© Compendium Developments, 2014, CompendiumDev.co.uk
Browser Tools Overview
●
Firefox
– Inspect
– Inspect with Firebug
●
FirePath
●
Chrome Inspect
© Compendium Developments, 2014, CompendiumDev.co.uk
Section: My First Test
14
© Compendium Developments, 2014, CompendiumDev.co.uk
exercises.myfirsttest.
MyFirstExtendedTest.java
@Test
public void getBasicPageAndCheckDetails(){
WebDriver driver = new FirefoxDriver();
driver.get(
"http://seleniumsimplified.com/testpages/basic_web_page.html"
);
Assert.assertTrue("Expected Different Title",
driver.getTitle().
equals("Basic Web Page Title"));
WebElement para1 = driver.findElement(By.id("para1"));
Assert.assertTrue(
"A paragraph of text".equals(para1.getText()));
driver.close();
driver.quit();
}
Exercise:
© Compendium Developments, 2014, CompendiumDev.co.uk
Used WebDriver API Summary
●
WebDriver driver = new FirefoxDriver();
– Create a new instance of a driver and
browser
●
driver.get(aURL);
– Open a web page in the browser
●
findElement(s), By.id, By.name, By.tagName
– WebElement element =
driver.findElement(By.id(“anid));
– Find a WebElement using id or name
attribute,
– NoSuchElementException
– findElement can be chained
●
WebElement
– getText, getAttribute
●
driver.getTitle()
– Get the title of the current
page
●
driver.close(), driver.quit()
– Close the current browser
window,
– quit the browser and driver
●
Junit Used
– @Test
– Assert.assertTrue
– Assert.assertEquals
– Comment as first arg
– Expected, then Actual
15
© Compendium Developments, 2014, CompendiumDev.co.uk
IntelliJ & JUnit
© Compendium Developments, 2014, CompendiumDev.co.uk
Java & Maven Summary
●
Packages organise the Java classes
●
Java Classes Start with Uppercase letters
●
Methods start with lowercase letters
●
Add tests in the src/test/java folder hierarchy
●
Add Test into the class name to automatically
run from maven as a test
●
Import classes from packages to use them
●
Code completion helps, be careful with imports
16
© Compendium Developments, 2014, CompendiumDev.co.uk
Basics
●
Use code completion as much as possible
●
Learn short cut keys
●
Remove any code syntax errors as fast as
possible because they interfere with code
completion
© Compendium Developments, 2014, CompendiumDev.co.uk
IntelliJ Evaluate Expression
●
In Debug mode use Evaluate to experiment
with code constructs in situ.
– e.g. when writing xpath findElements code
●
Can sometimes recompile and hot swap the
code into the running debug session.
17
© Compendium Developments, 2014, CompendiumDev.co.uk
Issues With Current Test
●
Many checks in same test method
●
If one check fails we have no info on later
checks
– But these are same state checks
– i.e. page is same state for all checks, so we
should get that information
© Compendium Developments, 2014, CompendiumDev.co.uk
Solution – Use JUnit to setup state
●
@Test
– The set of checks we want to make
– We can setup state in the test and move through
state e.g. open page, click links, etc.
●
@Before
– Before every @Test, do some stuff to setup state
●
@BeforeClass
– Before any test in the Class, setup some state
●
@After, @AfterClass
– 'clean up' for each 'before'
18
© Compendium Developments, 2014, CompendiumDev.co.uk
Demo - JunitBeforeAfterDemoTest
●
Outputs:
●
@BeforeClass
– -BeforeClass
●
@Before
– . Before
●
@Test (4 tests)
– + TestX
●
@AfterClass
– -AfterClass
●
@After
– . After
●
What will Happen?
© Compendium Developments, 2014, CompendiumDev.co.uk
JUnit @Before, @After
●
@BeforeClass,
– Run once before any @Test in the class is run
– static
●
@AfterClass
– Run once after all @Test in the class have run
– static
●
@Before,
– Run before each @Test
●
@After
– Run after each @Test
19
© Compendium Developments, 2014, CompendiumDev.co.uk
Demo
●
Refactor MyFirstExtendedTest to use
●
@Before, @After and
●
@BeforeClass and @AfterClass
© Compendium Developments, 2014, CompendiumDev.co.uk
JUnit Exercises
20
© Compendium Developments, 2014, CompendiumDev.co.uk
Debrief
●
WebDriver is not a test tool
●
JUnit is a test tool
●
JUnit, (or TestNG, or Cucumber etc.) give us
the 'test' capabilities
●
WebDriver provides the browser automation
capabilities
●
@Before,@After etc. allow us to make the test
code more readable so the @Test focuses on
the 'test' and the 'check' not the 'setup'
●
Keep your 'tests' small
© Compendium Developments, 2014, CompendiumDev.co.uk
Navigation & Interrogation
21
© Compendium Developments, 2014, CompendiumDev.co.uk
Navigation & Interrogation
●
We can navigate() around the site
●
We can find an individual element or find
multiple elements using different By locator
methods e.g. by Id, name, tagName etc.
●
When we have found an element we can
interrogate it using the methods on
WebElement
●
WebDriver object exposes interrogation
methods e.g. getUrl, getTitle, etc.
© Compendium Developments, 2014, CompendiumDev.co.uk
WebDriver
22
© Compendium Developments, 2014, CompendiumDev.co.uk
Navigation Annotated
●
driver
– .get(<url>)
– .navigate
●
.to(<url>)
●
.to(URL)
●
.back()
●
.forward()
●
.refresh()
driver.get(“http://aURL.com”);
'to' a URL String
'to' a java.net.URL Object
'back' through browser history
'forward' through browser history
'refresh' the current browser page
© Compendium Developments, 2014, CompendiumDev.co.uk
Driver level Interrogation Methods
●
driver
– .getTitle()
– .getCurrentUrl()
– .getPageSource()
All return String.
Pretty obvious what
each method does.
Be wary of using
getPageSource it may
not return what you
expect.
23
© Compendium Developments, 2014, CompendiumDev.co.uk
Be Careful with getPageSource
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Basic Web Page Title</title>
</head>
<body>
<p id="para1" class="main">A paragraph of text</p>
<p id="para2" class="sub">Another paragraph of text</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<title>Basic Web Page Title</title>
</head>
<body>
<p class="main" id="para1">A paragraph of text</p>
<p class="sub" id="para2">Another paragraph of text</p>
</body></html>
File on
server
String
returned
by
method
Line separation
Firefox
Driver
Differences
Additional
attributes
Attribute
Ordering
© Compendium Developments, 2014, CompendiumDev.co.uk
Driver .findElement(By)
●
By.id
●
By.xpath
●
By.cssSelector
●
By.className
●
By.linkText
●
By.name
●
By.tagName
●
By.partialLinkText
We find an element By
using a locator
strategy.
e.g. by using the id, by
evaluating an xpath
expression, by
executing a css
selector, etc.
driver.findElement(By.id("para1"))
24
© Compendium Developments, 2014, CompendiumDev.co.uk
.findElements
●
.findElement only returns 1 WebElement
●
When a By can return more elements then
.findElement returns the first in the list
●
.findElements does not throw an exception if it
can't match anything, it returns an empty list
●
.findElements returns the full list of matching
WebElements
– e.g. driver.findElements(By.className(“normal”));
© Compendium Developments, 2014, CompendiumDev.co.uk
Chaining .findElement (s)
findElement(By.id(“.”)).findElement(By.name(“.”))
e.g.
WebElement element = driver.findElement(By.id("div1")).
findElement(By.name("pName3")).
findElement(By.tagName("a"));
●
Can use any By locator strategy
●
Cannot Chain .findElements() as it returns a
List of Web Elements
List<WebElement>
25
© Compendium Developments, 2014, CompendiumDev.co.uk
Chaining with ByChained
●
ByChained is a support class
import org.openqa.selenium.support.pagefactory.ByChained;
●
ByChained extends By (it is a By)
●
Instantiate it and pass in the By objects
element = driver.findElement(
new ByChained(
By.id("div1"),
By.name("pName9"),
By.tagName("a")));
Takes a list of By Objects
Have to instantiate,
not use statically
© Compendium Developments, 2014, CompendiumDev.co.uk
Other By Support Classes
●
ByIdOrName(“string to match”)
@Test
public void findByIdOrNameMatchOnName(){
WebElement element;
element = driver.findElement(
new ByIdOrName("pName2"));
assertEquals("expected a match on name",
"This is b paragraph text",
element.getText());
}
Takes a String which
could be the ID
or the Name
Have to instantiate,
not use statically
26
© Compendium Developments, 2014, CompendiumDev.co.uk
Basic WebElement API Summary
●
WebElement, findElement,
– WebElement element =
driver.findElement(By.id(“anid));
– Find a WebElement using id or name
attribute,
– findElement can be chained
●
By.id, By.name
– Locators to 'find' the elements
●
element.sendKeys(“type this”);
– Send keys to a web element to
type something
●
element.click()
– Click on a web element
element.getText
– Get the text of the current
element
© Compendium Developments, 2014, CompendiumDev.co.uk
WebElement
27
© Compendium Developments, 2014, CompendiumDev.co.uk
Dom Element Interrogation
Approach
●
Find the Dom Element (WebElement)
– .findElement
– .findElements
●
Use the WebElement Object methods:
– .getText()
– .getAttribute()
– .getTagName()
– .isEnabled()
– .isSelected()
– .isDisplayed()
– .getSize()
– .getLocation()
– .getCssValue()
If you want to interrogate,
you have to locate
© Compendium Developments, 2014, CompendiumDev.co.uk
Does Navigation Require
Synchronisation?
●
Navigation blocks until the HTML of the page is
loaded
●
This does not mean the page is ready
– for manipulation or
– full interrogation
●
Sometimes synchronisation is required on
navigation
28
© Compendium Developments, 2014, CompendiumDev.co.uk
What about clicking?
●
Normally we navigate by clicking:
– on links
– on buttons
– by submitting forms
●
This is navigation as a side-effect
– It normally requires synchronisation
– We will cover this later in Manipulation
© Compendium Developments, 2014, CompendiumDev.co.uk
Navigation & Interrogation Exercises
29
© Compendium Developments, 2014, CompendiumDev.co.uk
Manipulation
© Compendium Developments, 2014, CompendiumDev.co.uk
Manipulation
●
When we find an element we can use the
methods on the WebElement to do stuff e.g.
click, clear, sendKeys etc.
30
© Compendium Developments, 2014, CompendiumDev.co.uk
WebElement Manipulation
●
.click()
●
.clear()
– Clear a field
●
.sendKeys(String) actually (CharSequence)
– Sends the appropriate events to a WebElement
keyup, keydown, etc.
– Helper Keys class (org.openqa.selenium.Keys)
●
.submit()
– Submit a form
© Compendium Developments, 2014, CompendiumDev.co.uk
More on SendKeys
●
Keys.chord to send Key Sequences
●
Shift, CTRL etc start a modifier sequence
●
Keys.NULL ends a modifier sequence
aTextArea.sendKeys("this text", "and this text");
commentTextArea.sendKeys(
Keys.chord(Keys.SHIFT, "bob", Keys.NULL, " Dobbs"));
assertEquals("BOB Dobbs", commentTextArea.getText())
31
© Compendium Developments, 2014, CompendiumDev.co.uk
SendKeys
© Compendium Developments, 2014, CompendiumDev.co.uk
Manipulation Exercises
32
© Compendium Developments, 2014, CompendiumDev.co.uk
Location Strategies
© Compendium Developments, 2014, CompendiumDev.co.uk
CSS Selectors
33
© Compendium Developments, 2014, CompendiumDev.co.uk
Useful Tools For CSS and XPath
●
Firefox
– Install FireBug and FirePath plugins
●
Chrome
– Developer tools are supposed to allow search using
xpath or css (sometimes this breaks between
releases)
© Compendium Developments, 2014, CompendiumDev.co.uk
CSS Selectors
●
A CSS Selector matches a set of elements
●
Used to style HTML from CSS Stylesheets
●
Reused in Selenium to match DOM elements
●
Useful References
– https://developer.mozilla.org/en-US/docs/CSS
– http://reference.sitepoint.com/css/selectorref
– http://net.tutsplus.com/tutorials/html-css-techniques/
the-30-css-selectors-you-must-memorize/
– http://www.quirksmode.org/css/contents.html
– http://www.w3schools.com/cssref/css_selectors.asp
– http://css-tricks.com/attribute-selectors/
34
© Compendium Developments, 2014, CompendiumDev.co.uk
Basics of CSS Selectors
●
*
– match any element e.g. *, *[name="bob"]
●
#id
– match an id e.g. #p4
●
.class
– match a class e.g. “.normal”, “p.normal”
●
tag
– match tags e.g. p, a, div
●
[attribute]
– Match on the attribute name e.g. [id]
© Compendium Developments, 2014, CompendiumDev.co.uk
CSS Attribute Matching
●
tag[attribute^=”val”]
– Partial match start of value
●
tag[attribute$=”lue”]
– Partial match end of value
●
tag[attribute~=”value”]
– Match on space separated
values
●
tag[a='val'], tag[b='val']
– , is an 'or' grouping
●
tag[attribute]
– match tags with an
attribute
●
tag[attribute=”value”]
– match tags with a specific
attribute value
●
tag[attr1='val1'][attr2='val2']
– match tag using multiple
attribute values
●
tag[attribute*=”alu”]
– Partial match anywhere on
value
35
© Compendium Developments, 2014, CompendiumDev.co.uk
CSS Selectors – Some Paths
●
A > B – B directly under A e.g. <A><B/></A>
●
A B – descendant
– selectors separated by space i.e. “this then that”
– Any degree of separation
– e.g. “div li” would match but “div > li” would not
●
A + B – B siblings of an A
●
B:first-child - every B which is first child of
something
●
B:nth-child(n) – the nth child of B e.g. n==3, 3rd
– For more selectors see the references
© Compendium Developments, 2014, CompendiumDev.co.uk
By.cssSelector
●
By.cssSelector(<a css selector string>)
@Test
public void findElementsUsingCSSTag(){
List<WebElement> elements;
elements = driver.findElements(
By.cssSelector("p"));
assertEquals("expected a different number",
41,
elements.size());
}
36
© Compendium Developments, 2014, CompendiumDev.co.uk
CSS Selectors
●
CSS Selectors are faster than Xpath
●
CSS Selectors can't traverse back up the dom
●
Try and use CSS Selectors by default for
complicated selection queries
© Compendium Developments, 2014, CompendiumDev.co.uk
Exercise: Manual CSS Selectors
37
© Compendium Developments, 2014, CompendiumDev.co.uk
XPath
© Compendium Developments, 2014, CompendiumDev.co.uk
XPath
●
XPath is an XML Query Language
●
XPath is often slower than CSS
●
XPath has more functions than CSS
●
Xpath can navigate up and down the DOM
●
References
– http://www.w3schools.com/xpath/
– http://www.w3schools.com/xpath/xpath_functions.asp
38
© Compendium Developments, 2014, CompendiumDev.co.uk
By.xpath
@Test
public void findSpecificPara(){
WebElement element;
element = driver.findElement(
By.xpath("//p[@name='pName5']"));
assertEquals("Expected matching id",
"p5",
element.getAttribute("id"));
}
Match p elements
anywhere in the
DOM which have
a name of 'pName5'
© Compendium Developments, 2014, CompendiumDev.co.uk
Xpath Selector Basics
●
// - match anywhere
●
/ - match from root
●
//* - any element
●
//tag – named element
●
//*[@attribute] – match if it has attribute
●
//*[@attribute='value'] – attribute with value
●
. for content matching
●
.. for traversing back up the path e.g. //div/p/a/..
●
Operators: and, or etc
– w3schools.com/xpath/xpath_operators.asp
●
Xpath functions e.g. contains, starts-with
– w3schools.com/xpath/xpath_functions.asp
39
© Compendium Developments, 2014, CompendiumDev.co.uk
Additional References
●
Xpath, CSS Rosetta Stone
– http://www.simple-talk.com/dotnet/.net-
framework/xpath,-css,-dom-and-selenium-the-
rosetta-stone
– http://bit.ly/RDJ3Wb
●
Note browsers tend to use Xpath 1.0
© Compendium Developments, 2014, CompendiumDev.co.uk
Chain XPath & CSS in findElement
●
Remember we can 'chain' findElement
– elem.findElement(By.id('me')).findElement(By.id('this');
●
You can use a combination of css selector and
xpath selector
– parentWebElement =
webElementFoundByCSS.findElement(By.xpath(".."));
– parentWebElement.findElements(
By.cssSelector("div > div"));
40
© Compendium Developments, 2014, CompendiumDev.co.uk
Exercise: Manual XPath Selectors
© Compendium Developments, 2014, CompendiumDev.co.uk
Exercise: CSS Basic Exercises
41
© Compendium Developments, 2014, CompendiumDev.co.uk
Synchronisation
© Compendium Developments, 2014, CompendiumDev.co.uk
Synchronisation
●
We need to synchronise the test with the
application execution otherwise the test will try
and click on things that are not displayed (etc.)
and the test will fail.
●
Most of our synchronisation will involve telling
WebDriver to Wait for certain conditions to be
true e.g. element to be clickable, presence
of element located, etc.
42
© Compendium Developments, 2014, CompendiumDev.co.uk
Demo
●
Manually use basic_ajax.html
●
Run the synchronisation demo test in debug
mode
●
Run the synchronisation demo test to failure
●
Discuss
– Why Synchronise?
– What can we synchronise on here?
© Compendium Developments, 2014, CompendiumDev.co.uk
Discussion
●
Why wait?
●
What conditions might you have waited for?
e.g. visible... what else?
●
Why wait for title instead of assert?
●
Can we make the code cleaner?
43
© Compendium Developments, 2014, CompendiumDev.co.uk
Waiting Approaches
●
Explicit wait
– WebDriverWait for a specific condition to occur, if it
does not occur then timeout after a certain time
– Throw an exception if the wait times out
– We wait only when we need to synchronise
●
Implicit wait
– Set a global timeout e.g. 10 seconds
– Every time we try and find an element, we will wait
for a maximum of 10 seconds
– We are blind to synchronisation
© Compendium Developments, 2014, CompendiumDev.co.uk
ImplicitWait Approach
●
Implicit wait
– .findElement has an implicit wait
– Default is 0
– Can amend the global default
driver.manage().timeouts()
.implicitlyWait(
10,
TimeUnit.SECONDS);
If an element can not be
found then a findElement
would take 10 seconds
before throwing an
Exception.
44
© Compendium Developments, 2014, CompendiumDev.co.uk
WebDriverWait Approaches
●
Explicit wait
– WebDriverWait
●
ExpectedConditions helper class
●
custom ExpectedCondition (Class or inline)
new WebDriverWait(driver,10).until(
ExpectedConditions.titleIs(
"HTML Form Elements"));
© Compendium Developments, 2014, CompendiumDev.co.uk
WebDriverWait Example@Test
public void exampleUsingExpectedConditions(){
WebDriver driver;
driver = driver.get("http://compendiumdev.co.uk/selenium/" +
"basic_html_form.html");
new WebDriverWait(driver,10).until(
ExpectedConditions.titleIs("HTML Form Elements"));
assertEquals("HTML Form Elements", driver.getTitle());
}
I don't really need
this assert because
WebDriverWait will throw a
TimeoutException if the
ExpectedCondition is not met
Construct a WebDriverWait
with the driver and
a timeout in seconds.
ExpectedConditions has a lot
of common conditions that
you would otherwise have to
code yourself.
Explore the methods
45
© Compendium Developments, 2014, CompendiumDev.co.uk
Implicit or Explicit?
●
Implicit can make initial tests faster to write
– you don't worry about synchronisation when writing
tests
●
It can be harder to add synchronisation later
– You have to identify a source of intermittency
●
If you start with implicit then you can expose
synchronisation problems by gradually reducing the
implicit wait time
●
Can make tests increasingly slower
●
Implicit can make test failures slower to report
© Compendium Developments, 2014, CompendiumDev.co.uk
Basic Synchronisation with
ExpectedConditions
●
Wait for the page to be in the correct state before working
with it
– Why? ElementNotFound exception
– More robust across browsers
●
WebDriverWait
– A wait class which handles element not found exceptions
●
ExpectedConditions
– A class of static helper methods
– e.g. new WebDriverWait(driver,10).until(
ExpectedConditions.somecondition(
By.id("user_login")));
46
© Compendium Developments, 2014, CompendiumDev.co.uk
Synchronisation API Summary
●
new WebDriverWait(driver,
seconds)
– Create a wait class
●
.until
– Wait until a specific condition
happens
– until can return an element
●
ExpectedConditions
– A class of static helper wait
methods for common scenarios
e.g.
●
.titleIs(expectedValue)
– True when title of page is
expectedValue
●
visibilityOfElementLocated
– Returns an element when
element located is visible
© Compendium Developments, 2014, CompendiumDev.co.uk
Basic Synchronisation Exercises
47
© Compendium Developments, 2014, CompendiumDev.co.uk
Section:
Custom Expected Condition
© Compendium Developments, 2014, CompendiumDev.co.uk
Custom Expected Condition
●
Warning – this section requires Java
Knowledge
●
Most people do not create custom expected
conditions, and instead use the
ExpectedConditions class
●
Learning the extra Java required to deal with
Custom Expected Conditions will improve the
readability and maintainability of your tests
48
© Compendium Developments, 2014, CompendiumDev.co.uk
Custom ExpectedCondition
new WebDriverWait(driver,10).until(
titleOfPageChangesFrom("in tutorial"));
);
●
Why?
– ExpectedConditions doesn't have what you need
– You want to make your tests read well for your
usage scenario
– You want to pass additional values to the apply
method
●
... create a Custom ExpectedCondition
© Compendium Developments, 2014, CompendiumDev.co.uk
Demo - ExpectedConditions
●
org.openqa.selenium.support.ui.
– ExpectedConditions
●
Contains static methods which return an
anonymous ExpectedCondition class
– Define the type of the 'return' value
● new ExpectedCondition<Boolean>()
– Need to write an 'apply' method
●
Which returns true, or an object to signify wait is over
●
To keep waiting return null, or false
49
© Compendium Developments, 2014, CompendiumDev.co.uk
WebDriverWait Basics
●
Create a new WebDriverWait, with a timeout and call
the 'until' method with an 'ExpectedCondition' as
argument
●
The 'until' method
– calls the 'apply' method on the ExpectedCondition
– Would stop waiting ...
●
If the 'apply' method returns true
●
If the apply method returns an object
– If still waiting then pause for 500ms then try again
– If timeout period expired then throw TimeoutException
new WebDriverWait(driver,10).until(
titleOfPageChangesFrom("in tutorial"));
);
© Compendium Developments, 2014, CompendiumDev.co.uk
Custom ExpectedCondition Example
new WebDriverWait(driver,10).until(
new SelectContainsText(By.id("combo2"),"Java")
);
50
© Compendium Developments, 2014, CompendiumDev.co.uk
Custom ExpectedCondition Example
private class SelectContainsText implements ExpectedCondition<Boolean> {
private String textToFind;
private By findBy;
public SelectContainsText(final By comboFindBy, final String textToFind) {
this.findBy = comboFindBy;
this.textToFind = textToFind;
}
@Override
public Boolean apply(WebDriver webDriver) {
WebElement combo = webDriver.findElement(this.findBy);
List<WebElement> options = combo.findElements(By.tagName("option"));
for(WebElement anOption : options){
if(anOption.getText().equals(this.textToFind))
return true;
}
return false;
}
}
I made it private because
It is local to my test, normally
this would be public
You can return anything e.g.
Boolean or WebElement.
I chose Boolean for this.
Pass in whatever you need
in the constructor
Override apply, this is
called by WebDriverWait
Implement your checking code
using the passed in WebDriver
© Compendium Developments, 2014, CompendiumDev.co.uk
Adhoc Waits Created Inline Example
new WebDriverWait(driver,10).until(
new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver webDriver) {
return webDriver.findElement(
By.cssSelector("option[value='23']"))
.isDisplayed();
}
}
);
Create ExpectedCondition
on the fly and @Override
the apply method
WebDriver automatically
passed through to
the apply method
Add your
synchronisation code in
the apply method
If not Boolean
then null == false
Anonymous classes cannot
have custom constructors
so use for simple waits.
Any other variables referenced
need to be 'final'
51
© Compendium Developments, 2014, CompendiumDev.co.uk
Wrap Adhoc Wait in a method
private ExpectedCondition<WebElement>
optionWithValueDisplayed(final String value) {
return new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver webDriver) {
return webDriver.findElement(
By.cssSelector("option[value='" + value + "']"));
}
};
}
Anonymous class wrapped
in a method can use
final params in the class.
Bypasses need
for a constructor.
Returning a WebElement
for future use.
© Compendium Developments, 2014, CompendiumDev.co.uk
Custom Expected Condition
Synchronisation Exercises
52
© Compendium Developments, 2014, CompendiumDev.co.uk
Abstraction
© Compendium Developments, 2014, CompendiumDev.co.uk
Abstraction Layers
●
Abstraction Layers are
– not a framework
– contextual for an application, or an application
framework
– Essential for:
●
Maintenance
●
Speed of writing tests
●
Ease of understanding tests
53
© Compendium Developments, 2014, CompendiumDev.co.uk
Abstraction Layers Categorised
1) Data
– Generic Data Abstractions e.g. email, postcode
2) Physical
– Physical layout of your application e.g. pages,
components
– Navigation across pages
3) Domain
– Your application Entities domain e.g. user, account
4) Logical
– User actions, workflows
© Compendium Developments, 2014, CompendiumDev.co.uk
Hints
●
Create the test you want to see
– Write the test as if the abstraction layers already
existed
– So you can:
●
See if it is readable, at the right level etc.
●
Use code completion to create classes (IntelliJ:
Alt+Enter)
54
© Compendium Developments, 2014, CompendiumDev.co.uk
Page Objects?
●
An abstraction around pages
– Or page components
●
Remove driver.xxxx from tests
●
Model the physical domain
●
Numerous ways of doing this
– Page Factory
– Extends LoadableComponent or
SlowLoadableComponent
– Basic objects
●
Modelling Decisions Required
© Compendium Developments, 2014, CompendiumDev.co.uk
Example Test Using Page Object
@Test
public void basicHtmlPageTest(){
WebDriver driver = new FirefoxDriver();
BasicHtmlPage page =
new BasicHtmlPage(driver);
page.get();
assertEquals(page.getParagraphOne(),
"A paragraph of text");
assertEquals(page.getParagraphTwo(),
"Another paragraph of text");
}
55
© Compendium Developments, 2014, CompendiumDev.co.uk
Example Page Object
public class BasicHtmlPage {
private final WebDriver driver;
private String paragraphTwo;
public BasicHtmlPage(WebDriver driver) {
this.driver = driver;
}
public void get() {
driver.get(
"http://seleniumsimplified.com/testpages/basic_web_page.html");
}
public String getParagraphOne() {
return driver.findElement(By.id("para1")).getText();
}
public String getParagraphTwo() {
return driver.findElement(By.id("para2")).getText();
}
}
© Compendium Developments, 2014, CompendiumDev.co.uk
Demo Page Objects POJO
●
demospageobjectsExamplePageObjectTest
●
Plain Old Java Object
●
Any Required Synchronisation in the methods
●
Simple
●
No Conventions
56
© Compendium Developments, 2014, CompendiumDev.co.uk
Refactor to Page Object Heuristics
●
Convert local abstractions to Page Abstractions
●
Replace comments with abstractions
●
Make Decisions about the model based on the
test readability and method reuse
●
Build only what you need as you need it
© Compendium Developments, 2014, CompendiumDev.co.uk
Page Object Heuristics
●
Construct the page object with a WebDriver
●
Limit the methods to physical actions
●
Expose Constants as public static final
●
Don't navigate to the page in the constructor,
either use a navigation object or a get()
method
57
© Compendium Developments, 2014, CompendiumDev.co.uk
Demo Refactor To Page Object
●
Convert test to use page object
●
demospageobjectsrefactorTo
– RefactorExampleToPageObjectTest
© Compendium Developments, 2014, CompendiumDev.co.uk
Page Object Factory
●
Annotate WebElements with @FindBy
@FindBy(how= How.ID, using="combo1")
private WebElement aComboElement;
●
Initialise the annotated WebElement(s) using a
PageFactory
public BasicAjaxPageObject(WebDriver webDriver) {
driver = webDriver;
PageFactory.initElements(driver, this);
}
58
© Compendium Developments, 2014, CompendiumDev.co.uk
Demo – Page Object Factor
●
Walkthrough of
●
demospageobjects
●
ExamplePageObjectTestUsingPageFactory
© Compendium Developments, 2014, CompendiumDev.co.uk
Slow Loadable Component
●
If our page objects extend SlowLoadableComponent
then we have an interface for 'waiting' for free
●
Instead of explicitly coding waits for page load:
TracksDashboardHomePage dashboard =
new TracksDashboardHomePage(driver);
new WebDriverWait(driver,10).until(
ExpectedConditions.titleIs(
dashboard.EXPECTED_TITLE));
●
We have a standard place to add conditions:
TracksDashboardHomePage dashboard =
new TracksDashboardHomePage(driver);
dashboard.get();
59
© Compendium Developments, 2014, CompendiumDev.co.uk
SlowLoadableComponent
●
Public interface
– get()
●
loads and waits for component to be available
extends SlowLoadableComponent<PageObjectClassName>
●
Call super constructor in constructor
public PageObjectClassName(WebDriver driver) {
super(new SystemClock(), 10);
this.driver = driver;
}
●
Implement load and isLoaded
– isLoaded throws a new Error if not loaded
– I often leave the 'load' part empty if I navigate to the page
© Compendium Developments, 2014, CompendiumDev.co.uk
Demo – Slow Loadable
●
Walkthrough of
●
demospageobjects
●
ExamplePageObjectTestSlowLoadable
60
© Compendium Developments, 2014, CompendiumDev.co.uk
Why do this?
●
Synchronise on load
– Page Load – don't do anything until page is loaded
– Component Load – so we don't try to engage with
the component until it is loaded
– Get in the habit of synchronising
– Interface makes it easy to extend
– 'forced' to think about synchronisation
– Encourages more comprehensive checks on
'ready'
●
No impact
– If we don't call .get() we don't trigger the wait
© Compendium Developments, 2014, CompendiumDev.co.uk
Tips
●
Sometimes I want to load a Slow Loadable
Component page, without triggering the wait
● create a getPage method, to use from the
test, and the load method
public void getPage(){
driver.get(Config.ROOT_URL +
"/basic_web_page.html");
}
@Override
protected void load() {
getPage();
}
61
© Compendium Developments, 2014, CompendiumDev.co.uk
Summary
●
Page Objects can be POJO
●
Domain Objects are POJO
●
Create abstraction layers
●
Write failing code and use code completion
●
Never add asserts into your abstraction layer
●
Write methods so they read as native
language
●
Refactor in small increments
●
Keep refactoring
© Compendium Developments, 2014, CompendiumDev.co.uk
Page Object Exercises
62
© Compendium Developments, 2014, CompendiumDev.co.uk
Bonus Sections
© Compendium Developments, 2014, CompendiumDev.co.uk
Bonus Sections
●
This section contains an overview of
'additional' sections of the WebDriver API
●
The tutorial may or may not have the time to
reach this point
63
© Compendium Developments, 2014, CompendiumDev.co.uk
Support Classes as Abstractions
Examine the WebDriver source code for
examples to build on.
e.g. org.openqa.selenium.support
●
Select
●
ExpectedConditions
© Compendium Developments, 2014, CompendiumDev.co.uk
Alerts
●
Handle Alerts with
– driver.switchTo().alert()
●
.getText()
●
.dismiss()
●
.accept()
●
.sendKeys(String)
– .alert() returns an Alert object
●
Does alert exist?
– Switch to it and catch the exception to check
– WebDriverWait ExpectedConditions.alertIsPresent
The hierarchy is
'kinda' obvious
When you think
About it.
Keep searching.
Learn the API.
64
© Compendium Developments, 2014, CompendiumDev.co.uk
User Interactions
●
new Actions(driver)
●
Actions Sequence
– .keyDown
– .keyUp
– .sendKeys
– .clickAndHold
– .release
– .click
– .doubleClick
– .moveToElement
●
.moveByOffset
●
.contextClick
●
.dragAndDrop
●
.dragAndDropBy
●
.build()
●
.perform()
May more accurately
simulate user interaction
Build if you want
to store and reuse
the action sequence,
otherwise just perform
© Compendium Developments, 2014, CompendiumDev.co.uk
More About Actions
●
Actions uses GWT – operating system mouse
move etc. so is more realistic
●
You can interfere with the test if you move
mouse and keyboard
●
Actions can be brittle
65
© Compendium Developments, 2014, CompendiumDev.co.uk
Actions to simulate user Actions
●
Hover, etc.
●
new Actions(driver).
ChainOfActions.
build().perform();
●
e.g. a chain of click actions
Actions actions = new Actions(driver);
actions.click(multiSelectOptions.get(0)).
click(multiSelectOptions.get(1)).
click(multiSelectOptions.get(2)).perform();
© Compendium Developments, 2014, CompendiumDev.co.uk
Actions API Summary Slides
Note: I try and
avoid actions
66
© Compendium Developments, 2014, CompendiumDev.co.uk
Cookies
© Compendium Developments, 2014, CompendiumDev.co.uk
Cookies
●
Inspect
– driver.manage
●
.getCookies()
●
.getCookieNamed(“name”)
●
Interact
– driver.manage
●
.addCookie(Cookie)
●
.deleteAllCookies
●
.deleteCookie(Cookie)
●
.deleteCookieNamed(“name”)
67
© Compendium Developments, 2014, CompendiumDev.co.uk
Cookies Example
@Test
public void visitSearchPageAndCheckNoLastSearchCookie(){
WebDriver driver;
driver = Driver.get("http://compendiumdev.co.uk/selenium/search.php");
driver.manage().deleteAllCookies();
driver.navigate().refresh();
Cookie aCookie = driver.manage().getCookieNamed("SeleniumSimplifiedLastSearch");
assertEquals("Should be no last search cookie", null, aCookie);
}
Delete all cookies
for current domain
Refresh page to get
an initial set of
cookies
Find a named cookie
Return null if not found
© Compendium Developments, 2014, CompendiumDev.co.uk
Cookie.Builder
●
Can use constructor
– new Cookie
●
Or can use Cookie.Builder
Cookie aNewCookie = new Cookie.Builder(
aCookie.getName(),
String.valueOf(2)).
domain(aCookie.getDomain()).
path( aCookie.getPath()).
expiresOn(aCookie.getExpiry()).
isSecure(aCookie.isSecure()).
build();
68
© Compendium Developments, 2014, CompendiumDev.co.uk
JavaScript
© Compendium Developments, 2014, CompendiumDev.co.uk
Why use Javascript?
●
Workarounds
●
Custom synchronisation
●
Make the app more testable
– Adjust hidden fields
– Amend values
●
Simulate hard to reach conditions
●
Test Flash & HTML 5
69
© Compendium Developments, 2014, CompendiumDev.co.uk
Javascript Execution
●
Cast WebDriver to JavascriptExecutor
– .executeScript(script, args...)
– .executeAsyncScript(script, args...)
●
Arguments are accessed using
– arguments[index] e.g. "document.title=arguments[0]"
●
Return values are converted to Java types
– Html Element = WebElement, decimal = Double, non-
decimal = Long, boolean = Boolean, array =
List<Object>, else String or null
●
Runs in an anonymous function
© Compendium Developments, 2014, CompendiumDev.co.uk
(JavascriptExecutor) Example
@Test
public void callAJavaScriptFunctionOnThePage(){
WebDriver driver = Driver.get(
"http://www.compendiumdev.co.uk/selenium/canvas_basic.html");
JavascriptExecutor js =(JavascriptExecutor)driver;
int actionsCount = driver.findElements(
By.cssSelector("#commandlist li")).size();
assertEquals("By default app has 2 actions listed",
2, actionsCount);
js.executeScript("draw(1, 150,150,40, '#FF1C0A');");
actionsCount = driver.findElements(
By.cssSelector("#commandlist li")).size();
assertEquals("Calling draw should add an action",
3, actionsCount);
}
Cast driver to
JavascriptExecutor
to access the
JavaScript methods
Test page is at
compendiumdev.co.uk/
selenium/canvas_basic.html
Execute the 'draw'
Function in the page
70
© Compendium Developments, 2014, CompendiumDev.co.uk
Introducing Different
Browsers
© Compendium Developments, 2014, CompendiumDev.co.uk
Browsers Overview
●
Firefox Driver – currently built in
●
HtmlUnit Driver – currently built in
●
ChromeDriver – separate .exe
●
OperaDriver – separate .exe available through
maven, does not support current Opera version
●
IEDriver – separate .exe IE10 and below
●
RemoteDriver – currently built in, requires a
server to connect to e.g. saucelabs or grid
●
Various mobile drivers and safari driver
71
© Compendium Developments, 2014, CompendiumDev.co.uk
Firefox Driver
●
Currently part of deployed Jars
– Effectively 'built in'
– Easy to get started with
– Can be slow to startup
aDriver = new FirefoxDriver();
© Compendium Developments, 2014, CompendiumDev.co.uk
Firefox Driver Profile
●
Driver Profiles allow us to initialise a driver with
additional capabilities and specific configuration
FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(profile);
●
Profile level methods
profile.setEnableNativeEvents(true);
●
Set browser preferences
profile.setPreference("extensions.firebug.currentVersion", "1.6.2");
●
Load extensions
profile.addExtension(new File(extensionPath));
72
© Compendium Developments, 2014, CompendiumDev.co.uk
FirefoxDriver Useful Links
●
Firefox Preferences
– about:config
– http://www.timeatlas.com/5_minute_tips/general/intr
oduction_to_firefox_preferences#.UIvbL4az728
●
Firefox Driver Documentation
– http://code.google.com/p/selenium/wiki/FirefoxDriver
© Compendium Developments, 2014, CompendiumDev.co.uk
Browser Capabilities
●
Generic browser control mechanism
●
e.g. Set Proxies
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy("localhost:8080")
.setFtpProxy("localhost:8080")
.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
73
© Compendium Developments, 2014, CompendiumDev.co.uk
HtmlUnitDriver
●
http://htmlunit.sourceforge.net/
●
DesiredCapabilities.setJavascriptEnabled
seems to have been added for HTMLUnit
WebDriver htmlunit =
new HtmlUnitDriver(true);
© Compendium Developments, 2014, CompendiumDev.co.uk
ChromeDriver
●
http://code.google.com/p/selenium/wiki/ChromeDriver
●
Download the driver server
– set "webdriver.chrome.driver" to the location
●
Command line switches
– http://peter.sh/experiments/chromium-command-
line-switches/
– Pass in via options.addArguments
●
ChromeDriver.log is useful debugging tool
74
© Compendium Developments, 2014, CompendiumDev.co.uk
ChromeDriver Example
String currentDir = System.getProperty("user.dir");
String chromeDriverLocation =
currentDir + "/../tools/chromedriver/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",
chromeDriverLocation);
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("disable-plugins");
options.addArguments("disable-extensions");
aDriver = new ChromeDriver(options);
Calculate the location of
Chromedriver relative
to my test code
Set the property
so that the driver
can be found
Disable the plugins
and extensions to
prevent them
interfering with the
testsGet the driver
© Compendium Developments, 2014, CompendiumDev.co.uk
Opera Driver
●
Documentation and Downloads
– http://code.google.com/p/selenium/wiki/OperaDriver
– https://github.com/operasoftware/operadriver
– http://mvnrepository.com/artifact/com.opera/operadriver
●
Add to maven
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
<version>1.1</version>
</dependency>
●
Does not support modern Opera version
75
© Compendium Developments, 2014, CompendiumDev.co.uk
Config Opera Driver
●
Capabilities
●
OperaProfile
– opera:config
– http://www.opera.com/support/usingopera/operaini
OperaProfile profile = new OperaProfile();
// switching off Javascript will cause the opera driver to fail
profile.preferences().set("Extensions", "Scripting", 0);
WebDriver opera = new OperaDriver(profile);
© Compendium Developments, 2014, CompendiumDev.co.uk
IE Driver
●
http://code.google.com/p/selenium/wiki/InternetExplorerDriver
●
Download the server executable
– http://code.google.com/p/selenium/downloads/list
– Set “webdriver.ie.driver” to the location of the driver
executable
String currentUserDir = System.getProperty("user.dir");
String IEDriverLocation = currentUserDir +
"/../tools/iedriver_32/IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", IEDriverLocation);
aDriver = new InternetExplorerDriver();
76
© Compendium Developments, 2014, CompendiumDev.co.uk
IE Driver on Path
●
Drivers change
●
IE Driver used to be add to path
●
This method will be deprecated in favour of
properties
●
Pointing this out because we have to learn to
read the code and the log messages output by
Selenium
© Compendium Developments, 2014, CompendiumDev.co.uk
RemoteWebDriver
77
© Compendium Developments, 2014, CompendiumDev.co.uk
Remote Driver
●
When server is running on another machine
●
e.g. SauceLabs.com
DesiredCapabilities capabilities;
capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("version", "5");
capabilities.setCapability("platform", Platform.XP);
try {
String sauceURL = System.getenv("SAUCELABS_URL");
aDriver = new RemoteWebDriver(
new URL(sauceURL),
capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Remote driver
configured by
capabilities
Watch out for
UnsupportedCommandException
during your tests
© Compendium Developments, 2014, CompendiumDev.co.uk
RemoteWebDriver Profiles
●
Can set firefox profiles on RemoteWebDriver
FirefoxProfile fp = new FirefoxProfile();
// set something on the profile...
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, fp);
WebDriver driver = new RemoteWebDriver(dc);
●
Can set firefox profiles on RemoteWebDriver
●
Can set Chrome options on RemoteWebDriver
ChromeOptions options = new ChromeOptions();
// set some options
DesiredCapabilities dc = DesiredCapabilities.chrome();
dc.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(dc);
78
© Compendium Developments, 2014, CompendiumDev.co.uk
Grid Systems
●
saucelabs.com
●
browserstack.com
●
Sign up for free
●
Free plan good enough for this course, and a
lot of things
●
Records video
●
Manual Testing
© Compendium Developments, 2014, CompendiumDev.co.uk
Section: Any Time Left?
79
© Compendium Developments, 2014, CompendiumDev.co.uk
Any Questions?
© Compendium Developments, 2014, CompendiumDev.co.uk
Time for Free For All Automation?
●
Pick a section and try and automated it
●
Refactor some of the code into page objects or
domain objects
●
Try generating test data
●
Try some of the advanced exercises
80
© Compendium Developments, 2014, CompendiumDev.co.uk
Reference Section
© Compendium Developments, 2014, CompendiumDev.co.uk
Basic Practice Pages
●
http://SeleniumSimplified.com/testpages/
– alert.html
– basic_ajax.html
– basic_web_page.html
– basic_html_form.html
– ajaxselect.php
●
Source available at
https://github.com/eviltester/seleniumTestPages
– calculate.php
– find_by_playground.php
– refresh.php
– search.php
– etc. (see index.html)
81
© Compendium Developments, 2014, CompendiumDev.co.uk
Advanced Practice Pages
●
compendiumdev.co.uk/selenium/Showcase/Showcase.html
– http://compendiumdev.co.uk/selenium/Showcase/Showcase.html
●
Source available at
https://github.com/eviltester/simpleGWTShowCaseClone
© Compendium Developments, 2014, CompendiumDev.co.uk
Recommended Applications For
Practice Overview
●
Intermediate:
– http://todomvc.com/
●
Advanced
– http://google-gruyere.appspot.com/
– http://demo.redmine.org/
– http://getontracks.org/
82
© Compendium Developments, 2014, CompendiumDev.co.uk
Learn More Java
●
Java For Testers
– by Alan Richardson
●
Effective Java
– by Joshua Bloch
●
Implementation Patterns
– by Kent Beck
●
Growing Object-Oriented Software, Guided by
Tests
– by Steve Freeman and Nat Pryce
© Compendium Developments, 2014, CompendiumDev.co.uk
Live pages to challenge yourself
gwt.google.com/samples/Showcase/Showcase.html
GWT demo application – good for synchronisation
and location practice
http://www.primefaces.org/showcase/
Primfaces demo application – good for
synchronisation and location practice. Also check
out the source as they use WebDriver for their
testing.
83
165
WebDriver Summary Sheet
WebElement
.getText()
.getAttribute(“name”)
.getTagName()
.isEnabled()
.isSelected()
.isDisplayed()
.getSize()
.getLocation()
.getCssValue()
Finding elements
WebDriver
WebElement =
.findElement(BY)
List<WebElement> =
.findElements(BY)
By
.id(“an_id”)
.xpath(“xpath”)
.cssSelector(“css”)
.className(“class”)
.linkText(“text”)
.name(“name”
.tagName(“a_tag”)
.partialLinkText(“t”);
Support
ByChaining(By, By)
ByIdOrName(“idName”)
Inspect
Navigate
WebDriver
.get(“URL”)
.navigate
.to(Url)
.to(“URL”)
.back()
.forward()
.refresh()
WebDriver
.getTitle()
.getCurrentUrl()
.getPageSource()
WebDriver Interact
= new <driverClass>()
.close()
.quit() Interact
WebElement
.click()
.submit()
.clear()
.sendKeys(String)
.sendKeys(Keys.x)
support.ui.Select
.<methods>()
SwitchTo.
.alert()
.getText()
.accept()
.dismiss()
.sendKeys(String)
.frame(...)
ExpectedConditions
.titleIs(String)
… a lot of helper methods
WebDriverWait
(driver, timeout in Seconds)
.until(ExpectedCondition)
Cookies
WebDriver
.manage()
.deleteAllCookies()
.addCookie(Cookie)
.deleteCookie(Cookie)
.deleteCookieNamed(String)
Synchronise
Cookies
WebDriver
.manage()
.getCookieNamed(String)
.getCookies()
Actions
.keyUp() etc.
.perform()
(JavascriptExecutor)
.executeScript
166
Selectors CSS Selectors
* - any
#id
.class
tag
[attribute]
[attribute="value"]
tag[attribute="value"]
tag[attr1='val1'][attr2='val2']
tag[att1='val1'], orThisTag
= (' or ")
*="anywhere in value"
^="start of value"
$="end of value"
~="spaceSeperatedValue"
Paths
A > B (child)
A B (any descendant)
A + B (A's B sibling)
tag:first-child
XPATH Selectors
// - match anywhere
/ - match from root
//* - any element
//tag
//*[@attribute]
//*[@attribute="value"]
//tag[@attribute="value"]
//tag1/tag2 (child)
//tag1//tag2 (any descendant)
//tag1/../.. (.. to go up)
//*[.='element textl]
[@at1="a" and @at2="b"]
and, or, =, !=, <, >, >=, <-
Functions
contains(@a,"alu")
starts-with(@a,"v")
ends-with(@a,"e")
Indexing
//tag[1]
XPath References
●
http://www.w3schools.com/xpath/
●
http://www.simple-talk.com/dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/
CSS References
●
http://reference.sitepoint.com/css/selectorref
●
http://net.tutsplus.com/tutorials/html-css-techniques/
the-30-css-selectors-you-must-memorize/
●
http://www.quirksmode.org/css/contents.html
●
http://www.w3schools.com/cssref/css_selectors.asp
84

More Related Content

What's hot

Integrating Selenium testing infrastructure into Scala Project
Integrating Selenium testing infrastructure into Scala ProjectIntegrating Selenium testing infrastructure into Scala Project
Integrating Selenium testing infrastructure into Scala ProjectKnoldus Inc.
 
Apache Cordova In Action
Apache Cordova In ActionApache Cordova In Action
Apache Cordova In ActionHazem Saleh
 
An Introduction to Maven Part 1
An Introduction to Maven Part 1An Introduction to Maven Part 1
An Introduction to Maven Part 1MD Sayem Ahmed
 
[Devoxx Morocco 2015] Apache Cordova In Action
[Devoxx Morocco 2015] Apache Cordova In Action[Devoxx Morocco 2015] Apache Cordova In Action
[Devoxx Morocco 2015] Apache Cordova In ActionHazem Saleh
 
Selenium_WebDriver_Java_TestNG
Selenium_WebDriver_Java_TestNGSelenium_WebDriver_Java_TestNG
Selenium_WebDriver_Java_TestNGBasul Asahab
 
Efficient JavaScript Unit Testing, JavaOne China 2013
Efficient JavaScript Unit Testing, JavaOne China 2013Efficient JavaScript Unit Testing, JavaOne China 2013
Efficient JavaScript Unit Testing, JavaOne China 2013Hazem Saleh
 
Vuejs getting-started - Extended Version
Vuejs getting-started - Extended VersionVuejs getting-started - Extended Version
Vuejs getting-started - Extended VersionMurat Doğan
 
Spark IT 2011 - Java EE 6 Workshop
Spark IT 2011 - Java EE 6 WorkshopSpark IT 2011 - Java EE 6 Workshop
Spark IT 2011 - Java EE 6 WorkshopArun Gupta
 
Testing Java Web Apps With Selenium
Testing Java Web Apps With SeleniumTesting Java Web Apps With Selenium
Testing Java Web Apps With SeleniumMarakana Inc.
 
Micronaut: A new way to build microservices
Micronaut: A new way to build microservicesMicronaut: A new way to build microservices
Micronaut: A new way to build microservicesLuram Archanjo
 
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13Fred Sauer
 
Testing Java EE apps with Arquillian
Testing Java EE apps with ArquillianTesting Java EE apps with Arquillian
Testing Java EE apps with ArquillianIvan Ivanov
 
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019Matt Raible
 
[AnDevCon 2016] Mutation Testing for Android
[AnDevCon 2016] Mutation Testing for Android[AnDevCon 2016] Mutation Testing for Android
[AnDevCon 2016] Mutation Testing for AndroidHazem Saleh
 

What's hot (20)

Maven
MavenMaven
Maven
 
Integrating Selenium testing infrastructure into Scala Project
Integrating Selenium testing infrastructure into Scala ProjectIntegrating Selenium testing infrastructure into Scala Project
Integrating Selenium testing infrastructure into Scala Project
 
Apache Cordova In Action
Apache Cordova In ActionApache Cordova In Action
Apache Cordova In Action
 
An Introduction to Maven Part 1
An Introduction to Maven Part 1An Introduction to Maven Part 1
An Introduction to Maven Part 1
 
Introduction to maven
Introduction to mavenIntroduction to maven
Introduction to maven
 
Maven tutorial
Maven tutorialMaven tutorial
Maven tutorial
 
[Devoxx Morocco 2015] Apache Cordova In Action
[Devoxx Morocco 2015] Apache Cordova In Action[Devoxx Morocco 2015] Apache Cordova In Action
[Devoxx Morocco 2015] Apache Cordova In Action
 
Apache Maven
Apache MavenApache Maven
Apache Maven
 
Selenium_WebDriver_Java_TestNG
Selenium_WebDriver_Java_TestNGSelenium_WebDriver_Java_TestNG
Selenium_WebDriver_Java_TestNG
 
Efficient JavaScript Unit Testing, JavaOne China 2013
Efficient JavaScript Unit Testing, JavaOne China 2013Efficient JavaScript Unit Testing, JavaOne China 2013
Efficient JavaScript Unit Testing, JavaOne China 2013
 
Vuejs getting-started - Extended Version
Vuejs getting-started - Extended VersionVuejs getting-started - Extended Version
Vuejs getting-started - Extended Version
 
Spark IT 2011 - Java EE 6 Workshop
Spark IT 2011 - Java EE 6 WorkshopSpark IT 2011 - Java EE 6 Workshop
Spark IT 2011 - Java EE 6 Workshop
 
Introduction to Maven
Introduction to MavenIntroduction to Maven
Introduction to Maven
 
Demystifying Maven
Demystifying MavenDemystifying Maven
Demystifying Maven
 
Testing Java Web Apps With Selenium
Testing Java Web Apps With SeleniumTesting Java Web Apps With Selenium
Testing Java Web Apps With Selenium
 
Micronaut: A new way to build microservices
Micronaut: A new way to build microservicesMicronaut: A new way to build microservices
Micronaut: A new way to build microservices
 
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
SF JUG - GWT Can Help You Create Amazing Apps - 2009-10-13
 
Testing Java EE apps with Arquillian
Testing Java EE apps with ArquillianTesting Java EE apps with Arquillian
Testing Java EE apps with Arquillian
 
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019
 
[AnDevCon 2016] Mutation Testing for Android
[AnDevCon 2016] Mutation Testing for Android[AnDevCon 2016] Mutation Testing for Android
[AnDevCon 2016] Mutation Testing for Android
 

Viewers also liked

Security Testing for Test Professionals
Security Testing for Test ProfessionalsSecurity Testing for Test Professionals
Security Testing for Test ProfessionalsTechWell
 
Meeting Strict Documentation Requirements in Agile
Meeting Strict Documentation Requirements in AgileMeeting Strict Documentation Requirements in Agile
Meeting Strict Documentation Requirements in AgileTechWell
 
Top Ten Attacks to Break Mobile Apps
Top Ten Attacks to Break Mobile AppsTop Ten Attacks to Break Mobile Apps
Top Ten Attacks to Break Mobile AppsTechWell
 
Software Managers: Their Place in Agile
Software Managers: Their Place in AgileSoftware Managers: Their Place in Agile
Software Managers: Their Place in AgileTechWell
 
A Very Large Enterprise Agile Transformation: Lessons Learned at Salesforce
A Very Large Enterprise Agile Transformation: Lessons Learned at SalesforceA Very Large Enterprise Agile Transformation: Lessons Learned at Salesforce
A Very Large Enterprise Agile Transformation: Lessons Learned at SalesforceTechWell
 
Scaling Git for the Enterprise
Scaling Git for the EnterpriseScaling Git for the Enterprise
Scaling Git for the EnterpriseTechWell
 
Speak Like a Test Manager
Speak Like a Test ManagerSpeak Like a Test Manager
Speak Like a Test ManagerTechWell
 
Can We Do Agile? Barriers to Agile Adoption
Can We Do Agile? Barriers to Agile AdoptionCan We Do Agile? Barriers to Agile Adoption
Can We Do Agile? Barriers to Agile AdoptionTechWell
 
Pairwise Testing Explained
Pairwise Testing ExplainedPairwise Testing Explained
Pairwise Testing ExplainedTechWell
 
Test Estimation in Practice
Test Estimation in PracticeTest Estimation in Practice
Test Estimation in PracticeTechWell
 
Assessing Agile Engineering Practices
Assessing Agile Engineering PracticesAssessing Agile Engineering Practices
Assessing Agile Engineering PracticesTechWell
 
Using DevOps to Improve Software Quality in the Cloud
Using DevOps to Improve Software Quality in the CloudUsing DevOps to Improve Software Quality in the Cloud
Using DevOps to Improve Software Quality in the CloudTechWell
 
Introducing Keyword-Driven Test Automation
Introducing Keyword-Driven Test AutomationIntroducing Keyword-Driven Test Automation
Introducing Keyword-Driven Test AutomationTechWell
 
Simplify Project and Portfolio Planning with “Real Options”
Simplify Project and Portfolio Planning with “Real Options”Simplify Project and Portfolio Planning with “Real Options”
Simplify Project and Portfolio Planning with “Real Options”TechWell
 
Test Automation Patterns: Issues and Solutions
Test Automation Patterns: Issues and SolutionsTest Automation Patterns: Issues and Solutions
Test Automation Patterns: Issues and SolutionsTechWell
 
Measurement and Metrics for Test Managers
Measurement and Metrics for Test ManagersMeasurement and Metrics for Test Managers
Measurement and Metrics for Test ManagersTechWell
 
End-to-End Testing with the Heuristic Software Test Model
End-to-End Testing with the Heuristic Software Test ModelEnd-to-End Testing with the Heuristic Software Test Model
End-to-End Testing with the Heuristic Software Test ModelTechWell
 

Viewers also liked (17)

Security Testing for Test Professionals
Security Testing for Test ProfessionalsSecurity Testing for Test Professionals
Security Testing for Test Professionals
 
Meeting Strict Documentation Requirements in Agile
Meeting Strict Documentation Requirements in AgileMeeting Strict Documentation Requirements in Agile
Meeting Strict Documentation Requirements in Agile
 
Top Ten Attacks to Break Mobile Apps
Top Ten Attacks to Break Mobile AppsTop Ten Attacks to Break Mobile Apps
Top Ten Attacks to Break Mobile Apps
 
Software Managers: Their Place in Agile
Software Managers: Their Place in AgileSoftware Managers: Their Place in Agile
Software Managers: Their Place in Agile
 
A Very Large Enterprise Agile Transformation: Lessons Learned at Salesforce
A Very Large Enterprise Agile Transformation: Lessons Learned at SalesforceA Very Large Enterprise Agile Transformation: Lessons Learned at Salesforce
A Very Large Enterprise Agile Transformation: Lessons Learned at Salesforce
 
Scaling Git for the Enterprise
Scaling Git for the EnterpriseScaling Git for the Enterprise
Scaling Git for the Enterprise
 
Speak Like a Test Manager
Speak Like a Test ManagerSpeak Like a Test Manager
Speak Like a Test Manager
 
Can We Do Agile? Barriers to Agile Adoption
Can We Do Agile? Barriers to Agile AdoptionCan We Do Agile? Barriers to Agile Adoption
Can We Do Agile? Barriers to Agile Adoption
 
Pairwise Testing Explained
Pairwise Testing ExplainedPairwise Testing Explained
Pairwise Testing Explained
 
Test Estimation in Practice
Test Estimation in PracticeTest Estimation in Practice
Test Estimation in Practice
 
Assessing Agile Engineering Practices
Assessing Agile Engineering PracticesAssessing Agile Engineering Practices
Assessing Agile Engineering Practices
 
Using DevOps to Improve Software Quality in the Cloud
Using DevOps to Improve Software Quality in the CloudUsing DevOps to Improve Software Quality in the Cloud
Using DevOps to Improve Software Quality in the Cloud
 
Introducing Keyword-Driven Test Automation
Introducing Keyword-Driven Test AutomationIntroducing Keyword-Driven Test Automation
Introducing Keyword-Driven Test Automation
 
Simplify Project and Portfolio Planning with “Real Options”
Simplify Project and Portfolio Planning with “Real Options”Simplify Project and Portfolio Planning with “Real Options”
Simplify Project and Portfolio Planning with “Real Options”
 
Test Automation Patterns: Issues and Solutions
Test Automation Patterns: Issues and SolutionsTest Automation Patterns: Issues and Solutions
Test Automation Patterns: Issues and Solutions
 
Measurement and Metrics for Test Managers
Measurement and Metrics for Test ManagersMeasurement and Metrics for Test Managers
Measurement and Metrics for Test Managers
 
End-to-End Testing with the Heuristic Software Test Model
End-to-End Testing with the Heuristic Software Test ModelEnd-to-End Testing with the Heuristic Software Test Model
End-to-End Testing with the Heuristic Software Test Model
 

Similar to Introduction to Selenium and WebDriver

Hands On with Selenium and WebDriver
Hands On with Selenium and WebDriverHands On with Selenium and WebDriver
Hands On with Selenium and WebDriverTechWell
 
Selenium Testing with TestingBot.com
Selenium Testing with TestingBot.comSelenium Testing with TestingBot.com
Selenium Testing with TestingBot.comtestingbot
 
Android UI Testing with Appium
Android UI Testing with AppiumAndroid UI Testing with Appium
Android UI Testing with AppiumLuke Maung
 
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-JupiterToolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-JupiterBoni García
 
前端網頁自動測試
前端網頁自動測試 前端網頁自動測試
前端網頁自動測試 政億 林
 
Getting up and running with selenium for automated Code palousa
Getting up and running with selenium for automated  Code palousaGetting up and running with selenium for automated  Code palousa
Getting up and running with selenium for automated Code palousaEmma Armstrong
 
Automated Testing using JavaScript
Automated Testing using JavaScriptAutomated Testing using JavaScript
Automated Testing using JavaScriptSimon Guest
 
Best java automation training institute in Bangalore - Selenium Labs
Best java automation training institute in Bangalore - Selenium Labs Best java automation training institute in Bangalore - Selenium Labs
Best java automation training institute in Bangalore - Selenium Labs Selenium Labs
 
Run Selenium Tests With Microsoft Test Manager
Run Selenium Tests With Microsoft Test ManagerRun Selenium Tests With Microsoft Test Manager
Run Selenium Tests With Microsoft Test ManagerDaniel Herken
 
Using protractor to build automated ui tests
Using protractor to build automated ui testsUsing protractor to build automated ui tests
Using protractor to build automated ui tests🌱 Dale Spoonemore
 
Automated Web Testing using JavaScript
Automated Web Testing using JavaScriptAutomated Web Testing using JavaScript
Automated Web Testing using JavaScriptSimon Guest
 
Get Started With Selenium 3 and Selenium 3 Grid
Get Started With Selenium 3 and Selenium 3 GridGet Started With Selenium 3 and Selenium 3 Grid
Get Started With Selenium 3 and Selenium 3 GridDaniel Herken
 
Selenium WebDriver Tutorial For Beginners | What Is Selenium WebDriver | Sele...
Selenium WebDriver Tutorial For Beginners | What Is Selenium WebDriver | Sele...Selenium WebDriver Tutorial For Beginners | What Is Selenium WebDriver | Sele...
Selenium WebDriver Tutorial For Beginners | What Is Selenium WebDriver | Sele...Edureka!
 
Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...
Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...
Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...Edureka!
 
Developing your own OpenStack Swift middleware
Developing your own OpenStack Swift middlewareDeveloping your own OpenStack Swift middleware
Developing your own OpenStack Swift middlewareChristian Schwede
 
Testing with Spring 4.x
Testing with Spring 4.xTesting with Spring 4.x
Testing with Spring 4.xSam Brannen
 

Similar to Introduction to Selenium and WebDriver (20)

Hands On with Selenium and WebDriver
Hands On with Selenium and WebDriverHands On with Selenium and WebDriver
Hands On with Selenium and WebDriver
 
Selenium Testing with TestingBot.com
Selenium Testing with TestingBot.comSelenium Testing with TestingBot.com
Selenium Testing with TestingBot.com
 
Android UI Testing with Appium
Android UI Testing with AppiumAndroid UI Testing with Appium
Android UI Testing with Appium
 
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-JupiterToolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
Toolbox for Selenium Tests in Java: WebDriverManager and Selenium-Jupiter
 
前端網頁自動測試
前端網頁自動測試 前端網頁自動測試
前端網頁自動測試
 
Selenium using C# by Yogesh Kumar
Selenium using C# by  Yogesh KumarSelenium using C# by  Yogesh Kumar
Selenium using C# by Yogesh Kumar
 
Automated ui-testing
Automated ui-testingAutomated ui-testing
Automated ui-testing
 
Getting up and running with selenium for automated Code palousa
Getting up and running with selenium for automated  Code palousaGetting up and running with selenium for automated  Code palousa
Getting up and running with selenium for automated Code palousa
 
Automated Testing using JavaScript
Automated Testing using JavaScriptAutomated Testing using JavaScript
Automated Testing using JavaScript
 
Best java automation training institute in Bangalore - Selenium Labs
Best java automation training institute in Bangalore - Selenium Labs Best java automation training institute in Bangalore - Selenium Labs
Best java automation training institute in Bangalore - Selenium Labs
 
Run Selenium Tests With Microsoft Test Manager
Run Selenium Tests With Microsoft Test ManagerRun Selenium Tests With Microsoft Test Manager
Run Selenium Tests With Microsoft Test Manager
 
Using protractor to build automated ui tests
Using protractor to build automated ui testsUsing protractor to build automated ui tests
Using protractor to build automated ui tests
 
Automated Web Testing using JavaScript
Automated Web Testing using JavaScriptAutomated Web Testing using JavaScript
Automated Web Testing using JavaScript
 
Get Started With Selenium 3 and Selenium 3 Grid
Get Started With Selenium 3 and Selenium 3 GridGet Started With Selenium 3 and Selenium 3 Grid
Get Started With Selenium 3 and Selenium 3 Grid
 
Selenium WebDriver Tutorial For Beginners | What Is Selenium WebDriver | Sele...
Selenium WebDriver Tutorial For Beginners | What Is Selenium WebDriver | Sele...Selenium WebDriver Tutorial For Beginners | What Is Selenium WebDriver | Sele...
Selenium WebDriver Tutorial For Beginners | What Is Selenium WebDriver | Sele...
 
Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...
Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...
Selenium Tutorial For Beginners | What Is Selenium? | Selenium Automation Tes...
 
Developing your own OpenStack Swift middleware
Developing your own OpenStack Swift middlewareDeveloping your own OpenStack Swift middleware
Developing your own OpenStack Swift middleware
 
Selenium
SeleniumSelenium
Selenium
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Testing with Spring 4.x
Testing with Spring 4.xTesting with Spring 4.x
Testing with Spring 4.x
 

More from TechWell

Failing and Recovering
Failing and RecoveringFailing and Recovering
Failing and RecoveringTechWell
 
Instill a DevOps Testing Culture in Your Team and Organization
Instill a DevOps Testing Culture in Your Team and Organization Instill a DevOps Testing Culture in Your Team and Organization
Instill a DevOps Testing Culture in Your Team and Organization TechWell
 
Test Design for Fully Automated Build Architecture
Test Design for Fully Automated Build ArchitectureTest Design for Fully Automated Build Architecture
Test Design for Fully Automated Build ArchitectureTechWell
 
System-Level Test Automation: Ensuring a Good Start
System-Level Test Automation: Ensuring a Good StartSystem-Level Test Automation: Ensuring a Good Start
System-Level Test Automation: Ensuring a Good StartTechWell
 
Build Your Mobile App Quality and Test Strategy
Build Your Mobile App Quality and Test StrategyBuild Your Mobile App Quality and Test Strategy
Build Your Mobile App Quality and Test StrategyTechWell
 
Testing Transformation: The Art and Science for Success
Testing Transformation: The Art and Science for SuccessTesting Transformation: The Art and Science for Success
Testing Transformation: The Art and Science for SuccessTechWell
 
Implement BDD with Cucumber and SpecFlow
Implement BDD with Cucumber and SpecFlowImplement BDD with Cucumber and SpecFlow
Implement BDD with Cucumber and SpecFlowTechWell
 
Develop WebDriver Automated Tests—and Keep Your Sanity
Develop WebDriver Automated Tests—and Keep Your SanityDevelop WebDriver Automated Tests—and Keep Your Sanity
Develop WebDriver Automated Tests—and Keep Your SanityTechWell
 
Eliminate Cloud Waste with a Holistic DevOps Strategy
Eliminate Cloud Waste with a Holistic DevOps StrategyEliminate Cloud Waste with a Holistic DevOps Strategy
Eliminate Cloud Waste with a Holistic DevOps StrategyTechWell
 
Transform Test Organizations for the New World of DevOps
Transform Test Organizations for the New World of DevOpsTransform Test Organizations for the New World of DevOps
Transform Test Organizations for the New World of DevOpsTechWell
 
The Fourth Constraint in Project Delivery—Leadership
The Fourth Constraint in Project Delivery—LeadershipThe Fourth Constraint in Project Delivery—Leadership
The Fourth Constraint in Project Delivery—LeadershipTechWell
 
Resolve the Contradiction of Specialists within Agile Teams
Resolve the Contradiction of Specialists within Agile TeamsResolve the Contradiction of Specialists within Agile Teams
Resolve the Contradiction of Specialists within Agile TeamsTechWell
 
Pin the Tail on the Metric: A Field-Tested Agile Game
Pin the Tail on the Metric: A Field-Tested Agile GamePin the Tail on the Metric: A Field-Tested Agile Game
Pin the Tail on the Metric: A Field-Tested Agile GameTechWell
 
Agile Performance Holarchy (APH)—A Model for Scaling Agile Teams
Agile Performance Holarchy (APH)—A Model for Scaling Agile TeamsAgile Performance Holarchy (APH)—A Model for Scaling Agile Teams
Agile Performance Holarchy (APH)—A Model for Scaling Agile TeamsTechWell
 
A Business-First Approach to DevOps Implementation
A Business-First Approach to DevOps ImplementationA Business-First Approach to DevOps Implementation
A Business-First Approach to DevOps ImplementationTechWell
 
Databases in a Continuous Integration/Delivery Process
Databases in a Continuous Integration/Delivery ProcessDatabases in a Continuous Integration/Delivery Process
Databases in a Continuous Integration/Delivery ProcessTechWell
 
Mobile Testing: What—and What Not—to Automate
Mobile Testing: What—and What Not—to AutomateMobile Testing: What—and What Not—to Automate
Mobile Testing: What—and What Not—to AutomateTechWell
 
Cultural Intelligence: A Key Skill for Success
Cultural Intelligence: A Key Skill for SuccessCultural Intelligence: A Key Skill for Success
Cultural Intelligence: A Key Skill for SuccessTechWell
 
Turn the Lights On: A Power Utility Company's Agile Transformation
Turn the Lights On: A Power Utility Company's Agile TransformationTurn the Lights On: A Power Utility Company's Agile Transformation
Turn the Lights On: A Power Utility Company's Agile TransformationTechWell
 

More from TechWell (20)

Failing and Recovering
Failing and RecoveringFailing and Recovering
Failing and Recovering
 
Instill a DevOps Testing Culture in Your Team and Organization
Instill a DevOps Testing Culture in Your Team and Organization Instill a DevOps Testing Culture in Your Team and Organization
Instill a DevOps Testing Culture in Your Team and Organization
 
Test Design for Fully Automated Build Architecture
Test Design for Fully Automated Build ArchitectureTest Design for Fully Automated Build Architecture
Test Design for Fully Automated Build Architecture
 
System-Level Test Automation: Ensuring a Good Start
System-Level Test Automation: Ensuring a Good StartSystem-Level Test Automation: Ensuring a Good Start
System-Level Test Automation: Ensuring a Good Start
 
Build Your Mobile App Quality and Test Strategy
Build Your Mobile App Quality and Test StrategyBuild Your Mobile App Quality and Test Strategy
Build Your Mobile App Quality and Test Strategy
 
Testing Transformation: The Art and Science for Success
Testing Transformation: The Art and Science for SuccessTesting Transformation: The Art and Science for Success
Testing Transformation: The Art and Science for Success
 
Implement BDD with Cucumber and SpecFlow
Implement BDD with Cucumber and SpecFlowImplement BDD with Cucumber and SpecFlow
Implement BDD with Cucumber and SpecFlow
 
Develop WebDriver Automated Tests—and Keep Your Sanity
Develop WebDriver Automated Tests—and Keep Your SanityDevelop WebDriver Automated Tests—and Keep Your Sanity
Develop WebDriver Automated Tests—and Keep Your Sanity
 
Ma 15
Ma 15Ma 15
Ma 15
 
Eliminate Cloud Waste with a Holistic DevOps Strategy
Eliminate Cloud Waste with a Holistic DevOps StrategyEliminate Cloud Waste with a Holistic DevOps Strategy
Eliminate Cloud Waste with a Holistic DevOps Strategy
 
Transform Test Organizations for the New World of DevOps
Transform Test Organizations for the New World of DevOpsTransform Test Organizations for the New World of DevOps
Transform Test Organizations for the New World of DevOps
 
The Fourth Constraint in Project Delivery—Leadership
The Fourth Constraint in Project Delivery—LeadershipThe Fourth Constraint in Project Delivery—Leadership
The Fourth Constraint in Project Delivery—Leadership
 
Resolve the Contradiction of Specialists within Agile Teams
Resolve the Contradiction of Specialists within Agile TeamsResolve the Contradiction of Specialists within Agile Teams
Resolve the Contradiction of Specialists within Agile Teams
 
Pin the Tail on the Metric: A Field-Tested Agile Game
Pin the Tail on the Metric: A Field-Tested Agile GamePin the Tail on the Metric: A Field-Tested Agile Game
Pin the Tail on the Metric: A Field-Tested Agile Game
 
Agile Performance Holarchy (APH)—A Model for Scaling Agile Teams
Agile Performance Holarchy (APH)—A Model for Scaling Agile TeamsAgile Performance Holarchy (APH)—A Model for Scaling Agile Teams
Agile Performance Holarchy (APH)—A Model for Scaling Agile Teams
 
A Business-First Approach to DevOps Implementation
A Business-First Approach to DevOps ImplementationA Business-First Approach to DevOps Implementation
A Business-First Approach to DevOps Implementation
 
Databases in a Continuous Integration/Delivery Process
Databases in a Continuous Integration/Delivery ProcessDatabases in a Continuous Integration/Delivery Process
Databases in a Continuous Integration/Delivery Process
 
Mobile Testing: What—and What Not—to Automate
Mobile Testing: What—and What Not—to AutomateMobile Testing: What—and What Not—to Automate
Mobile Testing: What—and What Not—to Automate
 
Cultural Intelligence: A Key Skill for Success
Cultural Intelligence: A Key Skill for SuccessCultural Intelligence: A Key Skill for Success
Cultural Intelligence: A Key Skill for Success
 
Turn the Lights On: A Power Utility Company's Agile Transformation
Turn the Lights On: A Power Utility Company's Agile TransformationTurn the Lights On: A Power Utility Company's Agile Transformation
Turn the Lights On: A Power Utility Company's Agile Transformation
 

Recently uploaded

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 

Recently uploaded (20)

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 

Introduction to Selenium and WebDriver

  • 1. MD Full Day Tutorial 10/13/2014 8:30:00 AM "Introduction to Selenium and WebDriver " Presented by: Alan Richardson Compendium Developments Ltd Brought to you by: 340 Corporate Way, Suite 300, Orange Park, FL 32073 888-268-8770 ∙ 904-278-0524 ∙ sqeinfo@sqe.com ∙ www.sqe.com
  • 2. Alan Richardson Compendium Developments Alan Richardson has more than twenty years of professional IT experience, working as a programmer and at every level of the testing hierarchy—from tester through head of testing. Author of the books Selenium Simplified and Java For Testers, Alan also has created online training courses to help people learn technical web testing and Selenium WebDriver with Java. He now works as an independent consultant, helping companies improve their use of automation, agile, and exploratory technical testing. Alan posts his writing and training videos on SeleniumSimplified.com, EvilTester.com, JavaForTesters.com, and CompendiumDev.co.uk. Speaker Pr esentations
  • 3. 1 © Compendium Developments, 2014, CompendiumDev.co.uk Hands On WebDriver Alan Richardson SeleniumSimplified.com JavaForTesters.com EvilTester.com CompendiumDev.co.uk © Compendium Developments, 2014, CompendiumDev.co.uk Course Overview ● Hints and Tips ● Installation ● Basic WebDriver API capabilities ● Simple interrogation and navigation ● Synchronization strategies ● AJAX applications. ● Tools ● Location strategies using CSS and XPath. ● Introduction to abstraction approaches
  • 4. 2 © Compendium Developments, 2014, CompendiumDev.co.uk Approach ● Presentation ● Demos ● Exercises – Multiple Tracks depending on your level © Compendium Developments, 2014, CompendiumDev.co.uk Status Check ● Java Experience? ● Automation Experience? ● Selenium Experience? ● WebDriver Experience?
  • 5. 3 © Compendium Developments, 2014, CompendiumDev.co.uk With Mixed Experience Levels... ● If you get stuck, ask for help ● You can keep doing the exercises, even when we move on ● Read the source code for the answers ● Try and run the answer source code ● Run the answer source code in debug mode ● Expect some of this to go over your head © Compendium Developments, 2014, CompendiumDev.co.uk Demo – WebDriver in Action mvn -Dtest=TheDemoTest test ● A quick walkthrough of the demo code – JUnit, @Test – Java public static Config – WebDriver driver – New FirefoxDriver – Driver. ● navigate() .to() .back() ● findElement By.id(...) By.cssSelector ● switchTo alert ● close, quit
  • 6. 4 © Compendium Developments, 2014, CompendiumDev.co.uk Introduction & Overview © Compendium Developments, 2014, CompendiumDev.co.uk Ancient History ● Selenium 1.0 – Javascript through a proxy – Flat API “selenium.” – Server based ● WebDriver – Object Oriented API, Interface which each driver implements – Driver calls browser directly – Has a server component Remote WebDriver
  • 7. 5 © Compendium Developments, 2014, CompendiumDev.co.uk Installation ● Java SDK ● Maven ● IntelliJ IDE ● Firefox ● FirePath plugin seleniumsimplified.com/get-started/ © Compendium Developments, 2014, CompendiumDev.co.uk Exercises: Download, Run, Import ● Exercise: Download – Download the source code ● Exercise: Run from Command Line – Run “mvn test -Dtest=MyFirstTest” from the command line ● Exercise: Import into IDE – Import the code into IntelliJ ● Exercise: Run MyFirstTest from IDE – Run the “MyFirstTest”
  • 8. 6 © Compendium Developments, 2014, CompendiumDev.co.uk What is Maven? ● A build tool ● Dependency Management ● Standard Folder Structure ● Java project tasks simpler – mvn clean compile – mvn package – mvn test ● http://maven.apache.org © Compendium Developments, 2014, CompendiumDev.co.uk pom.xml <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-server</artifactId> <version>2.42.2</version> </dependency> </dependencies>
  • 9. 7 © Compendium Developments, 2014, CompendiumDev.co.uk Java Language Levels ● Set in IntelliJ Project Settings ● And in the pom.xml <!-- I have added the build section to support importing into IntelliJ automatically without throwing errors about wrong Java Version. This basically says the source requires at least Java 1.7 and use a compiler that outputs Java 1.7 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> © Compendium Developments, 2014, CompendiumDev.co.uk Benefits of Maven ● Standard directory structure ● Supported by Selenium and some of the drivers ● Easy to update and install ● Simple to add to Continuous Integration – “mvn test”
  • 10. 8 © Compendium Developments, 2014, CompendiumDev.co.uk Section: WebDriver is an API © Compendium Developments, 2014, CompendiumDev.co.uk API Overview http://www.mindmeister.com/280141421/selenium-2-webdriver-commands http://unow.be/at/apimindmap
  • 11. 9 © Compendium Developments, 2014, CompendiumDev.co.uk Demos During This Section ● Explain basic code sample of MyFirstTest ● Inspect WebElements using Firebug ● Various API constructs ● Use of Code Completion, JavaDoc, Autoscroll, params, view code ● How to run a test Class and Method ● How to debug a test Method, with breakpoints ● How to use evaluate © Compendium Developments, 2014, CompendiumDev.co.uk MyFirstTest.javapackage com.eviltester.webdriver; import org.junit.Assert; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class MyFirstTest { @Test public void startWebDriver(){ WebDriver driver = new FirefoxDriver(); driver.navigate().to("http://seleniumsimplified.com"); Assert.assertTrue("Expected Selenium Simplified", driver.getTitle(). startsWith("Selenium Simplified")); driver.close(); driver.quit(); } }
  • 12. 10 © Compendium Developments, 2014, CompendiumDev.co.uk Basic Knowledge ● Open a browser ● Navigate to page ● Read the page title ● Read the url ● Get text from the page ● Click on links ● Fill in forms ● Click on buttons Navigate Interrogate Manipulate & Synchronize © Compendium Developments, 2014, CompendiumDev.co.uk WebDriver Basics ● WebDriver – Think of the driver as the browser – Interface with implementations – FirefoxDriver, ChromeDriver etc. – High Level commands ● get, getTitle, getCurrentUrl, close, quit – Functional Groupings ● Navigate, Manage – Locate ● FindElement, FindElements
  • 13. 11 © Compendium Developments, 2014, CompendiumDev.co.uk WebElement Basics ● The DOM elements – <a>, <p>, <ul>, etc. ● Found By.Id, By.Name, etc. ● Interrogate – .getAttribute, .getValue, isEnabled etc. ● Manipulate – SendKeys, click, etc. © Compendium Developments, 2014, CompendiumDev.co.uk JUnit Basics ● Test Execution Framework ● @Test – Annotate a method with @Test ● Can Run @Test methods from the IDE ● Use Assertions to check results – Import org.junit.Assert; – Assert.assertTrue(true); – Assert.assertEquals(1+1, 2);
  • 14. 12 © Compendium Developments, 2014, CompendiumDev.co.uk WebDriver API Learning Tips ● Use Code Completion (ctrl+space) ● ctrl+Q for Documentation of Commands (ctrl+j on mac) ● ctrl+click to view the actual WebDriver code (cmd+click on mac) ● AutoScroll © Compendium Developments, 2014, CompendiumDev.co.uk IntelliJ Navigation Shortcut Keys ● Find Symbol – Shift + Cntrl + Alt + N ● Find Class – Cntrl + N ● Find File – Shift + Cntrl + N on mac use Cmd instead of Cntrl
  • 15. 13 © Compendium Developments, 2014, CompendiumDev.co.uk Browser Tools Overview ● Firefox – Inspect – Inspect with Firebug ● FirePath ● Chrome Inspect © Compendium Developments, 2014, CompendiumDev.co.uk Section: My First Test
  • 16. 14 © Compendium Developments, 2014, CompendiumDev.co.uk exercises.myfirsttest. MyFirstExtendedTest.java @Test public void getBasicPageAndCheckDetails(){ WebDriver driver = new FirefoxDriver(); driver.get( "http://seleniumsimplified.com/testpages/basic_web_page.html" ); Assert.assertTrue("Expected Different Title", driver.getTitle(). equals("Basic Web Page Title")); WebElement para1 = driver.findElement(By.id("para1")); Assert.assertTrue( "A paragraph of text".equals(para1.getText())); driver.close(); driver.quit(); } Exercise: © Compendium Developments, 2014, CompendiumDev.co.uk Used WebDriver API Summary ● WebDriver driver = new FirefoxDriver(); – Create a new instance of a driver and browser ● driver.get(aURL); – Open a web page in the browser ● findElement(s), By.id, By.name, By.tagName – WebElement element = driver.findElement(By.id(“anid)); – Find a WebElement using id or name attribute, – NoSuchElementException – findElement can be chained ● WebElement – getText, getAttribute ● driver.getTitle() – Get the title of the current page ● driver.close(), driver.quit() – Close the current browser window, – quit the browser and driver ● Junit Used – @Test – Assert.assertTrue – Assert.assertEquals – Comment as first arg – Expected, then Actual
  • 17. 15 © Compendium Developments, 2014, CompendiumDev.co.uk IntelliJ & JUnit © Compendium Developments, 2014, CompendiumDev.co.uk Java & Maven Summary ● Packages organise the Java classes ● Java Classes Start with Uppercase letters ● Methods start with lowercase letters ● Add tests in the src/test/java folder hierarchy ● Add Test into the class name to automatically run from maven as a test ● Import classes from packages to use them ● Code completion helps, be careful with imports
  • 18. 16 © Compendium Developments, 2014, CompendiumDev.co.uk Basics ● Use code completion as much as possible ● Learn short cut keys ● Remove any code syntax errors as fast as possible because they interfere with code completion © Compendium Developments, 2014, CompendiumDev.co.uk IntelliJ Evaluate Expression ● In Debug mode use Evaluate to experiment with code constructs in situ. – e.g. when writing xpath findElements code ● Can sometimes recompile and hot swap the code into the running debug session.
  • 19. 17 © Compendium Developments, 2014, CompendiumDev.co.uk Issues With Current Test ● Many checks in same test method ● If one check fails we have no info on later checks – But these are same state checks – i.e. page is same state for all checks, so we should get that information © Compendium Developments, 2014, CompendiumDev.co.uk Solution – Use JUnit to setup state ● @Test – The set of checks we want to make – We can setup state in the test and move through state e.g. open page, click links, etc. ● @Before – Before every @Test, do some stuff to setup state ● @BeforeClass – Before any test in the Class, setup some state ● @After, @AfterClass – 'clean up' for each 'before'
  • 20. 18 © Compendium Developments, 2014, CompendiumDev.co.uk Demo - JunitBeforeAfterDemoTest ● Outputs: ● @BeforeClass – -BeforeClass ● @Before – . Before ● @Test (4 tests) – + TestX ● @AfterClass – -AfterClass ● @After – . After ● What will Happen? © Compendium Developments, 2014, CompendiumDev.co.uk JUnit @Before, @After ● @BeforeClass, – Run once before any @Test in the class is run – static ● @AfterClass – Run once after all @Test in the class have run – static ● @Before, – Run before each @Test ● @After – Run after each @Test
  • 21. 19 © Compendium Developments, 2014, CompendiumDev.co.uk Demo ● Refactor MyFirstExtendedTest to use ● @Before, @After and ● @BeforeClass and @AfterClass © Compendium Developments, 2014, CompendiumDev.co.uk JUnit Exercises
  • 22. 20 © Compendium Developments, 2014, CompendiumDev.co.uk Debrief ● WebDriver is not a test tool ● JUnit is a test tool ● JUnit, (or TestNG, or Cucumber etc.) give us the 'test' capabilities ● WebDriver provides the browser automation capabilities ● @Before,@After etc. allow us to make the test code more readable so the @Test focuses on the 'test' and the 'check' not the 'setup' ● Keep your 'tests' small © Compendium Developments, 2014, CompendiumDev.co.uk Navigation & Interrogation
  • 23. 21 © Compendium Developments, 2014, CompendiumDev.co.uk Navigation & Interrogation ● We can navigate() around the site ● We can find an individual element or find multiple elements using different By locator methods e.g. by Id, name, tagName etc. ● When we have found an element we can interrogate it using the methods on WebElement ● WebDriver object exposes interrogation methods e.g. getUrl, getTitle, etc. © Compendium Developments, 2014, CompendiumDev.co.uk WebDriver
  • 24. 22 © Compendium Developments, 2014, CompendiumDev.co.uk Navigation Annotated ● driver – .get(<url>) – .navigate ● .to(<url>) ● .to(URL) ● .back() ● .forward() ● .refresh() driver.get(“http://aURL.com”); 'to' a URL String 'to' a java.net.URL Object 'back' through browser history 'forward' through browser history 'refresh' the current browser page © Compendium Developments, 2014, CompendiumDev.co.uk Driver level Interrogation Methods ● driver – .getTitle() – .getCurrentUrl() – .getPageSource() All return String. Pretty obvious what each method does. Be wary of using getPageSource it may not return what you expect.
  • 25. 23 © Compendium Developments, 2014, CompendiumDev.co.uk Be Careful with getPageSource <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Basic Web Page Title</title> </head> <body> <p id="para1" class="main">A paragraph of text</p> <p id="para2" class="sub">Another paragraph of text</p> </body> </html> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head> <title>Basic Web Page Title</title> </head> <body> <p class="main" id="para1">A paragraph of text</p> <p class="sub" id="para2">Another paragraph of text</p> </body></html> File on server String returned by method Line separation Firefox Driver Differences Additional attributes Attribute Ordering © Compendium Developments, 2014, CompendiumDev.co.uk Driver .findElement(By) ● By.id ● By.xpath ● By.cssSelector ● By.className ● By.linkText ● By.name ● By.tagName ● By.partialLinkText We find an element By using a locator strategy. e.g. by using the id, by evaluating an xpath expression, by executing a css selector, etc. driver.findElement(By.id("para1"))
  • 26. 24 © Compendium Developments, 2014, CompendiumDev.co.uk .findElements ● .findElement only returns 1 WebElement ● When a By can return more elements then .findElement returns the first in the list ● .findElements does not throw an exception if it can't match anything, it returns an empty list ● .findElements returns the full list of matching WebElements – e.g. driver.findElements(By.className(“normal”)); © Compendium Developments, 2014, CompendiumDev.co.uk Chaining .findElement (s) findElement(By.id(“.”)).findElement(By.name(“.”)) e.g. WebElement element = driver.findElement(By.id("div1")). findElement(By.name("pName3")). findElement(By.tagName("a")); ● Can use any By locator strategy ● Cannot Chain .findElements() as it returns a List of Web Elements List<WebElement>
  • 27. 25 © Compendium Developments, 2014, CompendiumDev.co.uk Chaining with ByChained ● ByChained is a support class import org.openqa.selenium.support.pagefactory.ByChained; ● ByChained extends By (it is a By) ● Instantiate it and pass in the By objects element = driver.findElement( new ByChained( By.id("div1"), By.name("pName9"), By.tagName("a"))); Takes a list of By Objects Have to instantiate, not use statically © Compendium Developments, 2014, CompendiumDev.co.uk Other By Support Classes ● ByIdOrName(“string to match”) @Test public void findByIdOrNameMatchOnName(){ WebElement element; element = driver.findElement( new ByIdOrName("pName2")); assertEquals("expected a match on name", "This is b paragraph text", element.getText()); } Takes a String which could be the ID or the Name Have to instantiate, not use statically
  • 28. 26 © Compendium Developments, 2014, CompendiumDev.co.uk Basic WebElement API Summary ● WebElement, findElement, – WebElement element = driver.findElement(By.id(“anid)); – Find a WebElement using id or name attribute, – findElement can be chained ● By.id, By.name – Locators to 'find' the elements ● element.sendKeys(“type this”); – Send keys to a web element to type something ● element.click() – Click on a web element element.getText – Get the text of the current element © Compendium Developments, 2014, CompendiumDev.co.uk WebElement
  • 29. 27 © Compendium Developments, 2014, CompendiumDev.co.uk Dom Element Interrogation Approach ● Find the Dom Element (WebElement) – .findElement – .findElements ● Use the WebElement Object methods: – .getText() – .getAttribute() – .getTagName() – .isEnabled() – .isSelected() – .isDisplayed() – .getSize() – .getLocation() – .getCssValue() If you want to interrogate, you have to locate © Compendium Developments, 2014, CompendiumDev.co.uk Does Navigation Require Synchronisation? ● Navigation blocks until the HTML of the page is loaded ● This does not mean the page is ready – for manipulation or – full interrogation ● Sometimes synchronisation is required on navigation
  • 30. 28 © Compendium Developments, 2014, CompendiumDev.co.uk What about clicking? ● Normally we navigate by clicking: – on links – on buttons – by submitting forms ● This is navigation as a side-effect – It normally requires synchronisation – We will cover this later in Manipulation © Compendium Developments, 2014, CompendiumDev.co.uk Navigation & Interrogation Exercises
  • 31. 29 © Compendium Developments, 2014, CompendiumDev.co.uk Manipulation © Compendium Developments, 2014, CompendiumDev.co.uk Manipulation ● When we find an element we can use the methods on the WebElement to do stuff e.g. click, clear, sendKeys etc.
  • 32. 30 © Compendium Developments, 2014, CompendiumDev.co.uk WebElement Manipulation ● .click() ● .clear() – Clear a field ● .sendKeys(String) actually (CharSequence) – Sends the appropriate events to a WebElement keyup, keydown, etc. – Helper Keys class (org.openqa.selenium.Keys) ● .submit() – Submit a form © Compendium Developments, 2014, CompendiumDev.co.uk More on SendKeys ● Keys.chord to send Key Sequences ● Shift, CTRL etc start a modifier sequence ● Keys.NULL ends a modifier sequence aTextArea.sendKeys("this text", "and this text"); commentTextArea.sendKeys( Keys.chord(Keys.SHIFT, "bob", Keys.NULL, " Dobbs")); assertEquals("BOB Dobbs", commentTextArea.getText())
  • 33. 31 © Compendium Developments, 2014, CompendiumDev.co.uk SendKeys © Compendium Developments, 2014, CompendiumDev.co.uk Manipulation Exercises
  • 34. 32 © Compendium Developments, 2014, CompendiumDev.co.uk Location Strategies © Compendium Developments, 2014, CompendiumDev.co.uk CSS Selectors
  • 35. 33 © Compendium Developments, 2014, CompendiumDev.co.uk Useful Tools For CSS and XPath ● Firefox – Install FireBug and FirePath plugins ● Chrome – Developer tools are supposed to allow search using xpath or css (sometimes this breaks between releases) © Compendium Developments, 2014, CompendiumDev.co.uk CSS Selectors ● A CSS Selector matches a set of elements ● Used to style HTML from CSS Stylesheets ● Reused in Selenium to match DOM elements ● Useful References – https://developer.mozilla.org/en-US/docs/CSS – http://reference.sitepoint.com/css/selectorref – http://net.tutsplus.com/tutorials/html-css-techniques/ the-30-css-selectors-you-must-memorize/ – http://www.quirksmode.org/css/contents.html – http://www.w3schools.com/cssref/css_selectors.asp – http://css-tricks.com/attribute-selectors/
  • 36. 34 © Compendium Developments, 2014, CompendiumDev.co.uk Basics of CSS Selectors ● * – match any element e.g. *, *[name="bob"] ● #id – match an id e.g. #p4 ● .class – match a class e.g. “.normal”, “p.normal” ● tag – match tags e.g. p, a, div ● [attribute] – Match on the attribute name e.g. [id] © Compendium Developments, 2014, CompendiumDev.co.uk CSS Attribute Matching ● tag[attribute^=”val”] – Partial match start of value ● tag[attribute$=”lue”] – Partial match end of value ● tag[attribute~=”value”] – Match on space separated values ● tag[a='val'], tag[b='val'] – , is an 'or' grouping ● tag[attribute] – match tags with an attribute ● tag[attribute=”value”] – match tags with a specific attribute value ● tag[attr1='val1'][attr2='val2'] – match tag using multiple attribute values ● tag[attribute*=”alu”] – Partial match anywhere on value
  • 37. 35 © Compendium Developments, 2014, CompendiumDev.co.uk CSS Selectors – Some Paths ● A > B – B directly under A e.g. <A><B/></A> ● A B – descendant – selectors separated by space i.e. “this then that” – Any degree of separation – e.g. “div li” would match but “div > li” would not ● A + B – B siblings of an A ● B:first-child - every B which is first child of something ● B:nth-child(n) – the nth child of B e.g. n==3, 3rd – For more selectors see the references © Compendium Developments, 2014, CompendiumDev.co.uk By.cssSelector ● By.cssSelector(<a css selector string>) @Test public void findElementsUsingCSSTag(){ List<WebElement> elements; elements = driver.findElements( By.cssSelector("p")); assertEquals("expected a different number", 41, elements.size()); }
  • 38. 36 © Compendium Developments, 2014, CompendiumDev.co.uk CSS Selectors ● CSS Selectors are faster than Xpath ● CSS Selectors can't traverse back up the dom ● Try and use CSS Selectors by default for complicated selection queries © Compendium Developments, 2014, CompendiumDev.co.uk Exercise: Manual CSS Selectors
  • 39. 37 © Compendium Developments, 2014, CompendiumDev.co.uk XPath © Compendium Developments, 2014, CompendiumDev.co.uk XPath ● XPath is an XML Query Language ● XPath is often slower than CSS ● XPath has more functions than CSS ● Xpath can navigate up and down the DOM ● References – http://www.w3schools.com/xpath/ – http://www.w3schools.com/xpath/xpath_functions.asp
  • 40. 38 © Compendium Developments, 2014, CompendiumDev.co.uk By.xpath @Test public void findSpecificPara(){ WebElement element; element = driver.findElement( By.xpath("//p[@name='pName5']")); assertEquals("Expected matching id", "p5", element.getAttribute("id")); } Match p elements anywhere in the DOM which have a name of 'pName5' © Compendium Developments, 2014, CompendiumDev.co.uk Xpath Selector Basics ● // - match anywhere ● / - match from root ● //* - any element ● //tag – named element ● //*[@attribute] – match if it has attribute ● //*[@attribute='value'] – attribute with value ● . for content matching ● .. for traversing back up the path e.g. //div/p/a/.. ● Operators: and, or etc – w3schools.com/xpath/xpath_operators.asp ● Xpath functions e.g. contains, starts-with – w3schools.com/xpath/xpath_functions.asp
  • 41. 39 © Compendium Developments, 2014, CompendiumDev.co.uk Additional References ● Xpath, CSS Rosetta Stone – http://www.simple-talk.com/dotnet/.net- framework/xpath,-css,-dom-and-selenium-the- rosetta-stone – http://bit.ly/RDJ3Wb ● Note browsers tend to use Xpath 1.0 © Compendium Developments, 2014, CompendiumDev.co.uk Chain XPath & CSS in findElement ● Remember we can 'chain' findElement – elem.findElement(By.id('me')).findElement(By.id('this'); ● You can use a combination of css selector and xpath selector – parentWebElement = webElementFoundByCSS.findElement(By.xpath("..")); – parentWebElement.findElements( By.cssSelector("div > div"));
  • 42. 40 © Compendium Developments, 2014, CompendiumDev.co.uk Exercise: Manual XPath Selectors © Compendium Developments, 2014, CompendiumDev.co.uk Exercise: CSS Basic Exercises
  • 43. 41 © Compendium Developments, 2014, CompendiumDev.co.uk Synchronisation © Compendium Developments, 2014, CompendiumDev.co.uk Synchronisation ● We need to synchronise the test with the application execution otherwise the test will try and click on things that are not displayed (etc.) and the test will fail. ● Most of our synchronisation will involve telling WebDriver to Wait for certain conditions to be true e.g. element to be clickable, presence of element located, etc.
  • 44. 42 © Compendium Developments, 2014, CompendiumDev.co.uk Demo ● Manually use basic_ajax.html ● Run the synchronisation demo test in debug mode ● Run the synchronisation demo test to failure ● Discuss – Why Synchronise? – What can we synchronise on here? © Compendium Developments, 2014, CompendiumDev.co.uk Discussion ● Why wait? ● What conditions might you have waited for? e.g. visible... what else? ● Why wait for title instead of assert? ● Can we make the code cleaner?
  • 45. 43 © Compendium Developments, 2014, CompendiumDev.co.uk Waiting Approaches ● Explicit wait – WebDriverWait for a specific condition to occur, if it does not occur then timeout after a certain time – Throw an exception if the wait times out – We wait only when we need to synchronise ● Implicit wait – Set a global timeout e.g. 10 seconds – Every time we try and find an element, we will wait for a maximum of 10 seconds – We are blind to synchronisation © Compendium Developments, 2014, CompendiumDev.co.uk ImplicitWait Approach ● Implicit wait – .findElement has an implicit wait – Default is 0 – Can amend the global default driver.manage().timeouts() .implicitlyWait( 10, TimeUnit.SECONDS); If an element can not be found then a findElement would take 10 seconds before throwing an Exception.
  • 46. 44 © Compendium Developments, 2014, CompendiumDev.co.uk WebDriverWait Approaches ● Explicit wait – WebDriverWait ● ExpectedConditions helper class ● custom ExpectedCondition (Class or inline) new WebDriverWait(driver,10).until( ExpectedConditions.titleIs( "HTML Form Elements")); © Compendium Developments, 2014, CompendiumDev.co.uk WebDriverWait Example@Test public void exampleUsingExpectedConditions(){ WebDriver driver; driver = driver.get("http://compendiumdev.co.uk/selenium/" + "basic_html_form.html"); new WebDriverWait(driver,10).until( ExpectedConditions.titleIs("HTML Form Elements")); assertEquals("HTML Form Elements", driver.getTitle()); } I don't really need this assert because WebDriverWait will throw a TimeoutException if the ExpectedCondition is not met Construct a WebDriverWait with the driver and a timeout in seconds. ExpectedConditions has a lot of common conditions that you would otherwise have to code yourself. Explore the methods
  • 47. 45 © Compendium Developments, 2014, CompendiumDev.co.uk Implicit or Explicit? ● Implicit can make initial tests faster to write – you don't worry about synchronisation when writing tests ● It can be harder to add synchronisation later – You have to identify a source of intermittency ● If you start with implicit then you can expose synchronisation problems by gradually reducing the implicit wait time ● Can make tests increasingly slower ● Implicit can make test failures slower to report © Compendium Developments, 2014, CompendiumDev.co.uk Basic Synchronisation with ExpectedConditions ● Wait for the page to be in the correct state before working with it – Why? ElementNotFound exception – More robust across browsers ● WebDriverWait – A wait class which handles element not found exceptions ● ExpectedConditions – A class of static helper methods – e.g. new WebDriverWait(driver,10).until( ExpectedConditions.somecondition( By.id("user_login")));
  • 48. 46 © Compendium Developments, 2014, CompendiumDev.co.uk Synchronisation API Summary ● new WebDriverWait(driver, seconds) – Create a wait class ● .until – Wait until a specific condition happens – until can return an element ● ExpectedConditions – A class of static helper wait methods for common scenarios e.g. ● .titleIs(expectedValue) – True when title of page is expectedValue ● visibilityOfElementLocated – Returns an element when element located is visible © Compendium Developments, 2014, CompendiumDev.co.uk Basic Synchronisation Exercises
  • 49. 47 © Compendium Developments, 2014, CompendiumDev.co.uk Section: Custom Expected Condition © Compendium Developments, 2014, CompendiumDev.co.uk Custom Expected Condition ● Warning – this section requires Java Knowledge ● Most people do not create custom expected conditions, and instead use the ExpectedConditions class ● Learning the extra Java required to deal with Custom Expected Conditions will improve the readability and maintainability of your tests
  • 50. 48 © Compendium Developments, 2014, CompendiumDev.co.uk Custom ExpectedCondition new WebDriverWait(driver,10).until( titleOfPageChangesFrom("in tutorial")); ); ● Why? – ExpectedConditions doesn't have what you need – You want to make your tests read well for your usage scenario – You want to pass additional values to the apply method ● ... create a Custom ExpectedCondition © Compendium Developments, 2014, CompendiumDev.co.uk Demo - ExpectedConditions ● org.openqa.selenium.support.ui. – ExpectedConditions ● Contains static methods which return an anonymous ExpectedCondition class – Define the type of the 'return' value ● new ExpectedCondition<Boolean>() – Need to write an 'apply' method ● Which returns true, or an object to signify wait is over ● To keep waiting return null, or false
  • 51. 49 © Compendium Developments, 2014, CompendiumDev.co.uk WebDriverWait Basics ● Create a new WebDriverWait, with a timeout and call the 'until' method with an 'ExpectedCondition' as argument ● The 'until' method – calls the 'apply' method on the ExpectedCondition – Would stop waiting ... ● If the 'apply' method returns true ● If the apply method returns an object – If still waiting then pause for 500ms then try again – If timeout period expired then throw TimeoutException new WebDriverWait(driver,10).until( titleOfPageChangesFrom("in tutorial")); ); © Compendium Developments, 2014, CompendiumDev.co.uk Custom ExpectedCondition Example new WebDriverWait(driver,10).until( new SelectContainsText(By.id("combo2"),"Java") );
  • 52. 50 © Compendium Developments, 2014, CompendiumDev.co.uk Custom ExpectedCondition Example private class SelectContainsText implements ExpectedCondition<Boolean> { private String textToFind; private By findBy; public SelectContainsText(final By comboFindBy, final String textToFind) { this.findBy = comboFindBy; this.textToFind = textToFind; } @Override public Boolean apply(WebDriver webDriver) { WebElement combo = webDriver.findElement(this.findBy); List<WebElement> options = combo.findElements(By.tagName("option")); for(WebElement anOption : options){ if(anOption.getText().equals(this.textToFind)) return true; } return false; } } I made it private because It is local to my test, normally this would be public You can return anything e.g. Boolean or WebElement. I chose Boolean for this. Pass in whatever you need in the constructor Override apply, this is called by WebDriverWait Implement your checking code using the passed in WebDriver © Compendium Developments, 2014, CompendiumDev.co.uk Adhoc Waits Created Inline Example new WebDriverWait(driver,10).until( new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver webDriver) { return webDriver.findElement( By.cssSelector("option[value='23']")) .isDisplayed(); } } ); Create ExpectedCondition on the fly and @Override the apply method WebDriver automatically passed through to the apply method Add your synchronisation code in the apply method If not Boolean then null == false Anonymous classes cannot have custom constructors so use for simple waits. Any other variables referenced need to be 'final'
  • 53. 51 © Compendium Developments, 2014, CompendiumDev.co.uk Wrap Adhoc Wait in a method private ExpectedCondition<WebElement> optionWithValueDisplayed(final String value) { return new ExpectedCondition<WebElement>() { @Override public WebElement apply(WebDriver webDriver) { return webDriver.findElement( By.cssSelector("option[value='" + value + "']")); } }; } Anonymous class wrapped in a method can use final params in the class. Bypasses need for a constructor. Returning a WebElement for future use. © Compendium Developments, 2014, CompendiumDev.co.uk Custom Expected Condition Synchronisation Exercises
  • 54. 52 © Compendium Developments, 2014, CompendiumDev.co.uk Abstraction © Compendium Developments, 2014, CompendiumDev.co.uk Abstraction Layers ● Abstraction Layers are – not a framework – contextual for an application, or an application framework – Essential for: ● Maintenance ● Speed of writing tests ● Ease of understanding tests
  • 55. 53 © Compendium Developments, 2014, CompendiumDev.co.uk Abstraction Layers Categorised 1) Data – Generic Data Abstractions e.g. email, postcode 2) Physical – Physical layout of your application e.g. pages, components – Navigation across pages 3) Domain – Your application Entities domain e.g. user, account 4) Logical – User actions, workflows © Compendium Developments, 2014, CompendiumDev.co.uk Hints ● Create the test you want to see – Write the test as if the abstraction layers already existed – So you can: ● See if it is readable, at the right level etc. ● Use code completion to create classes (IntelliJ: Alt+Enter)
  • 56. 54 © Compendium Developments, 2014, CompendiumDev.co.uk Page Objects? ● An abstraction around pages – Or page components ● Remove driver.xxxx from tests ● Model the physical domain ● Numerous ways of doing this – Page Factory – Extends LoadableComponent or SlowLoadableComponent – Basic objects ● Modelling Decisions Required © Compendium Developments, 2014, CompendiumDev.co.uk Example Test Using Page Object @Test public void basicHtmlPageTest(){ WebDriver driver = new FirefoxDriver(); BasicHtmlPage page = new BasicHtmlPage(driver); page.get(); assertEquals(page.getParagraphOne(), "A paragraph of text"); assertEquals(page.getParagraphTwo(), "Another paragraph of text"); }
  • 57. 55 © Compendium Developments, 2014, CompendiumDev.co.uk Example Page Object public class BasicHtmlPage { private final WebDriver driver; private String paragraphTwo; public BasicHtmlPage(WebDriver driver) { this.driver = driver; } public void get() { driver.get( "http://seleniumsimplified.com/testpages/basic_web_page.html"); } public String getParagraphOne() { return driver.findElement(By.id("para1")).getText(); } public String getParagraphTwo() { return driver.findElement(By.id("para2")).getText(); } } © Compendium Developments, 2014, CompendiumDev.co.uk Demo Page Objects POJO ● demospageobjectsExamplePageObjectTest ● Plain Old Java Object ● Any Required Synchronisation in the methods ● Simple ● No Conventions
  • 58. 56 © Compendium Developments, 2014, CompendiumDev.co.uk Refactor to Page Object Heuristics ● Convert local abstractions to Page Abstractions ● Replace comments with abstractions ● Make Decisions about the model based on the test readability and method reuse ● Build only what you need as you need it © Compendium Developments, 2014, CompendiumDev.co.uk Page Object Heuristics ● Construct the page object with a WebDriver ● Limit the methods to physical actions ● Expose Constants as public static final ● Don't navigate to the page in the constructor, either use a navigation object or a get() method
  • 59. 57 © Compendium Developments, 2014, CompendiumDev.co.uk Demo Refactor To Page Object ● Convert test to use page object ● demospageobjectsrefactorTo – RefactorExampleToPageObjectTest © Compendium Developments, 2014, CompendiumDev.co.uk Page Object Factory ● Annotate WebElements with @FindBy @FindBy(how= How.ID, using="combo1") private WebElement aComboElement; ● Initialise the annotated WebElement(s) using a PageFactory public BasicAjaxPageObject(WebDriver webDriver) { driver = webDriver; PageFactory.initElements(driver, this); }
  • 60. 58 © Compendium Developments, 2014, CompendiumDev.co.uk Demo – Page Object Factor ● Walkthrough of ● demospageobjects ● ExamplePageObjectTestUsingPageFactory © Compendium Developments, 2014, CompendiumDev.co.uk Slow Loadable Component ● If our page objects extend SlowLoadableComponent then we have an interface for 'waiting' for free ● Instead of explicitly coding waits for page load: TracksDashboardHomePage dashboard = new TracksDashboardHomePage(driver); new WebDriverWait(driver,10).until( ExpectedConditions.titleIs( dashboard.EXPECTED_TITLE)); ● We have a standard place to add conditions: TracksDashboardHomePage dashboard = new TracksDashboardHomePage(driver); dashboard.get();
  • 61. 59 © Compendium Developments, 2014, CompendiumDev.co.uk SlowLoadableComponent ● Public interface – get() ● loads and waits for component to be available extends SlowLoadableComponent<PageObjectClassName> ● Call super constructor in constructor public PageObjectClassName(WebDriver driver) { super(new SystemClock(), 10); this.driver = driver; } ● Implement load and isLoaded – isLoaded throws a new Error if not loaded – I often leave the 'load' part empty if I navigate to the page © Compendium Developments, 2014, CompendiumDev.co.uk Demo – Slow Loadable ● Walkthrough of ● demospageobjects ● ExamplePageObjectTestSlowLoadable
  • 62. 60 © Compendium Developments, 2014, CompendiumDev.co.uk Why do this? ● Synchronise on load – Page Load – don't do anything until page is loaded – Component Load – so we don't try to engage with the component until it is loaded – Get in the habit of synchronising – Interface makes it easy to extend – 'forced' to think about synchronisation – Encourages more comprehensive checks on 'ready' ● No impact – If we don't call .get() we don't trigger the wait © Compendium Developments, 2014, CompendiumDev.co.uk Tips ● Sometimes I want to load a Slow Loadable Component page, without triggering the wait ● create a getPage method, to use from the test, and the load method public void getPage(){ driver.get(Config.ROOT_URL + "/basic_web_page.html"); } @Override protected void load() { getPage(); }
  • 63. 61 © Compendium Developments, 2014, CompendiumDev.co.uk Summary ● Page Objects can be POJO ● Domain Objects are POJO ● Create abstraction layers ● Write failing code and use code completion ● Never add asserts into your abstraction layer ● Write methods so they read as native language ● Refactor in small increments ● Keep refactoring © Compendium Developments, 2014, CompendiumDev.co.uk Page Object Exercises
  • 64. 62 © Compendium Developments, 2014, CompendiumDev.co.uk Bonus Sections © Compendium Developments, 2014, CompendiumDev.co.uk Bonus Sections ● This section contains an overview of 'additional' sections of the WebDriver API ● The tutorial may or may not have the time to reach this point
  • 65. 63 © Compendium Developments, 2014, CompendiumDev.co.uk Support Classes as Abstractions Examine the WebDriver source code for examples to build on. e.g. org.openqa.selenium.support ● Select ● ExpectedConditions © Compendium Developments, 2014, CompendiumDev.co.uk Alerts ● Handle Alerts with – driver.switchTo().alert() ● .getText() ● .dismiss() ● .accept() ● .sendKeys(String) – .alert() returns an Alert object ● Does alert exist? – Switch to it and catch the exception to check – WebDriverWait ExpectedConditions.alertIsPresent The hierarchy is 'kinda' obvious When you think About it. Keep searching. Learn the API.
  • 66. 64 © Compendium Developments, 2014, CompendiumDev.co.uk User Interactions ● new Actions(driver) ● Actions Sequence – .keyDown – .keyUp – .sendKeys – .clickAndHold – .release – .click – .doubleClick – .moveToElement ● .moveByOffset ● .contextClick ● .dragAndDrop ● .dragAndDropBy ● .build() ● .perform() May more accurately simulate user interaction Build if you want to store and reuse the action sequence, otherwise just perform © Compendium Developments, 2014, CompendiumDev.co.uk More About Actions ● Actions uses GWT – operating system mouse move etc. so is more realistic ● You can interfere with the test if you move mouse and keyboard ● Actions can be brittle
  • 67. 65 © Compendium Developments, 2014, CompendiumDev.co.uk Actions to simulate user Actions ● Hover, etc. ● new Actions(driver). ChainOfActions. build().perform(); ● e.g. a chain of click actions Actions actions = new Actions(driver); actions.click(multiSelectOptions.get(0)). click(multiSelectOptions.get(1)). click(multiSelectOptions.get(2)).perform(); © Compendium Developments, 2014, CompendiumDev.co.uk Actions API Summary Slides Note: I try and avoid actions
  • 68. 66 © Compendium Developments, 2014, CompendiumDev.co.uk Cookies © Compendium Developments, 2014, CompendiumDev.co.uk Cookies ● Inspect – driver.manage ● .getCookies() ● .getCookieNamed(“name”) ● Interact – driver.manage ● .addCookie(Cookie) ● .deleteAllCookies ● .deleteCookie(Cookie) ● .deleteCookieNamed(“name”)
  • 69. 67 © Compendium Developments, 2014, CompendiumDev.co.uk Cookies Example @Test public void visitSearchPageAndCheckNoLastSearchCookie(){ WebDriver driver; driver = Driver.get("http://compendiumdev.co.uk/selenium/search.php"); driver.manage().deleteAllCookies(); driver.navigate().refresh(); Cookie aCookie = driver.manage().getCookieNamed("SeleniumSimplifiedLastSearch"); assertEquals("Should be no last search cookie", null, aCookie); } Delete all cookies for current domain Refresh page to get an initial set of cookies Find a named cookie Return null if not found © Compendium Developments, 2014, CompendiumDev.co.uk Cookie.Builder ● Can use constructor – new Cookie ● Or can use Cookie.Builder Cookie aNewCookie = new Cookie.Builder( aCookie.getName(), String.valueOf(2)). domain(aCookie.getDomain()). path( aCookie.getPath()). expiresOn(aCookie.getExpiry()). isSecure(aCookie.isSecure()). build();
  • 70. 68 © Compendium Developments, 2014, CompendiumDev.co.uk JavaScript © Compendium Developments, 2014, CompendiumDev.co.uk Why use Javascript? ● Workarounds ● Custom synchronisation ● Make the app more testable – Adjust hidden fields – Amend values ● Simulate hard to reach conditions ● Test Flash & HTML 5
  • 71. 69 © Compendium Developments, 2014, CompendiumDev.co.uk Javascript Execution ● Cast WebDriver to JavascriptExecutor – .executeScript(script, args...) – .executeAsyncScript(script, args...) ● Arguments are accessed using – arguments[index] e.g. "document.title=arguments[0]" ● Return values are converted to Java types – Html Element = WebElement, decimal = Double, non- decimal = Long, boolean = Boolean, array = List<Object>, else String or null ● Runs in an anonymous function © Compendium Developments, 2014, CompendiumDev.co.uk (JavascriptExecutor) Example @Test public void callAJavaScriptFunctionOnThePage(){ WebDriver driver = Driver.get( "http://www.compendiumdev.co.uk/selenium/canvas_basic.html"); JavascriptExecutor js =(JavascriptExecutor)driver; int actionsCount = driver.findElements( By.cssSelector("#commandlist li")).size(); assertEquals("By default app has 2 actions listed", 2, actionsCount); js.executeScript("draw(1, 150,150,40, '#FF1C0A');"); actionsCount = driver.findElements( By.cssSelector("#commandlist li")).size(); assertEquals("Calling draw should add an action", 3, actionsCount); } Cast driver to JavascriptExecutor to access the JavaScript methods Test page is at compendiumdev.co.uk/ selenium/canvas_basic.html Execute the 'draw' Function in the page
  • 72. 70 © Compendium Developments, 2014, CompendiumDev.co.uk Introducing Different Browsers © Compendium Developments, 2014, CompendiumDev.co.uk Browsers Overview ● Firefox Driver – currently built in ● HtmlUnit Driver – currently built in ● ChromeDriver – separate .exe ● OperaDriver – separate .exe available through maven, does not support current Opera version ● IEDriver – separate .exe IE10 and below ● RemoteDriver – currently built in, requires a server to connect to e.g. saucelabs or grid ● Various mobile drivers and safari driver
  • 73. 71 © Compendium Developments, 2014, CompendiumDev.co.uk Firefox Driver ● Currently part of deployed Jars – Effectively 'built in' – Easy to get started with – Can be slow to startup aDriver = new FirefoxDriver(); © Compendium Developments, 2014, CompendiumDev.co.uk Firefox Driver Profile ● Driver Profiles allow us to initialise a driver with additional capabilities and specific configuration FirefoxProfile profile = new FirefoxProfile(); WebDriver driver = new FirefoxDriver(profile); ● Profile level methods profile.setEnableNativeEvents(true); ● Set browser preferences profile.setPreference("extensions.firebug.currentVersion", "1.6.2"); ● Load extensions profile.addExtension(new File(extensionPath));
  • 74. 72 © Compendium Developments, 2014, CompendiumDev.co.uk FirefoxDriver Useful Links ● Firefox Preferences – about:config – http://www.timeatlas.com/5_minute_tips/general/intr oduction_to_firefox_preferences#.UIvbL4az728 ● Firefox Driver Documentation – http://code.google.com/p/selenium/wiki/FirefoxDriver © Compendium Developments, 2014, CompendiumDev.co.uk Browser Capabilities ● Generic browser control mechanism ● e.g. Set Proxies org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy(); proxy.setHttpProxy("localhost:8080") .setFtpProxy("localhost:8080") .setSslProxy("localhost:8080"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.PROXY, proxy);
  • 75. 73 © Compendium Developments, 2014, CompendiumDev.co.uk HtmlUnitDriver ● http://htmlunit.sourceforge.net/ ● DesiredCapabilities.setJavascriptEnabled seems to have been added for HTMLUnit WebDriver htmlunit = new HtmlUnitDriver(true); © Compendium Developments, 2014, CompendiumDev.co.uk ChromeDriver ● http://code.google.com/p/selenium/wiki/ChromeDriver ● Download the driver server – set "webdriver.chrome.driver" to the location ● Command line switches – http://peter.sh/experiments/chromium-command- line-switches/ – Pass in via options.addArguments ● ChromeDriver.log is useful debugging tool
  • 76. 74 © Compendium Developments, 2014, CompendiumDev.co.uk ChromeDriver Example String currentDir = System.getProperty("user.dir"); String chromeDriverLocation = currentDir + "/../tools/chromedriver/chromedriver.exe"; System.setProperty("webdriver.chrome.driver", chromeDriverLocation); ChromeOptions options = new ChromeOptions(); options.addArguments("test-type"); options.addArguments("disable-plugins"); options.addArguments("disable-extensions"); aDriver = new ChromeDriver(options); Calculate the location of Chromedriver relative to my test code Set the property so that the driver can be found Disable the plugins and extensions to prevent them interfering with the testsGet the driver © Compendium Developments, 2014, CompendiumDev.co.uk Opera Driver ● Documentation and Downloads – http://code.google.com/p/selenium/wiki/OperaDriver – https://github.com/operasoftware/operadriver – http://mvnrepository.com/artifact/com.opera/operadriver ● Add to maven <dependency> <groupId>com.opera</groupId> <artifactId>operadriver</artifactId> <version>1.1</version> </dependency> ● Does not support modern Opera version
  • 77. 75 © Compendium Developments, 2014, CompendiumDev.co.uk Config Opera Driver ● Capabilities ● OperaProfile – opera:config – http://www.opera.com/support/usingopera/operaini OperaProfile profile = new OperaProfile(); // switching off Javascript will cause the opera driver to fail profile.preferences().set("Extensions", "Scripting", 0); WebDriver opera = new OperaDriver(profile); © Compendium Developments, 2014, CompendiumDev.co.uk IE Driver ● http://code.google.com/p/selenium/wiki/InternetExplorerDriver ● Download the server executable – http://code.google.com/p/selenium/downloads/list – Set “webdriver.ie.driver” to the location of the driver executable String currentUserDir = System.getProperty("user.dir"); String IEDriverLocation = currentUserDir + "/../tools/iedriver_32/IEDriverServer.exe"; System.setProperty("webdriver.ie.driver", IEDriverLocation); aDriver = new InternetExplorerDriver();
  • 78. 76 © Compendium Developments, 2014, CompendiumDev.co.uk IE Driver on Path ● Drivers change ● IE Driver used to be add to path ● This method will be deprecated in favour of properties ● Pointing this out because we have to learn to read the code and the log messages output by Selenium © Compendium Developments, 2014, CompendiumDev.co.uk RemoteWebDriver
  • 79. 77 © Compendium Developments, 2014, CompendiumDev.co.uk Remote Driver ● When server is running on another machine ● e.g. SauceLabs.com DesiredCapabilities capabilities; capabilities = DesiredCapabilities.firefox(); capabilities.setCapability("version", "5"); capabilities.setCapability("platform", Platform.XP); try { String sauceURL = System.getenv("SAUCELABS_URL"); aDriver = new RemoteWebDriver( new URL(sauceURL), capabilities); } catch (MalformedURLException e) { e.printStackTrace(); } Remote driver configured by capabilities Watch out for UnsupportedCommandException during your tests © Compendium Developments, 2014, CompendiumDev.co.uk RemoteWebDriver Profiles ● Can set firefox profiles on RemoteWebDriver FirefoxProfile fp = new FirefoxProfile(); // set something on the profile... DesiredCapabilities dc = DesiredCapabilities.firefox(); dc.setCapability(FirefoxDriver.PROFILE, fp); WebDriver driver = new RemoteWebDriver(dc); ● Can set firefox profiles on RemoteWebDriver ● Can set Chrome options on RemoteWebDriver ChromeOptions options = new ChromeOptions(); // set some options DesiredCapabilities dc = DesiredCapabilities.chrome(); dc.setCapability(ChromeOptions.CAPABILITY, options); WebDriver driver = new RemoteWebDriver(dc);
  • 80. 78 © Compendium Developments, 2014, CompendiumDev.co.uk Grid Systems ● saucelabs.com ● browserstack.com ● Sign up for free ● Free plan good enough for this course, and a lot of things ● Records video ● Manual Testing © Compendium Developments, 2014, CompendiumDev.co.uk Section: Any Time Left?
  • 81. 79 © Compendium Developments, 2014, CompendiumDev.co.uk Any Questions? © Compendium Developments, 2014, CompendiumDev.co.uk Time for Free For All Automation? ● Pick a section and try and automated it ● Refactor some of the code into page objects or domain objects ● Try generating test data ● Try some of the advanced exercises
  • 82. 80 © Compendium Developments, 2014, CompendiumDev.co.uk Reference Section © Compendium Developments, 2014, CompendiumDev.co.uk Basic Practice Pages ● http://SeleniumSimplified.com/testpages/ – alert.html – basic_ajax.html – basic_web_page.html – basic_html_form.html – ajaxselect.php ● Source available at https://github.com/eviltester/seleniumTestPages – calculate.php – find_by_playground.php – refresh.php – search.php – etc. (see index.html)
  • 83. 81 © Compendium Developments, 2014, CompendiumDev.co.uk Advanced Practice Pages ● compendiumdev.co.uk/selenium/Showcase/Showcase.html – http://compendiumdev.co.uk/selenium/Showcase/Showcase.html ● Source available at https://github.com/eviltester/simpleGWTShowCaseClone © Compendium Developments, 2014, CompendiumDev.co.uk Recommended Applications For Practice Overview ● Intermediate: – http://todomvc.com/ ● Advanced – http://google-gruyere.appspot.com/ – http://demo.redmine.org/ – http://getontracks.org/
  • 84. 82 © Compendium Developments, 2014, CompendiumDev.co.uk Learn More Java ● Java For Testers – by Alan Richardson ● Effective Java – by Joshua Bloch ● Implementation Patterns – by Kent Beck ● Growing Object-Oriented Software, Guided by Tests – by Steve Freeman and Nat Pryce © Compendium Developments, 2014, CompendiumDev.co.uk Live pages to challenge yourself gwt.google.com/samples/Showcase/Showcase.html GWT demo application – good for synchronisation and location practice http://www.primefaces.org/showcase/ Primfaces demo application – good for synchronisation and location practice. Also check out the source as they use WebDriver for their testing.
  • 85. 83 165 WebDriver Summary Sheet WebElement .getText() .getAttribute(“name”) .getTagName() .isEnabled() .isSelected() .isDisplayed() .getSize() .getLocation() .getCssValue() Finding elements WebDriver WebElement = .findElement(BY) List<WebElement> = .findElements(BY) By .id(“an_id”) .xpath(“xpath”) .cssSelector(“css”) .className(“class”) .linkText(“text”) .name(“name” .tagName(“a_tag”) .partialLinkText(“t”); Support ByChaining(By, By) ByIdOrName(“idName”) Inspect Navigate WebDriver .get(“URL”) .navigate .to(Url) .to(“URL”) .back() .forward() .refresh() WebDriver .getTitle() .getCurrentUrl() .getPageSource() WebDriver Interact = new <driverClass>() .close() .quit() Interact WebElement .click() .submit() .clear() .sendKeys(String) .sendKeys(Keys.x) support.ui.Select .<methods>() SwitchTo. .alert() .getText() .accept() .dismiss() .sendKeys(String) .frame(...) ExpectedConditions .titleIs(String) … a lot of helper methods WebDriverWait (driver, timeout in Seconds) .until(ExpectedCondition) Cookies WebDriver .manage() .deleteAllCookies() .addCookie(Cookie) .deleteCookie(Cookie) .deleteCookieNamed(String) Synchronise Cookies WebDriver .manage() .getCookieNamed(String) .getCookies() Actions .keyUp() etc. .perform() (JavascriptExecutor) .executeScript 166 Selectors CSS Selectors * - any #id .class tag [attribute] [attribute="value"] tag[attribute="value"] tag[attr1='val1'][attr2='val2'] tag[att1='val1'], orThisTag = (' or ") *="anywhere in value" ^="start of value" $="end of value" ~="spaceSeperatedValue" Paths A > B (child) A B (any descendant) A + B (A's B sibling) tag:first-child XPATH Selectors // - match anywhere / - match from root //* - any element //tag //*[@attribute] //*[@attribute="value"] //tag[@attribute="value"] //tag1/tag2 (child) //tag1//tag2 (any descendant) //tag1/../.. (.. to go up) //*[.='element textl] [@at1="a" and @at2="b"] and, or, =, !=, <, >, >=, <- Functions contains(@a,"alu") starts-with(@a,"v") ends-with(@a,"e") Indexing //tag[1] XPath References ● http://www.w3schools.com/xpath/ ● http://www.simple-talk.com/dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/ CSS References ● http://reference.sitepoint.com/css/selectorref ● http://net.tutsplus.com/tutorials/html-css-techniques/ the-30-css-selectors-you-must-memorize/ ● http://www.quirksmode.org/css/contents.html ● http://www.w3schools.com/cssref/css_selectors.asp
  • 86. 84