Working with WebDriver events

The Selenium WebDriver provides the EventFiringWebDriver class, which listens to various events happening during the test execution. For example, events are raised when we navigate to a page, when a click is performed on an element, or value is changed. The following table lists all the events that we can track during the test execution:

Event

Description

beforeNavigateTo

This method is called before the get(String url) or the navigate().to(String url) method is called.

afterNavigateTo

This method is called after the get(String url) or the navigate().to(String url) method is called.

beforeNavigateBack

This method is called before the navigate().back() method.

afterNavigateBack

This method is called after the navigate().back() method.

beforeNavigateForward

This method is called before the navigate().forward() method.

afterNavigateForward

This method is called after the navigate().forward() method.

beforeFindBy

This method is called before the following methods:

  • WebDriver.findElement(...)
  • WebDriver.findElements(...)
  • WebElement.findElement(...)
  • WebElement.findElements(...)

afterFindBy

This method is called after the following methods:

  • WebDriver.findElement(...)
  • WebDriver.findElements(...)
  • WebElement.findElement(...)
  • WebElement.findElements(...)

beforeChangeValueOf

This method is called before the WebElement.clear() or the WebElement.sendKeys(...) method.

afterChangeValueOf

This method is called after the WebElement.clear() or the WebElement.sendKeys(...) method.

beforeClickOn

This method is called before the WebElement.click() method.

afterClickOn

This method is called after the WebElement.click() method.

beforeScript

This method is called before the RemoteWebDriver.executeScript(java.lang.String, java.lang.Object[]) method.

afterScript

This method is called after the RemoteWebDriver.executeScript(java.lang.String, java.lang.Object[]) method.

onException

This method is called whenever an exception would be thrown.

We can use these event handlers to process extra commands. For example, before entering a value into a textbox, we can clear the existing value or capture a screenshot even if an exception is raised by WebDriver. This is done with the following steps:

  • Create your own user-defined event listener class. We can add the code that will be invoked when specific events are called to this class
  • Create an EventFiringWebDriver instance using WebDriver instance
  • And register the event listener class to the EventFiringWebDriver instance

The event listener class can be created in two ways:

  • By implementing the WebDriverEventListener interface
  • By extending the AbstractWebDriverEventListener class

In this recipe, we will see how to use EventFiringWebDriver to listen to the WebDriver events.

Getting ready

Create a new test that will get an instance of WebDriver, navigate to a site, and perform some basic actions and verifications.

How to do it...

First, we will define an event listener class by implementing the WebDriverEventListener interface in the following way:

package com.secookbook.examples.chapter04;

import org.openqa.selenium.*;
import org.openqa.selenium.support.events.WebDriverEventListener;

public class MyListener implements WebDriverEventListener {

  public void beforeChangeValueOf(WebElement element, WebDriver driver) {
    element.clear();
  }

}

Next, we will create a test that uses EventFiringWebDriver:

package com.secookbook.examples.chapter04;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class EventFiringTest {
  private WebDriver driver;

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
  }

  @Test
  public void testEventFiringWebDriver() throws Exception {

    EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
    MyListener myListener = new MyListener();
    eventDriver.register(myListener);

    eventDriver.get("http://bit.ly/1DbdhsW");
    eventDriver.findElement(By.id("q"))
      .sendKeys("Selenium Testing Tools Cookbook");
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
  }
}

There's more...

Let's add one more event handler to the event listener class to capture a screenshot when an exception is thrown:

public void onException(Throwable exception, WebDriver driver) {
  try {
   if (driver.getClass().getName().equals("org.openqa.selenium.remote.RemoteWebDriver")) {
    driver = new Augmenter().augment(driver);
   }
   File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
   FileUtils.copyFile(scrFile, new File("target/screenshots/error.png"));
  } catch (Exception e) {
   e.printStackTrace();
  }
}
..................Content has been hidden....................

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