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. Its a nice customization feature to have in your Webdriver tests.

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


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



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 :








MyCustomizationListener is an EventListener created by extending AbstractWebDriverEventListener.MyCustomizationListener code for highlighting the web element 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 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("http://www.testingDocs.com"); driver.findElement(By.id("sb_form_go")).click(); Assert.assertTrue(true,"Sample Assert"); }



This would be useful while running tests to know Webdriver interaction with the web elements during the test runs.