Download - How To Use Selenium Successfully
![Page 1: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/1.jpg)
How To Use Selenium, Successfully
by Dave Haeffner, @TourDeDave
![Page 2: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/2.jpg)
http://www.wpclipart.com/geography/features/chasm.png.html
![Page 3: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/3.jpg)
http://en.wikipedia.org/wiki/Optimal_solutions_for_Rubik's_Cube
![Page 4: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/4.jpg)
Write business valuable tests that are reusable, maintainable and resilient across all relevant browsers.
Then package and scale them for you & your team.
![Page 5: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/5.jpg)
Selenium Overview
• What it is — the Reader’s Digest version
• What it is and is not good at
• IDE vs. Local vs. Remote
• Slow, brittle, and hard to maintain?
![Page 6: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/6.jpg)
Step 1 Define a Test Strategy
![Page 7: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/7.jpg)
Test Strategy1. 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 to test and which browsers to care about
![Page 8: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/8.jpg)
Step 2 Pick a Programming
Language
![Page 9: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/9.jpg)
Programming Language
• Same language as the app?
• Who will own it?
• Build a framework or use an existing one?
• http://bit.ly/seleniumframeworks
![Page 10: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/10.jpg)
Step 3 Use Selenium fundamentals
![Page 11: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/11.jpg)
Selenium Fundamentals
• Mimics human action
• Uses a few common actions
• Works with “locators”
Locators tell Selenium which HTML element to interact with
![Page 12: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/12.jpg)
Common Actions
• get();
• findElement();
• click(); //or submit();
• sendKeys();
• isDisplayed();
![Page 13: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/13.jpg)
Locator Strategies• Class
• CSS selectors
• ID
• Link Text
• Partial Link Text
• Tag Name
• XPath
Good locators are: • unique • descriptive • unlikely to change
That rules a few of these out
![Page 14: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/14.jpg)
Locator Strategies• Class
• CSS selectors
• ID
• Link Text
• Partial Link Text
• Tag Name
• XPath
Good locators are: • unique • descriptive • unlikely to change
That rules a few of these out
![Page 15: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/15.jpg)
Locator Strategies• Class
• CSS selectors
• ID
• Link Text
• Partial Link Text
• Tag Name
• XPath
Good locators are: • unique • descriptive • unlikely to change
That rules a few of these out
Start with IDs and Classes
![Page 16: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/16.jpg)
Locator Strategies• Class
• CSS selectors
• ID
• Link Text
• Partial Link Text
• Tag Name
• XPath
Good locators are: • unique • descriptive • unlikely to change
That rules a few of these out
Start with IDs and Classes
Use CSS or XPath (with care)
![Page 17: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/17.jpg)
Locator Strategies• Class
• CSS selectors
• ID
• Link Text
• Partial Link Text
• Tag Name
• XPath
CSS vs XPath http://bit.ly/seleniumbenchmarks http://bit.ly/cssxpathexamples
![Page 18: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/18.jpg)
Finding Quality Locators• Inspect the page
• Verify your selection
• e.g., FirePath or FireFinder
• http://bit.ly/verifyinglocators
• Learn through gaming
• http://bit.ly/locatorgame
• Conversation
![Page 19: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/19.jpg)
![Page 20: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/20.jpg)
Step 4 Write your first test
![Page 21: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/21.jpg)
Good Test Anatomy
• Write for BDD or xUnit test framework
• Test one thing (atomic)
• Each test can be run independently (autonomous)
• Anyone can understand what it is doing
• Group similar tests together
![Page 22: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/22.jpg)
A Login Example
1. Visit the login form
2. Find the login form’s username field and input text
3. Find the login form’s password field and input text
4. Find the submit button and click it
1. or, find the form and submit it
![Page 24: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/24.jpg)
![Page 25: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/25.jpg)
![Page 26: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/26.jpg)
Now to find an assertion
1. Login
2. Inspect the page
3. Find a locator
4. Verify it
5. Add it to the test
![Page 27: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/27.jpg)
![Page 28: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/28.jpg)
![Page 29: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/29.jpg)
Exception Handling• org.openqa.selenium.NoSuchElementException:
Unable to locate element: {"method":"css selector","selector":".flash.error"}
• Most common ones you’ll run into: NoSuchElement and StaleElementReferenceError
• A list of all WebDriver exceptions: http://bit.ly/se-exceptions-java
![Page 31: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/31.jpg)
Step 5 Write reusable and
maintainable test code
![Page 32: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/32.jpg)
Page Objects
![Page 33: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/33.jpg)
Application Under Test
Test 1 Test 2 Test 3 Test 4 Test 5Test 1 Test 2 Test 3 Test 4 Test 5
Need to update EVERY test :-(
![Page 34: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/34.jpg)
Application Under Test
Page Object(s)
Test 1 Test 2 Test 3 Test 4 Test 5
Need to update JUST the page object :-D
![Page 35: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/35.jpg)
Let’s look at a page object for login
![Page 36: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/36.jpg)
![Page 37: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/37.jpg)
And here’s what the test looks like when using it
![Page 38: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/38.jpg)
Page object helpers: http://bit.ly/po-html-elements http://bit.ly/po-page-factory
![Page 39: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/39.jpg)
Base Page Object
![Page 40: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/40.jpg)
Selenium Commands
Page Object 1
Page Object 2
Page Object 3
Page Object 4
Page Object 5
![Page 41: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/41.jpg)
Base Page Object
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://bit.ly/se-upgrade
![Page 42: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/42.jpg)
Let’s take a look at a Base Page Object
![Page 43: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/43.jpg)
![Page 44: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/44.jpg)
And here it is implemented
![Page 45: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/45.jpg)
![Page 46: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/46.jpg)
How everything fits together
Test TestTest
Page Object
Page Object
Base Page
Object
Tests use page objects
Page objects inherits the base page object (a.k.a. “selenium wrapper”)
The base page object wraps your Selenium commands
![Page 47: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/47.jpg)
Step 6 Make your tests resilient
![Page 48: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/48.jpg)
Waiting
![Page 49: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/49.jpg)
Thread.sleep(); Implicit wait Explicit waits
![Page 50: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/50.jpg)
Thread.sleep(); Implicit wait Explicit waits
![Page 52: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/52.jpg)
Explicit Waits
• Specify an amount of time, and an action
• Selenium will try repeatedly until either:
• The action is completed, or
• The amount of time specified has been reached (and throw a timeout exception)
![Page 53: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/53.jpg)
![Page 54: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/54.jpg)
In the Base page object
![Page 55: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/55.jpg)
In the DynamicLoading page object
![Page 56: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/56.jpg)
Browser Timing Considerations
![Page 57: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/57.jpg)
Step 7 Prep for use
![Page 58: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/58.jpg)
Test Harness• Simple organizational structure
• Central setup and teardown
• Configurable at run-time (with sensible defaults)
• Reporting & Logging
• Parallelization
• Test Grouping
![Page 59: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/59.jpg)
Folder structure
![Page 61: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/61.jpg)
Simple config with defaults
![Page 62: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/62.jpg)
Reporting & Logging
• Machine readablee.g., JUnit XML
• Human readable e.g., screenshots, failure message, stack trace
Fantastic Test Report Tool http://bit.ly/se-reporter (Allure Framework)
![Page 63: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/63.jpg)
Parallelization• In code
• Through your test runner
• Through your Continuous Integration (CI) server
#protip Enforce random order execution of tests http://bit.ly/junit-random-order
Recommended approach: http://bit.ly/mvn-surefire
![Page 64: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/64.jpg)
Test Grouping• Metadata (a.k.a. Categories)
• Enables “test packs”
• Some category ideas
• wip
• shallow
• deep
• story number
More info: bit.ly/junit-categories
![Page 65: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/65.jpg)
Step 8 Add in cross-browser
execution
![Page 66: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/66.jpg)
Locallyhttp://bit.ly/se-chromedriver http://bit.ly/se-firefoxdriver http://bit.ly/se-iedriver http://bit.ly/se-operadriver (12.16) http://bit.ly/se-safaridriver
![Page 67: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/67.jpg)
Chrome
![Page 68: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/68.jpg)
Grid
Grid Hub
Browser
Tests
All done with the Selenium Standalone Server Just requires additional runtime flags
Grid Node
Grid Node
Grid Node
Browser
Browser
![Page 69: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/69.jpg)
GridHub
Node(s)
![Page 70: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/70.jpg)
Grid
More on Selenium Grid http://bit.ly/se-grid-docs http://bit.ly/se-grid-post http://bit.ly/se-grid-extras http://bit.ly/se-grid-scaler
![Page 71: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/71.jpg)
Sauce Labs
Sauce Labs BrowserTests
![Page 72: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/72.jpg)
Sauce Labs
Additional Considerations - Test name - Pass/Fail status - Secure tunnel
More on Sauce: http://bit.ly/sauce-platforms http://bit.ly/sauce-post http://bit.ly/sauce-tutorial-java
![Page 73: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/73.jpg)
![Page 74: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/74.jpg)
Step 9 Build an automated
feedback loop
![Page 75: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/75.jpg)
Feedback loops• The goal: Find failures early and often
• Done with continuous integration and notifications
• Notifications e.g., remote: Email, chat, SMSin-person: audio/visual, public shaming
![Page 76: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/76.jpg)
Code Committed
Unit/Integ. (pass?)
Deploy to autom. test
server (success?)
Run automated
tests (pass?)
Deploy to next env.
yes
yes
yes
Notify team if no
Code Promotion
Bonus points: stop the line
![Page 77: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/77.jpg)
Simple CI configuration1. 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
![Page 78: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/78.jpg)
![Page 79: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/79.jpg)
Step 10 Find information on
your own
http://bit.ly/se-info-slides
http://bit.ly/se-info-video
![Page 80: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/80.jpg)
Steps to solve the puzzle1. Define a Test Strategy
2. Pick a programming language
3. Use Selenium Fundamentals
4. Write Your First Test
5. Write re-usable and maintainable test code
6. Make your tests resilient
7. Package your tests into a framework
8. Add in cross-browser execution
9. Build an automated feedback loop
10. Find information on your own
![Page 81: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/81.jpg)
Write business valuable tests that are reusable, maintainable and resilient across all relevant browsers.
Then package them and scale them for you & your team.
![Page 82: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/82.jpg)
–Dave Haeffner
“You may think your puzzle is unique. But really, everyone is
trying to solve the same puzzle. Yours is just configured
differently — and it’s solvable”
![Page 84: How To Use Selenium Successfully](https://reader034.vdocuments.us/reader034/viewer/2022042615/55b9f667bb61eb212b8b4702/html5/thumbnails/84.jpg)
Get in touch
@TourDeDave
DaveHaeffner.com