Book Image

Selenium Testing Tools Cookbook Second Edition

By : UNMESH GUNDECHA
5 (1)
Book Image

Selenium Testing Tools Cookbook Second Edition

5 (1)
By: UNMESH GUNDECHA

Overview of this book

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.
Table of Contents (16 chapters)
15
Index

Checking an element's presence

The Selenium WebDriver does not implement Selenium RC's isElementPresent() method to check if an element is present on a page. This method is useful for building a reliable test that checks an element's presence before performing any action on it.

In this recipe, we will write a method similar to the isElementPresent() method.

How to do it...

To implement the isElementPresent() method, follow these steps:

  1. Create a method isElementPresent() and keep it accessible to your tests, shown as follows:
    private boolean isElementPresent(By by) {
      try {
        driver.findElement(by);
          return true;
        } catch (NoSuchElementException e) {
          return false;
      }
    }
  2. Now, implement a test that calls the isElementPresent() method. It will check if the desired element is present on a page. If found, it will click on the element; else it fails the test. This is done as follows:
    @Test
    public void testIsElementPresent() {
      // Check if element with locator criteria...