How To Use Selenium,
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
Write business valuable tests that are
reusable, maintainable and
resilient.
!
Then package them for humans and
robots — a...
1. Prepare Yourself
• Define a Test Strategy
• Pick a Programming Language
• Choose a Text Editor
Define a Test Strategy
1. How does your business make money?
2. How do your users user your application?
3. What browsers a...
Pick a
Programming Language
• Same language as the app?
• Who will own it?
• Build a framework or use an existing one?
• h...
Choose a Text Editor
• Emacs
• IntelliJ
• Vim
• Sublime Text
2. Write Tests Well
• Atomic
• Autonomous
• Descriptive
• Small batches
• For a test runner
• Stored in Version Control
3. Leverage Selenium
Fundamentals
• Mimics human action
• Uses a few common actions
• Works with “locators”
Locators tell ...
Locator Strategies
• Class
• CSS selectors
• ID
• Link Text
• Partial Link Text
• Tag Name
• XPath
Good locators are:
• un...
Locator Strategies
• Class
• CSS selectors
• ID
• Link Text
• Partial Link Text
• Tag Name
• XPath
Good locators are:
• un...
Locator Strategies
• Class!
• CSS selectors
• ID!
• Link Text
• Partial Link Text
• Tag Name
• XPath
Good locators are:
• ...
Locator Strategies
• Class!
• CSS selectors!
• ID!
• Link Text
• Partial Link Text
• Tag Name
• XPath
Good locators are:
•...
Locator Strategies
• Class!
• CSS selectors!
• ID!
• Link Text
• Partial Link Text
• Tag Name
• XPath
CSS vs XPath
http://...
Finding Quality Locators
• Inspect the page
• Verify your selection
• e.g., FirePath or FireFinder
• http://bit.ly/verifyi...
Common Selenium actions
• click
• clear
• send_keys
• text
• displayed?
Look up your language’s bindings
http://bit.ly/sel...
A Login Example
1. Visit the main page of a site
2. Find the login button and click it!
3. Find the login form’s username ...
http://the-internet.herokuapp.com/login
4. Write Re-usable/
Maintainable Test Code
• Page Object Pattern
• Facade layer (a.k.a. Base Page Object)
Web Page
Test 1 Test 2 Test 3 Test 4 Test 5
X X X X X
Page
Object
Test 1 Test 2 Test 3 Test 4 Test 5
Web Page
X
X X X X X
Selenium
Commands
Page
Object 1
Page
Object 2
Page
Object 3
Page
Object 4
Page
Object 5
Base Page
Object
Page
Object 1
Page
Object 2
Page
Object 3
Page
Object 4
Page
Object 5
Selenium
Commands
• Insulates you f...
5. Make Your
Tests Resilient
Explicit waits
• Specify an amount of time, and an action
• Selenium will try until either:
• The action can be accomplish...
Selenium::WebDriver::Wait.new(timeout: 10).until do!
is_displayed? FINISH_TEXT!
end
Added to base page
6. Prep for use
• Central setup/teardown
• Folder structure
• Config file(s)
• Tagging
• Reporting (screenshots, JUnit XML, ...
Central setup/teardown
A simple file/folder structure
configs/
pages/
spec/
vendor/
Gemfile
Rakefile
Tagging
• Test packs
• Some tagging ideas
• wip
• critical
• component name
• slow
• story number
What this looks like in ...
Reporting
• For robots: JUnit XML output
• http://bit.ly/rspec-junit
• For humans: screenshots, video, logs, etc.
Human Readable
Robot Ready
7. Scale It
• Scaled execution
• 3rd party cloud provider, your own grid, etc.
• Parallelization
• Continuous Integration
Cloud execution
http://saucelabs.com
cloud_config.rb
spec_helper.rb
Parallelization
• In code
• with threads: http://bit.ly/seleniumparallel1
• with processes: http://bit.ly/seleniumparallel...
Continuous Integration
• Feedback loops
• Code promotion
Feedback loops
• The goal: Find failures early and often
• Notifications
• Email, chat, SMS
• In-person (audio & visual)
Code
Committed
Integration
(pass?)
Deploy to
autom. test
server
(success?)
Run
automated
tests
(pass?)
Deploy to
manual te...
Simple Jenkins configuration
1. Create a Job
2. Pull In Your Test Code
3. Set up Build Triggers
4. Configure Build steps
5. ...
Steps to solve the puzzle
1. Prepare yourself
2. Write tests well
3. Leverage Selenium fundamentals
4. Write re-usable & m...
Write business valuable tests that are
reusable, maintainable and
resilient.
!
Then package them for humans and
robots — a...
–Dave Haeffner, @TourDeDave
“You may think your puzzle is unique. But really, everyone is
trying to solve the same puzzle....
http://SeleniumGuidebook.com
d
http://ElementalSelenium.com
How To Use Selenium Successfully
How To Use Selenium Successfully
How To Use Selenium Successfully
How To Use Selenium Successfully
How To Use Selenium Successfully
Upcoming SlideShare
Loading in...5
×

How To Use Selenium Successfully

3,588

Published on

Want to learn how to use Selenium from the ground up? This presentation will show you how to start from nothing and build out a well factored, maintainable, resilient, and parallelized set of tests that will run locally, on a Continuous Integration server, and in the cloud. These tests will not only work well, but exercise relevant functionality that matters to the business.

Published in: Technology
2 Comments
11 Likes
Statistics
Notes
  • @Peter Kehl Thanks for the link. But consider me skeptical. I've seen dozens of tools attempt to crack the record-and-playback nut that fall short and end up being a footnote in the history of Selenium.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dave, thank you for this. However, a lot this can be now accomplished with Selenium IDE: packaged, reusable and maintainable. See SeLite – https://code.google.com/p/selite/wiki/ProjectHome.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
3,588
On Slideshare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
182
Comments
2
Likes
11
Embeds 0
No embeds

No notes for slide

Transcript of "How To Use Selenium Successfully"

  1. 1. How To Use Selenium, Successfully by Dave Haeffner, @TourDeDave
  2. 2. http://www.wpclipart.com/geography/features/chasm.png.html
  3. 3. http://en.wikipedia.org/wiki/Optimal_solutions_for_Rubik's_Cube
  4. 4. Write business valuable tests that are reusable, maintainable and resilient. ! Then package them for humans and robots — and scale them for you & your team.
  5. 5. 1. Prepare Yourself • Define a Test Strategy • Pick a Programming Language • Choose a Text Editor
  6. 6. Define a Test Strategy 1. How does your business make money? 2. How do your users user your application? 3. What browsers are your users using? 4. What things have broken before? Outcome: What to test and which browsers to care about
  7. 7. Pick a Programming Language • Same language as the app? • Who will own it? • Build a framework or use an existing one? • http://bit.ly/seleniumframeworks • What about a scripting language?
  8. 8. Choose a Text Editor • Emacs • IntelliJ • Vim • Sublime Text
  9. 9. 2. Write Tests Well • Atomic • Autonomous • Descriptive • Small batches • For a test runner • Stored in Version Control
  10. 10. 3. Leverage Selenium Fundamentals • Mimics human action • Uses a few common actions • Works with “locators” Locators tell Selenium which HTML element a command refers to
  11. 11. 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
  12. 12. 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
  13. 13. 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
  14. 14. 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 sanely
  15. 15. 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
  16. 16. 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
  17. 17. Common Selenium actions • click • clear • send_keys • text • displayed? Look up your language’s bindings http://bit.ly/seleniumwiki
  18. 18. A Login Example 1. Visit the main page of a site 2. Find the login button and click it! 3. Find the login form’s username field and input text! 4. Find the login form’s password field and input text! 5. Find the submit button and click it
  19. 19. http://the-internet.herokuapp.com/login
  20. 20. 4. Write Re-usable/ Maintainable Test Code • Page Object Pattern • Facade layer (a.k.a. Base Page Object)
  21. 21. Web Page Test 1 Test 2 Test 3 Test 4 Test 5 X X X X X
  22. 22. Page Object Test 1 Test 2 Test 3 Test 4 Test 5 Web Page X X X X X X
  23. 23. Selenium Commands Page Object 1 Page Object 2 Page Object 3 Page Object 4 Page Object 5
  24. 24. Base Page Object Page Object 1 Page Object 2 Page Object 3 Page Object 4 Page Object 5 Selenium Commands • Insulates you from Selenium API changes http://bit.ly/se-upgrade • More readable • Global reuse
  25. 25. 5. Make Your Tests Resilient
  26. 26. Explicit waits • Specify an amount of time, and an action • Selenium will try until either: • The action can be accomplished, or • The amount of time has been reached (and throw a timeout exception)
  27. 27. Selenium::WebDriver::Wait.new(timeout: 10).until do! is_displayed? FINISH_TEXT! end Added to base page
  28. 28. 6. Prep for use • Central setup/teardown • Folder structure • Config file(s) • Tagging • Reporting (screenshots, JUnit XML, etc.) • Command-line execution wrapper
  29. 29. Central setup/teardown
  30. 30. A simple file/folder structure configs/ pages/ spec/ vendor/ Gemfile Rakefile
  31. 31. Tagging • Test packs • Some tagging ideas • wip • critical • component name • slow • story number What this looks like in RSpec http://bit.ly/rspectagging
  32. 32. Reporting • For robots: JUnit XML output • http://bit.ly/rspec-junit • For humans: screenshots, video, logs, etc.
  33. 33. Human Readable
  34. 34. Robot Ready
  35. 35. 7. Scale It • Scaled execution • 3rd party cloud provider, your own grid, etc. • Parallelization • Continuous Integration
  36. 36. Cloud execution http://saucelabs.com
  37. 37. cloud_config.rb
  38. 38. spec_helper.rb
  39. 39. Parallelization • In code • with threads: http://bit.ly/seleniumparallel1 • with processes: http://bit.ly/seleniumparallel2 • Through a test runner (e.g., TestNG in Java) • Through your CI server #protip enforce random order execution of tests
  40. 40. Continuous Integration • Feedback loops • Code promotion
  41. 41. Feedback loops • The goal: Find failures early and often • Notifications • Email, chat, SMS • In-person (audio & visual)
  42. 42. Code Committed Integration (pass?) Deploy to autom. test server (success?) Run automated tests (pass?) Deploy to manual test server (manual) (success?) yes yes yes Notify team if no Code Promotion Bonus points: stop the line
  43. 43. Simple Jenkins 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 Don’t forget about a systems check!
  44. 44. Steps to solve the puzzle 1. Prepare yourself 2. Write tests well 3. Leverage Selenium fundamentals 4. Write re-usable & maintainable test code 5. Make your tests resilient 6. Prep everything for use 7. Scale it for you and your team
  45. 45. Write business valuable tests that are reusable, maintainable and resilient. ! Then package them for humans and robots — and scale them for you & your team.
  46. 46. –Dave Haeffner, @TourDeDave “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”
  47. 47. http://SeleniumGuidebook.com
  48. 48. d http://ElementalSelenium.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×