Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

How To Use Selenium Successfully (Java Edition)

7,205 views

Published on

Dave Haeffner, a Selenium expert and active member of the Selenium project, steps through the why, how, and what of Selenium (the open-source automated web-testing tool for functional testing).

He also discusses how to start from nothing and build out a well-factored, maintainable, resilient, fast and scalable set of tests in Java. These will test your app across all of the browsers you care about, while exercising relevant functionality that matters to your business.

Published in: Internet
  • Hey guys! Who wants to chat with me? More photos with me here 👉 http://www.bit.ly/katekoxx
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Really Nice presentation sir :)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

How To Use Selenium Successfully (Java Edition)

  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 across all relevant browsers. Then package and scale them for you & your team.
  5. 5. 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?
  6. 6. Step 1 Define a Test Strategy
  7. 7. 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 to test and which browsers to care about
  8. 8. Step 2 Pick a Programming Language
  9. 9. Programming Language • Same language as the app? • Who will own it? • Build a framework or use an existing one? • http://bit.ly/seleniumframeworks
  10. 10. Step 3 Use Selenium fundamentals
  11. 11. Selenium Fundamentals • Mimics human action • Uses a few common actions • Works with “locators” Locators tell Selenium which HTML element to interact with
  12. 12. Common Actions • get(); • findElement(); • click(); //or submit(); • sendKeys(); • isDisplayed();
  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
  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
  15. 15. 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
  16. 16. 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)
  17. 17. 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
  18. 18. 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
  19. 19. Step 4 Write your first test
  20. 20. 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
  21. 21. 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
  22. 22. http://the-internet.herokuapp.com/login
  23. 23. Now to find an assertion 1. Login 2. Inspect the page 3. Find a locator 4. Verify it 5. Add it to the test
  24. 24. 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/java-exceptions
  25. 25. Exception Handling cont’d http://bit.ly/se-exceptions-howto
  26. 26. Step 5 Write reusable and maintainable test code
  27. 27. Page Objects
  28. 28. 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 :-(
  29. 29. Page Object(s) Application Under Test Test 1 Test 2 Test 3 Test 4 Test 5 Need to update JUST the page object :-D
  30. 30. Let’s look at a page object for login
  31. 31. And here’s what the test looks like when using it
  32. 32. Page object helpers: http://bit.ly/po-html-elements http://bit.ly/page-factory
  33. 33. Base Page Object a.k.a. Selenium Wrapper Utility Class etc.
  34. 34. Selenium Commands Page Object 1 Page Object 2 Page Object 3 Page Object 4 Page Object 5
  35. 35. 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
  36. 36. Let’s take a look at a Base Page Object
  37. 37. And here it is implemented
  38. 38. How everything fits together Test TestTest Page Object Page Object Base Page Object Tests use page objects like building blocks Page objects inherit from the base page object The base page object wraps your Selenium commands
  39. 39. Step 6 Make your tests resilient
  40. 40. Waiting
  41. 41. Thread.sleep(); Implicit wait Explicit waits
  42. 42. Thread.sleep(); Implicit wait Explicit waits
  43. 43. Thread.sleep(); Implicit wait Explicit waits http://bit.ly/se-waiting
  44. 44. 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)
  45. 45. In the Base page object
  46. 46. In the DynamicLoading page object
  47. 47. Browser Timing Considerations
  48. 48. Step 7 Prep for use
  49. 49. Test Harness • Simple organizational structure • Central setup and teardown • Configurable at run-time (with sensible defaults) • Reporting & Logging • Parallelization • Test Grouping
  50. 50. Folder structure
  51. 51. Central setup/teardown More on JUnit Rules: http://bit.ly/junit-rules
  52. 52. Simple config with defaults Import config where it’s needed (e.g., base test, etc.)
  53. 53. Reporting & Logging • Machine readable
 e.g., JUnit XML • Human readable
 e.g., screenshots, failure message, stack trace Fantastic Test Report Tool http://bit.ly/se-reporter (Allure Framework)
  54. 54. 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 Recommended approach: http://bit.ly/maven-surefire-parallel
  55. 55. Test Grouping • Metadata (a.k.a. Categories) • Enables “test packs” • Some category ideas • wip • shallow • deep • story number More info: bit.ly/junit-categories
  56. 56. Step 8 Add in cross-browser execution
  57. 57. Locally http://bit.ly/chrome-driver http://bit.ly/firefox-driver http://bit.ly/ie-driver http://bit.ly/edge-driver http://bit.ly/safari-driver
  58. 58. Chrome
  59. 59. 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
  60. 60. Grid Hub Node(s)
  61. 61. Grid More on Selenium Grid http://bit.ly/se-grid-wiki http://bit.ly/se-grid-post http://bit.ly/se-grid-extras http://bit.ly/se-grid-scaler
  62. 62. Sauce Labs Sauce Labs Browser Tests
  63. 63. Sauce Labs Additional Considerations - Test name - Pass/Fail status - Secure tunnel More on Sauce: http://bit.ly/saucelabs-platforms http://bit.ly/sauce-post http://bit.ly/sauce-java-docs
  64. 64. Step 9 Build an automated feedback loop
  65. 65. Feedback loops • The goal: Find failures early and often • Done with continuous integration and notifications • Notifications
 - remote: Email, chat, SMS
 - in-person: audio/visual indicators
  66. 66. 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
  67. 67. 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
  68. 68. Step 10 Find information on your own http://bit.ly/se-info-slides http://bit.ly/se-info-video http://bit.ly/se-info-writeup
  69. 69. Steps to solve the puzzle 1. 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
  70. 70. Write business valuable tests that are reusable, maintainable and resilient across all relevant browsers. Then package them and scale them for you & your team.
  71. 71. –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”
  72. 72. https://seleniumguidebook.comhttp://elementalselenium.com http://elementalselenium.com/bootcamp https://seleniumguidebook.com/#sample Both available in Java and Ruby. Additional languages coming later this year! @TourDeDave dhaeffner@gmail.com DaveHaeffner.com

×