You are previewing Selenium Design Patterns and Best Practices.
O'Reilly logo
Selenium Design Patterns and Best Practices

Book Description

Build a powerful, stable, and automated test suite using Selenium WebDriver

In Detail

Selenium WebDriver is a global leader in automated web testing. It empowers users to perform complex testing scenarios with its simple and powerful interface.

This guide will provide you with all the skills you need to successfully create a functional Selenium test suite. Starting from the very beginning of the Selenium IDE, this book will show you how to transition into a real programing language such as Ruby or Java. You will quickly learn how to improve your code quality with refactoring and the skills needed to plan for the future development of your website to future-proof your test suite. With ample test examples running against a life-like e-commerce store and detailed step-by-step code review and explanations, you will be ready to test any challenge web developers might throw your way.

This book is intended for anyone who wants to create a test suite that is easy to maintain by expanding your knowledge until you feel truly confident and comfortable with Selenium.

What You Will Learn

  • Control Selenium WebDriver within any major programing language such as Java, Ruby, Python, and .NET
  • Learn how to implement a simple test script or a complex Page Objects framework
  • Set up each test to automatically deal with AJAX and jQuery
  • Remove test instabilities by blocking third-party services
  • Deal with data uncertainties by using fixtures, JSON APIs, and API stubbing
  • Improve your test suite continuously by refactoring code and using the DRY principle
  • Stabilize your tests by using patterns such as the Action Wrapper and Black Hole Proxy patterns
  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

    Table of Contents

    1. Selenium Design Patterns and Best Practices
      1. Table of Contents
      2. Selenium Design Patterns and Best Practices
      3. Credits
      4. Foreword
      5. About the Author
      6. Acknowledgments
      7. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      9. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      10. 1. Writing the First Test
        1. Choosing Selenium over other tools
          1. Right tool for the right job
          2. Price
          3. Open source
          4. Flexibility
        2. The Record and Playback pattern
          1. Advantages of the Record and Playback pattern
          2. Disadvantages of the Record and Playback pattern
        3. Getting started with the Selenium IDE
          1. Installing the Selenium IDE
          2. Recording our first test
          3. Saving the test
        4. Understanding Selenium commands
          1. Reading Selenese
        5. Comparing Ruby to Selenese
        6. Comparing Selenium commands in multiple languages
        7. Writing a Selenium test in Ruby
          1. Introducing Test::Unit
          2. Introducing asserts
          3. Interactive test debugging
        8. Summary
      11. 2. The Spaghetti Pattern
        1. Introducing the Spaghetti pattern
          1. Advantages of the Spaghetti pattern
          2. Disadvantages of the Spaghetti pattern
        2. Testing the product review functionality
          1. Starting a product review test
          2. Locating elements on the page
            1. Using a browser's element inspector
          3. Introducing locator strategies
            1. Using advanced locator strategies
              1. Using the absolute path
              2. Using the relative path
          4. Writing locator strategy code
            1. Using chained selector strategy methods
            2. Using the CSS selector
            3. Using XPath
          5. Implementing clicks and assertions
          6. Duplicating the product review test
        3. Reasons for failures
        4. The Chain Linked pattern
        5. The Big Ball of Mud pattern
        6. Summary
      12. 3. Refactoring Tests
        1. Refactoring tests
        2. The DRY testing pattern
          1. Advantages of the DRY testing pattern
          2. Disadvantages of the DRY testing pattern
            1. Moving code into a setup and teardown
            2. Removing duplication with methods
            3. Removing external test goals
            4. Using a method to fill out the review form
            5. Reviewing the refactored code
        3. The Hermetic test pattern
          1. Advantages of the Hermetic test pattern
          2. Disadvantages of the Hermetic test pattern
          3. Removing test-on-test dependence
            1. Using timestamps as test data
            2. Extracting the remaining common actions to methods
              1. Creating a new review with a single method call
            3. Reviewing the test-on-test dependency refactoring
        4. Creating generic DRY methods
          1. Refactoring with generic methods
        5. The random run order principle
          1. Advantages of the random run order principle
          2. Disadvantages of the random run order principle
        6. Summary
      13. 4. Data-driven Testing
        1. Data relevance versus data accessibility
        2. Hardcoding input data
          1. Hiding test data from tests
            1. Choosing the test environment
        3. Introducing test fixtures
          1. Parsing fixture data
          2. Using fixture data in the tests
            1. Using fixtures to validate products
            2. Testing the remaining products
              1. Multiple test models
              2. A single test model
              3. Implementing multiple test models
              4. Making test failures more expressive
        4. Using an API as a source of fixture data
        5. Using data stubs
        6. The default values pattern
          1. Advantages of the default values pattern
          2. Disadvantages of the default values pattern
          3. Merging the default values pattern and the faker library
            1. Implementing faker methods
            2. Updating the comment test to use default values
        7. Summary
      14. 5. Stabilizing the Tests
        1. Engineering the culture of stability
          1. Running fast and failing fast
          2. Running as often as possible
          3. Keeping a clean and consistent environment
          4. Discarding bad code changes
          5. Maintaining a stable test suite
        2. Waiting for AJAX
          1. Testing without AJAX delays
          2. Using explicit delays to test AJAX forms
          3. Implementing intelligent delays
        3. Waiting for JavaScript animations
        4. The Action Wrapper pattern
          1. Advantages of the Action Wrapper pattern
          2. Disadvantages of the Action Wrapper pattern
          3. Implementing the Action Wrapper pattern
        5. The Black Hole Proxy pattern
          1. Advantages of the Black Hole Proxy pattern
          2. Disadvantages of the Black Hole Proxy pattern
          3. Implementing the Black Hole Proxy pattern
        6. Test your tests!
        7. Summary
      15. 6. Testing the Behavior
        1. Behavior-driven Development
          1. Advantages of BDD
          2. Disadvantages of BDD
        2. Testing the shopping cart behavior
          1. Describing shopping cart behavior
          2. Writing step definitions
          3. Is BDD right for my project?
        3. Introducing Cucumber
          1. Feature files
          2. Step definition files
          3. The configuration directory
            1. Cucumber.yml
            2. env.rb
        4. Running the Cucumber suite
        5. The write once, test everywhere pattern
          1. Advantages of the write once, test everywhere pattern
          2. Disadvantages of the write once, test everywhere pattern
        6. Testing a mobile site
          1. Updating the Selenium wrapper
          2. Moving step definition files
          3. Updating the Cucumber profile and tagging tests
          4. Running and fixing incompatible steps
        7. Testing the purchase API
        8. Summary
      16. 7. The Page Objects Pattern
        1. Understanding objects
          1. Describing a literal object
            1. Object properties
            2. Object actions
            3. Objects within objects
          2. Describing a programming object
          3. Describing a web page with objects
        2. The Page Objects pattern
          1. Advantages of the Page Objects pattern
          2. Disadvantages of the Page Objects pattern
        3. Creating a Page Objects framework
          1. Creating a page super class
          2. Implementing sidebar objects
            1. Implementing the SidebarCart class
          3. Adding Self Verification to pages
          4. Implementing individual page classes
          5. Increasing the number of sidebar objects as the website grows
        4. Running tests with the Page Objects framework
          1. Using Page Objects in the Test::Unit framework
          2. Using Page Objects in different testing frameworks
            1. Looking at the Cucumber implementation
            2. Looking at the RSpec implementation
        5. The test tool independence pattern
          1. Advantages of the test tool independence
          2. Disadvantages of the test tool independence
        6. The right way to implement Page Objects
          1. Making pages smarter than tests
          2. Making tests smarter than pages
          3. Using modules instead of inheritance
          4. Placing logic in Page Objects
        7. Summary
      17. 8. Growing the Test Suite
        1. Strategies for writing test suites
          1. Different types of tests
          2. The smoke test suite
          3. The money path suite
          4. New feature growth strategy
          5. Bug-driven growth strategy
          6. The regression suite
          7. The 99 percent coverage suite
        2. Continuous Integration
          1. Managing the test environments and nodes
            1. Deploying new builds
            2. CI environment management
              1. Build node management
                1. Configuration management system
              2. Virtualization
          2. Selenium Grid
            1. Understanding standalone and grid modes
              1. JsonWire protocol
              2. Standalone mode
              3. Grid mode
            2. Installing Selenium Grid
              1. Using Selenium Grid
              2. Selenium Grid Extras
          3. Choosing the CI tool
            1. Decoupling tests from tools
        3. Frequently Asked Questions
          1. How to test on multiple browsers?
            1. Problem
            2. Possible solutions
              1. Localhost testing
              2. Setting up Selenium Grid
              3. Setting up SauceLabs Grid
          2. Which programming language to write tests in?
          3. Should we use Selenium to test the JS functionality?
            1. Problem
            2. Possible solution
          4. Why should I use a headless browser?
            1. Possible solution
              1. PhantomJS
          5. Which BDD tool should I use on my team?
            1. Problem
            2. Possible solutions
          6. Can I use Selenium for performance testing?
            1. Problem
            2. Possible solutions
        4. Summary
      18. A. Getting Started with Selenium
        1. Setting up the computer
          1. Using Command Line Interface
            1. Using the terminal on Windows
              1. Using MS-DOS
              2. Using PowerShell
              3. Using the terminal emulator
            2. Using the terminal on Mac OS X
            3. Using the terminal on Linux
          2. Configuring the Ruby runtime environment
            1. Installing Ruby
            2. Installing the selenium-webdriver gem
          3. Installing Firefox
        2. Understanding test class naming
          1. Naming files
          2. Naming classes
          3. Understanding the namespace
          4. Showing object inheritance
        3. Summary
      19. Index