The listener is used to track any activity. There are many listeners available in selenium and testNG.
WebDriverEventListener is provided in WebDriver to track events those take place in WebDriver during script execution.
If we want to see closely how all actions are triggered either this is the click event of a button, URL or navigating back and forward in browser or some exception occur while executing script. To take screenshot once some exceptions occur during execution.
So WebDriverEventListener has been just the answer of all the queries.
Create any class for tracking events. Implements WebDriverEventListener to any class and override all methods available in that interface.
We have created a class with name : TrackChangesInApplication
Click on no 3 that is - Add unimplemented methods.
You will see automatically many methods comes with an editor. Just write some testing text there like below.
package com.javabykiran.listeners;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
public class TrackChangesInApplication implements WebDriverEventListener {
@Override
public void beforeNavigateTo(String url, WebDriver driver){
System.out.println("beforeNavigateTo -- ");
}
@Override
public void afterNavigateTo(String url, WebDriver driver) {
System.out.println("afterNavigateTo -- ");
}
@Override
public void beforeNavigateBack(WebDriver driver) {
System.out.println("beforeNavigateBack -- ");
}
@Override
public void afterNavigateBack(WebDriver driver) {
System.out.println("afterNavigateBack -- ");
}
@Override
public void beforeNavigateForward(WebDriver driver) {
System.out.println("beforeNavigateForward -- ");
}
@Override
public void afterNavigateForward(WebDriver driver) {
System.out.println("afterNavigateForward -- ");
}
@Override
public void beforeFindBy(By by, WebElement element, WebDriver driver) {
System.out.println("beforeFindBy -- ");
}
@Override
public void afterFindBy(By by, WebElement element, WebDriver driver) {
System.out.println("afterFindBy -- ");
}
@Override
public void beforeClickOn(WebElement element, WebDriver driver) {
System.out.println("beforeClickOn -- ");
}
@Override
public void afterClickOn(WebElement element, WebDriver driver) {
System.out.println("afterClickOn -- ");
}
@Override
public void beforeChangeValueOf(WebElement element, WebDriver driver) {
System.out.println("beforeChangeValueOf -- ");
}
@Override
public void afterChangeValueOf(WebElement element, WebDriver driver) {
System.out.println("afterChangeValueOf -- ");
}
@Override
public void beforeScript(String script, WebDriver driver) {
System.out.println("beforeScript -- ");
}
@Override
public void afterScript(String script, WebDriver driver) {
System.out.println("afterScript -- ");
}
@Override
public void onException(Throwable throwable, WebDriver driver) {
System.out.println("onException -- ");
}
}
Once this is finished we will create test scripts and we will initialise a driver with listener just we wrote.
package com.javabykiran.automation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import com.javabykiran.listeners.TrackChangesInApplication;
public class FirstExampleForListener {
public static void main(String[] args) {
// opens a Firefox browser
WebDriver driver = new FirefoxDriver();
// Initializing instance of EventFiringWebDriver
EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
// Now create object of TrackChangesInApplication to register it with EventFiringWebDriver
TrackChangesInApplication trackChange = new TrackChangesInApplication();
// Registering it with EventFiringWebDriver
eventDriver.register(trackChange);
// opening of url with eventDriver
eventDriver.get("http://www.javabykiran.com/selenium/demo/");
// Maximize window
eventDriver.manage().window().maximize();
// locate email element
WebElement userEle = eventDriver.findElement(By.cssSelector("input[id=email]"));
// enter uname into text box
userEle.sendKeys("kiran@gmail.com");
// locate password element
WebElement passwordEle = eventDriver.findElement(By.id("password"));
// enter password into text box
passwordEle.sendKeys("123456");
// locate sign in button
WebElement signInbuttonEle = eventDriver.findElement(By.xpath(".//*[@id='form']/div[3]/div/button"));
// click sign in button
signInbuttonEle.click();
// locate logout button
WebElement logoutButtonEle = eventDriver.findElement(By.xpath("html/body/div/header/nav/div/ul/li/a"));
// click logout button
logoutButtonEle.click();
}
}
Output:
beforeNavigateTo --
afterNavigateTo --
beforeFindBy --
afterFindBy --
beforeChangeValueOf --
afterChangeValueOf --
beforeFindBy --
afterFindBy --
beforeChangeValueOf --
afterChangeValueOf --
beforeFindBy --
afterFindBy --
beforeClickOn --
afterClickOn --
beforeFindBy --
afterFindBy --
beforeClickOn --
afterClickOn --
We will observe that every operation is tracked here and so if we want to do anything we can do it here itself in listener.
If we want to put some wait time before every find by() method. If there is no listener then what we will have to do is write code again and again before every find by() method.
Instead we can use above listener so that we need not write that code again and again.
@Override
public void beforeFindBy(By by, WebElement element, WebDriver driver) {
System.out.println("beforeFindBy -- ");
// Here write code for explicit wait which will be called for
// every find by method call in test script
}
This code will execute every time we do “find by” to any control.