You are previewing Selenium Testing Tools Cookbook - Second Edition.
O'Reilly logo
Selenium Testing Tools Cookbook - Second Edition

Book Description

Over 90 recipes to help you build and run automated tests for your web applications with Selenium WebDriver

About This Book

  • Learn to leverage the power of Selenium WebDriver with simple examples that illustrate real-world problems and their workarounds

  • Explains the testing of mobile applications with Appium for mobile platforms such as iOS and Android

  • A pragmatic manual with engaging recipes and attractive screenshots to test your web applications efficiently

  • Who This Book Is For

    This book is intended for software quality assurance/testing professionals, software project managers, or software developers with prior experience in using Selenium and Java to test web-based applications. This books also provides examples for C#, Python and Ruby users.

    What You Will Learn

  • Understand how the locators work and use various locator methods to build reliable tests

  • Build reliable and maintainable tests with the Selenium WebDriver API

  • Use the PageFactory pattern to build a robust and easy to maintain test framework

  • Build data-driven tests and extend Selenium API to implement custom steps and checks

  • Integrate and use ATDD/BDD tools such as Cucumber, SpecFlow, Capybara, and Behave with the Selenium WebDriver API

  • Set up iPhone/iPad and Android simulators and devices to test your mobile web application with Appium

  • Set up Selenium Grid for faster and parallel running of tests, increasing test coverage and reducing test execution time for cross-browser testing

  • Build extended Selenium WebDriver tests for additional coverage

  • In Detail

    This book is an incremental guide that will help you learn and use the advanced features of the Selenium toolset including the WebDriver API in various situations to build a reliable test automation. You start off by setting up the test development environment and gain tips on the advanced locater strategy and the effective use of the Selenium WebDriver API.

    After that, the use of design patterns such as data - driven tests and PageFactory are demonstrated. You will then be familiarised with extending Selenium WebDriver API by implementing custom tasks and setting up your own distributed environment to run tests in parallel for cross-browser testing.

    Finally, we give you some tips on integrating Selenium WebDriver with other popular tools and testing mobile applications. By the end of this book, you will have learned enough to solve complex testing issues on your own.

    Style and approach

    This recipe-based guide covers real-life scenarios of testing your web apps with Selenium. Each recipe begins with a short introduction and key concepts along with illustrated examples of use cases, and ends with detailed but informative descriptions of the inner workings of the example.

    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 http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Selenium Testing Tools Cookbook Second Edition
      1. Table of Contents
      2. Selenium Testing Tools Cookbook Second Edition
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Sections
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      8. 1. Getting Started
        1. Introduction
        2. Configuring the Selenium WebDriver test development environment for Java with Eclipse and Maven
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
        3. Using Ant for the Selenium WebDriver test execution
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Configuring Microsoft Visual Studio for Selenium WebDriver test development
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Configuring Selenium WebDriver for Python and Ruby
          1. Getting ready
            1. Installing Python
            2. Installing Ruby
          2. How to do it...
            1. Installing Selenium WebDriver with Python
            2. Installing Selenium WebDriver with Ruby
          3. How it works...
        6. Setting up Internet Explorer Driver Server
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
        7. Setting up ChromeDriver for Google Chrome
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Setting up Microsoft WebDriver for Microsoft Edge
          1. Getting ready
          2. How to do it...
          3. How it works...
      9. 2. Finding Elements
        1. Introduction
        2. Using browser tools for inspecting elements and page structure
          1. How to do it...
            1. Inspecting pages and elements with Mozilla Firefox using the Firebug add-on
            2. Inspecting pages and elements with Google Chrome
            3. Inspecting pages and elements with Microsoft Internet Explorer
          2. How it works...
        3. Finding an element using the findElement method
          1. How to do it...
            1. Finding elements by the ID attribute
            2. Finding elements by the Class attribute
          2. How it works...
          3. There's more...
            1. NoSuchElementFoundException
          4. See also
        4. Finding elements using the findElements method
          1. How to do it...
          2. How it works...
          3. See also
        5. Finding links
          1. How to do it...
            1. Finding a link by its text
            2. Finding a link by partial text
          2. How it works...
          3. There's more...
          4. See also
        6. Finding elements by tag name
          1. How to do it...
          2. How it works...
          3. See also
        7. Finding elements using XPath
          1. Selecting nodes
          2. How to do it...
            1. Finding elements with an absolute path
            2. Finding elements with a relative path
            3. Finding elements using predicates
            4. Finding elements using attributes values with XPath
            5. Finding elements using attributes with XPath
            6. Matching any attribute using a value
            7. Selecting unknown nodes
            8. Selecting several paths
            9. Locating elements with XPath axes
          3. How it works...
        8. Finding elements using CSS selectors
          1. How to do it...
            1. Finding elements with an absolute path
            2. Finding elements with a relative path
            3. Finding elements using the Class selector
            4. Finding elements using the ID selector
            5. Finding elements using the attributes selector
            6. Finding elements using the attributes name selector
            7. Selecting several paths
            8. Performing a partial match on attribute values
          2. How it works...
          3. See also
        9. Locating elements using text
          1. How to do it...
            1. Using XPath's text function
            2. Finding elements using exact text value in XPath
            3. Using the CSS selector contains() pseudo-class
          2. How it works...
        10. Finding elements using advanced CSS selectors
          1. How to do it...
            1. Finding child elements
            2. Finding sibling elements
            3. Using user action pseudo-classes
            4. Using UI state pseudo-classes
          2. How it works...
          3. See also
        11. Using jQuery selectors
          1. How to do it...
          2. How it works...
          3. There's more...
      10. 3. Working with Elements
        1. Introduction
        2. Automating textboxes, text areas, and buttons
          1. How to do it...
            1. Clearing text from textbox and text-area elements
            2. Entering text in textbox and text-area elements
            3. Submitting forms
            4. Performing a click on a button element
          2. How it works...
          3. See also
        3. Checking an element's text
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        4. Checking an element's attribute and CSS values
          1. How to do it...
          2. How it works...
        5. Automating dropdowns and lists
          1. How to do it...
          2. How it works...
            1. Selection/deselection by visible text
            2. Selection/deselection by value
            3. Selection/deselection by index
          3. There's more...
          4. See also
        6. Checking options in the Select element
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        7. Checking selected options in dropdowns and lists
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. Checking selected options in a multi-select dropdown or list
          4. There's more...
          5. See also
        8. Automating radio buttons and radio groups
          1. How to do it...
          2. How it works...
            1. Working with radio groups
        9. Automating checkboxes
          1. How to do it...
          2. How it works...
        10. Working with WebTables
          1. How to do it...
          2. How it works...
          3. There's more…
      11. 4. Working with Selenium API
        1. Introduction
        2. Checking an element's presence
          1. How to do it...
          2. How it works...
          3. See also
        3. Checking an element's state
          1. How to do it...
          2. How it works...
        4. Using Advanced User Interactions API for mouse and keyboard events
          1. How to do it...
          2. How it works...
          3. See also
        5. Performing double-click on an element
          1. How to do it...
          2. How it works...
          3. See also
        6. Performing drag-and-drop operations
          1. How to do it...
          2. How it works...
          3. See also
        7. Working with context menus
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        8. Executing the JavaScript code
          1. How to do it...
          2. How it works...
          3. There's more...
        9. Capturing screenshots with Selenium WebDriver
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Capturing screenshots with RemoteWebDriver/Selenium Grid
          4. See also
        10. Maximizing the browser window
          1. Getting ready
          2. How to do it...
        11. Handling session cookies
          1. Getting ready
          2. How to do it...
          3. How it works...
        12. Working with browser navigation
          1. Getting ready
          2. How to do it...
          3. How it works...
        13. Working with WebDriver events
          1. Getting ready
          2. How to do it...
          3. There's more...
      12. 5. Synchronizing Tests
        1. Introduction
        2. Synchronizing a test with an implicit wait
          1. How to do it...
          2. How it works...
          3. See also
        3. Synchronizing a test with an explicit wait
          1. How to do it...
          2. How it works...
          3. See also
        4. Synchronizing a test with custom-expected conditions
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Waiting for an element's attribute value update
            2. Waiting for an element's visibility
            3. Waiting for DOM events
          4. See also
        5. Synchronizing a test with FluentWait
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
      13. 6. Working with Alerts, Frames, and Windows
        1. Introduction
        2. Handling a simple JavaScript alert box
          1. How to do it...
          2. How it works...
          3. There's more...
            1. The NoAlertPresentException
          4. See also
        3. Handling a confirm and prompt alert box
          1. The prompt alert box
          2. How to do it...
            1. Handling a prompt box
          3. How it works...
          4. See also
        4. Identifying and handling frames
          1. How to do it...
          2. How it works...
          3. There's more…
          4. See also
        5. Working with IFRAME
          1. How to do it...
          2. How it works...
          3. See also
        6. Identifying and handling a child window
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        7. Identifying and handling a window by its title
          1. How to do it...
          2. How it works...
          3. See also
        8. Identifying and handling a pop-up window by its content
          1. How to do it...
          2. How it works...
          3. See also
      14. 7. Data-Driven Testing
        1. Introduction
          1. Data-driven approach – workflow
          2. Benefits of data-driven testing
        2. Creating a data-driven test using JUnit
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        3. Creating a data-driven test using TestNG
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        4. Reading test data from a CSV file using JUnit
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Reading test data from an Excel file using JUnit and Apache POI
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Creating a data-driven test in NUnit
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        7. Creating a data-driven test in MSTEST
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        8. Creating a data-driven test in Ruby using Roo
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Creating a data-driven test in Python using DDT
          1. Getting ready
          2. How to do it...
          3. How it works...
      15. 8. Using the Page Object Model
        1. Introduction
        2. Using the PageFactory class for exposing the elements on a page
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. The FindBy annotations
            2. The CacheLookUp attribute
        3. Using the PageFactory class for exposing an operation on a page
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Using the LoadableComponent class
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Implementing nested Page Object instances
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Implementing the Page Object model in .NET
          1. Getting ready
          2. How to do it...
          3. How it works...
        7. Implementing the Page Object model in Python
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Implementing the Page Object model in Ruby using the page-object gem
          1. Getting ready
          2. How to do it...
          3. How it works...
      16. 9. Extending Selenium
        1. Introduction
        2. Creating an extension class for web tables
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Creating an extension for the jQueryUI tab widget
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Implementing an extension for the WebElement object to set the element attribute values
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Implementing an extension for the WebElement object to highlight elements
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Creating an object map for Selenium tests
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
        7. Capturing screenshots of elements in the Selenium WebDriver
          1. How to do it...
          2. How it works...
          3. See also
        8. Comparing images in Selenium
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        9. Measuring performance with the Navigation Timing API
          1. Getting ready
          2. How to do it...
          3. How it works...
      17. 10. Testing HTML5 Web Applications
        1. Introduction
        2. Automating the HTML5 video player
          1. How to do it...
          2. How it works...
            1. The video playback
          3. See also
        3. Automating interaction on the HTML5 canvas element
          1. How to do it...
          2. How it works...
          3. See also
        4. Web storage – testing local storage
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        5. Web storage – testing session storage
          1. How to do it...
          2. How it works...
          3. See also
        6. Cleaning local and session storage
          1. How to do it...
          2. See also
      18. 11. Behavior-Driven Development
        1. Introduction
        2. Using Cucumber-JVM and Selenium WebDriver in Java for BDD
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
          5. See also
        3. Using SpecFlow.NET and Selenium WebDriver in .NET for BDD
          1. Getting ready
          2. How to do it...
            1. Creating a spec file
            2. Creating a step definition file
            3. Defining a Page object and a helper class
            4. Running the tests
          3. How it works...
          4. See also
        4. Using Capybara, Cucumber, and Selenium WebDriver in Ruby
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Using Behave and Selenium WebDriver in Python
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      19. 12. Integration with Other Tools
        1. Introduction
        2. Configuring Jenkins for continuous integration
          1. Getting ready
          2. How to do it...
            1. Adding JDK
            2. Adding Ant
            3. Adding Maven
          3. There's more…
          4. See also
        3. Using Jenkins and Maven for Selenium WebDriver test execution in continuous integration
          1. Getting ready
          2. How to do it...
            1. Scheduling build for an automatic execution
          3. How it works...
            1. Scheduling builds
            2. Test results
          4. See also
        4. Using Ant for Selenium WebDriver test execution
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Using Jenkins and Ant for Selenium WebDriver test execution in continuous integration
          1. Getting ready
          2. How to do it...
            1. Scheduling the build for an automatic execution
          3. See also
        6. Automating a non-web UI in Selenium WebDriver with AutoIt
          1. Getting ready
          2. How to do it...
            1. Creating the AutoIt script
            2. Using OpenDialogHandler in Selenium WebDriver script
          3. How it works...
          4. There's more…
          5. See also
        7. Automating a non-web UI in Selenium WebDriver with Sikuli
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more…
          5. See also
      20. 13. Cross-Browser Testing
        1. Introduction
          1. Testing the framework for parallel execution
        2. Setting up Selenium Grid Server for parallel execution
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Adding nodes to Selenium Grid for cross-browser testing
          1. How to do it...
            1. Adding an IE node
            2. Adding a Firefox node
            3. Adding a Chrome node
            4. Mac OS X with Safari
        4. Creating and executing the Selenium script in parallel with TestNG
          1. Getting ready
          2. How to do it...
          3. How it works...
        5. Creating and executing the Selenium script in parallel with Python
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        6. Using Cloud tools for cross-browser testing running tests in the Cloud
          1. Getting ready
          2. How to do it...
        7. Running tests in headless mode with PhantomJS
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      21. 14. Testing Applications on Mobile Browsers
        1. Introduction
        2. Setting up Appium for testing mobile applications
          1. How to do it...
        3. Testing mobile web applications on iOS using Appium
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Testing mobile web applications on Android using Appium
          1. Getting Ready
          2. How to do it...
          3. How it works...
      22. Index