Chapter 4. Selenium WebDriver Best Practices

In this chapter, we will learn the best practices for Selenium WebDriver and its techniques for handling a complex web application. Better understanding of WebDriver provides better results to find a quick solution. For example, JavascriptExecutor provides a quick workaround to automate web pages at a faster rate without using DOM.

In general, we face problems while involving an automation process without examining the application. It's quite necessary to understand why standard WebDriver approaches fail to work. Most of the problems occur when a page is overloaded with Ajax calls that load DOM elements asynchronously or when a page contains lots of frames, ActiveX/flex/flash components, and so on. Eventually, Selenium WebDriver overrides all these glitches with a proper workaround, where the driver simulates browsers exactly like a real user would do.

An efficient approach will help you to ensure better interaction with user interface components such as alerts, forms, and lists. For instance, the approach should be stable, quick, and reliable. The PageObject pattern is one of the best Selenium practices to maintain test suites or a collection of tests. Let's go through them one by one in the following sections.

In this chapter, we will learn the following topics:

  • Handling Ajax websites
    • isElementPresent()
    • Waits
  • Page Object pattern
  • Event-firing WebDriver
  • Handling iFrames
  • Handling native OS and browser pop-ups
  • JavascriptExecutor

Handling Ajax websites

Every modern web application makes use of Ajax calls that return data through asynchronous calls made to the web server. It avoids page reload and updates part of the web page at any time. Let's see how to manage these Ajax-based websites through Selenium WebDriver in detail.

The isElementPresent method

The isElementPresent() method is a user-defined method that checks for an element's availability within a web page. By default, the Selenium IDE generates the following script, where the object returns a Boolean value (however, this method is not recommended for handling Ajax-based web apps):

private boolean isElementPresent(By by) {
  try {
    driver.findElement(by);
    return true;
  } catch (NoSuchElementException e) {
    return false;
  }
}

@Test
public void Test01() throws Exception {
  driver.get("https://www.google.co.in/");
  Boolean a = isElementPresent(By.name("q"));
  System.out.println(a);
  Boolean b = isElementPresent(By.name("selenium_essentials"));
  System.out.println(b);
}

This method lets you wait for an element to execute appropriate actions. If the element is not found, it returns false. The try-catch statement in this example captures all the exceptions thrown (such as NoSuchElementException). This method can be written in many different ways. Let's see a few of the helpful tasks using this method:

  • Method 1: This approach is used to verify whether an element is present in a page or not:
    if (isElementPresent(By.locatorType("path"))) {
      System.out.println("Element is available");
    } else {
      System.out.println("Element not available");
    }
  • Method 2: The following is an alternative method to check the element's availability in a web page:
    if(driver.findElements(By.locatorType("path"))).size()>0)
    {
      System.out.println("Element is present in the webpage");
    } else {
      System.out.println("Element not available");
    }
  • Method 3: The following is a negative approach that verifies the unavailability of an element in a web page:
    if(!isElementPresent(By.locatorType("path")))
    {
      System.out.println("Element not available");
    } else {
      System.out.println("Element is available");
    }

In spite of the method verifying the element's availability, our tests certainly fail due to regular timeout (by returning false); what it lacks is WebDriverWait an explicit wait to meet a specific condition to occur and an implicit wait to wait for a specific time interval. We will explore this in detail in the following section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.173.238