How To Use Selenium, Successfully
How To Use Selenium, Successfully
Successfully
by Dave Haeffner, @TourDeDave
http://www.wpclipart.com/geography/features/chasm.png.html
http://en.wikipedia.org/wiki/Optimal_solutions_for_Rubik's_Cube
Selenium Overview
Step 1
Define a Test Strategy
Test Strategy
1. How does your business make money?
2. What features of your application are being used?
3. What browsers are your users using?
4. What things have broken in the app before?
Outcome:
- What features to test
- Which browsers to care about
Step 2
Pick a Programming
Language
Programming Language
http://se.tips/seleniumframeworks
Step 3
Use Selenium
fundamentals
Selenium Fundamentals
Common Actions
get();
findElement();
sendKeys();
isDisplayed();
Locator Strategies
Class
CSS selectors
ID
Link Text
Tag Name
XPath
Locator Strategies
Class
CSS selectors
ID
Link Text
Tag Name
XPath
Locator Strategies
Class
CSS selectors
ID
Link Text
Tag Name
XPath
Locator Strategies
Class
CSS selectors
ID
Link Text
Tag Name
XPath
Locator Strategies
Class
CSS selectors
ID
Link Text
Tag Name
XPath
CSS vs XPath
http://se.tips/seleniumbenchmarks
http://se.tips/cssxpathexamples
http://se.tips/verifyinglocators
http://se.tips/locatorgame
Conversation
Step 4
Write your first test
A Login Example
1. Visit the login form
2. Find the login forms username field and input text
3. Find the login forms password field and input text
4. Find the submit button and click it
1. or, find the form and submit it
http://the-internet.herokuapp.com/login
http://se.tips/visual-testing-getting-started
In pom.xml
Exception Handling
org.openqa.selenium.NoSuchElementException:
Unable to locate element: {"method":"css
selector","selector":".flash.error"}
http://se.tips/se-exceptions-howto
Step 5
Write reusable and
maintainable test code
Page Objects
Test 2
Test 3
Test 4
Test 5
Test 2
Test 3
Page Object(s)
Test 4
Test 5
Selenium
Commands
Page
Object 1
Page
Object 2
Page
Object 3
Page
Object 4
Page
Object 5
Selenium
Commands
Global reuse
More readable
Insulates you from
Selenium API changes
http://se.tips/se-upgrade
Base Page
Object
Page
Object 1
Page
Object 2
Page
Object 3
Page
Object 4
Page
Object 5
And here it is
implemented
Test
Test
Page
Object
Page
Object
Base
Page
Object
Step 6
Make your tests resilient
Waiting
Thread.sleep();
Implicit wait
Explicit waits
Thread.sleep();
Implicit wait
Explicit waits
Thread.sleep();
Implicit wait
Explicit waits
http://se.tips/se-waiting
Explicit Waits
Browser Timing
Considerations
Step 7
Prep for use
Test Harness
Parallelization
Test Grouping
Folder structure
Central setup/teardown
Machine readable
e.g., JUnit XML
Human readable
e.g., screenshots, failure message, stack trace
Parallelization
In code
Test Grouping
defect
More info:
bit.ly/junit-categories
Step 8
Add in cross-browser
execution
Locally
http://se.tips/se-chromedriver
http://se.tips/se-firefoxdriver
http://se.tips/se-iedriver
http://bit.ly/edge-driver
http://se.tips/se-safaridriver
Chrome
Grid
Tests
Grid Hub
Grid
Node
Browser
Grid
Node
Browser
Grid
Node
Browser
Grid
Hub
Node(s)
Grid
http://se.tips/grid-getting-started
http://se.tips/se-grid-extras
http://se.tips/se-grid-scaler
Sauce Labs
Tests
Sauce Labs
Browser
http://se.tips/sauce-getting-started
http://se.tips/sauce-with-applitools
Step 9
Build an automated
feedback loop
Feedback loops
Notifications
- remote: Email, chat, SMS
- in-person: audio/visual indicators
Code Promotion
yes
Code
Committed
Unit/Integ.
(pass?)
yes
Deploy to
autom. test
server
(success?)
Run
automated
tests
(pass?)
yes
Notify team if no
Bonus points: stop the line
Deploy to
next env.
Simple CI configuration
1. Create a Job
2. Pull In Your Test Code
3. Set up Build Triggers
4. Configure Build steps
5. Configure Test Reports
6. Set up Notifications
7. Run Tests & View The Results
8. High-five your neighbor
Step 10
Find information on
your own
http://se.tips/selenium-resources
6.
2.
7.
3.
8.
4.
9.
5.
Dave Haeffner