Selenium Test Customization

Introduction

In this article, we would see how we may customize Selenium webdriver tests run by highlighting the web element on which the WebDriver interacts.

Often while running tests with webdriver we encounter many failures of tests and we take screenshots of them.In this Tip we would highlight the WebElement with which the WebDriver interacts with a color border, something similar to the FireBug style when inspecting an element. This would be helpful while watching webdriver tests running or during taking screenshots for debugging tests.

Let’s see how we can add this feature to our tests and learn about event listening in WebDriver.

EventFiringWebDriver

EventFiringWebDriver gives the WebDriver Capability of firing events. You should attach one or more EventListener classes to get notified of the events. EventFiringWebDriver is mostly used to the customization of tests and for debugging purposes.How to register EventFiringWebDriver.

 

EventFiringWebDriver_Listener

 

As shown in the above figure we may register our MyCustomizationListener();  with WebDriver instances to capture the events.Importing EventFiringWebDriver into your class. MyCustomizationListener  class would be implementing the interface WebDriverEventListener  or extending the abstract class AbstractWebDriverEventListener.There are several beforeX and afterX methods for implementation and exception methods in WebDriverEventListener.

Sample are :

beforeNavigateTo();

afterNavigateTo();

beforeFindBy();

afterFindBy();

beforeScript();

afterScript();

 

MyCustomizationListener is an EventListener created by extending AbstractWebDriverEventListener.MyCustomizationListener code for highlighting the webelement  interaction with a solid border.We use JavascriptExecutor to execute a script as shown below.

 

public class MyCustomizationListener extends AbstractWebDriverEventListener {

    
public void beforeFindBy(By by, WebElement element, WebDriver driver){
        
        WebElement webelement = driver.findElement(by);
  
        if (driver instanceof JavascriptExecutor) {
           ((JavascriptExecutor)driver).executeScript("arguments[0].style.border='5px solid blue'", webelement);
        }
    }
}

 

 Register a Listener.

Import EventFiringWebDriver as shown in the below snippet.

import org.openqa.selenium.support.events.EventFiringWebDriver;

Register a Listener to the EventFiringWebDriver

Let us see how to customize the webdriver sample test runs by highlighting the web elements found by the test using the listener above. I have written a sample @Test method that searches my website on the Bing search engine as shown in the below code snippet.

public class SearchWeb 
{
    public WebDriver webDriver = null;
    public EventFiringWebDriver driver = null;
    
    @Test
    public void Search_Bing() throws Throwable {
        
        webDriver = new FirefoxDriver();
        driver = new EventFiringWebDriver(webDriver);
        MyCustomizationListener myListener = new MyCustomizationListener();
        driver.register(myListener);
        driver.navigate().to("http://bing.com");
        driver.findElement(By.id("sb_form_q")).clear();
        driver.findElement(By.id("sb_form_q")).sendKeys("https://www.testingDocs.com");
        driver.findElement(By.id("sb_form_go")).click();
        Assert.assertTrue(true,"Sample Assert");
    }
        
}

 

Now we may see in the below picture screenshot of how we have highlighted the web elements ( like Firebug inspect kind off blue border style ) while the webdriver test run.This would be useful while running tests to know webdriver interaction with the web elements during the test runs.

Bing_Window_TestingDocs

 

 

This feature helps a bit visually while debugging the tests during the test runs or in screenshots taken during the tests.