WebDriver Event Listener : Take Screenshot On Exception

When you execute selenium automation testing scripts, there are a lot of times that you want to do something when an event happened. For example:

  1. Take screenshot when an exception occurred.
  2. Monitor and track the web element status after some action take place.
  3. Log data before or after an action.
  4. Log the navigation url to check if the navigation logic is correct.
  5. Log web element status to check if the javascript execute correctly.

In this article we will show you how to use webdriver event listener and how to take screenshot when an exception happened in selenium test scripts.

Selenium Provide Below Interface Or Class For Event Listening.

  1. WebDriverEventListener
    This is an interface.
  2. AbstractWebDriverEventListener
    This is an abstract class. 
  3. EventFiringWebDriver
    This is the wrapper class for webdriver object, after wrapping, you can register event listener object to it.

Steps To Register Event Listener

  1. Create a Listener class
    Either by implement WebDriverEventListener interface.

    public class Dev2qaWebDriverEventListener implements WebDriverEventListener
    {
          ......
    }

    Or extend AbstractWebDriverEventListener abstract class.

    public class Dev2qaWebDriverEventListener extends AbstractWebDriverEventListener
    {
         ......
    }

    The difference between above two methods is that you need to implement all methods when choose implement the WebDriverEventListener interface, and you only need to implement the methods you need when choose extend AbstractWebDriverEventListener abstract class.

    You can click here to see detail introduction for the methods of above interface or abstract class.

  2. Implement the listening event java code in related event method. Such as beforeClickOn(WebElement webElement, WebDriver driver) or onException(Throwable error, WebDriver driver)
    	@Override
    	public void beforeClickOn(WebElement webElement, WebDriver driver) {
    		System.out.println("Before click, lement text : " + webElement.getText() + " , tag name: " + webElement.getTagName());
    	}
    
  3. Wrap webdriver object to create EventFiringWebDriver object.
    		/* Initiate Firefox driver. */
    		this.webDriver = new FirefoxDriver();
    		
    		/* Initiate wrapping object. */
    		this.eventWebDriver = new EventFiringWebDriver(this.webDriver);
  4. Create a new event listener object for the class in step1.
    		/* Create a event listener object. */
    		this.eventListener = new Dev2qaWebDriverEventListener();
  5. Register event listener to the EventFiringWebDriver object.
    		/* Register event listener. */
    		this.eventWebDriver.register(eventListener);

Code Example

There are two java classes and two html pages in below example.

  1. Dev2qaWebDriverEventListener.java:
    This is the event listener class, it extend AbstractWebDriverEventListener.
    It only implement some methods. In most method it will print out log info. And in onException method, it will take a screenshot and save to C:\\Workspace\\webdriverEventListenerScreenshot.png

    public class Dev2qaWebDriverEventListener extends AbstractWebDriverEventListener {
    
    	@Override
    	public void afterClickOn(WebElement webElement, WebDriver driver) {
    		System.out.println("afterClickOn trigger, " + this.getWebElementInfo(webElement));
    	}
    
    	@Override
    	public void afterFindBy(By by, WebElement webElement, WebDriver driver) {
    		System.out.println("afterFindBy trigger, Condition : " + by.toString() + this.getWebElementInfo(webElement));
    	}
    
    	@Override
    	public void afterNavigateBack(WebDriver driver) {
    		System.out.println("afterNavigateBack trigger.");
    	}
    
    	@Override
    	public void afterNavigateTo(String url, WebDriver driver) {
    		System.out.println("afterNavigateTo trigger, navigate to : " + url);
    	}
    
    	@Override
    	public void beforeClickOn(WebElement webElement, WebDriver driver) {
    		System.out.println("beforeClickOn trigger, " + this.getWebElementInfo(webElement));
    	}
    
    	@Override
    	public void beforeFindBy(By by, WebElement webElement, WebDriver driver) {
    		
    		System.out.println("beforeFindBy trigger, Condition : " + by.toString() + this.getWebElementInfo(webElement));
    	}
    
    	@Override
    	public void beforeNavigateTo(String url, WebDriver driver) {
    		System.out.println("beforeNavigateTo trigger , navigate to : " + url);
    	}
    
    	@Override
    	public void onException(Throwable throwable, WebDriver driver) {
    		try
    		{
    			/* Take screenshot when exception happened. */
    			File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    			/* save screenshot to file. */
    			FileUtils.copyFile(scrFile, new File("C:\\Workspace\\webdriverEventListenerScreenshot.png"));
    		}catch(IOException ex)
    		{
    			ex.printStackTrace();
    		}
    	}
    
    	@Override
    	public void beforeNavigateBack(WebDriver driver) {
    		System.out.println("beforeNavigateBack trigger. ");
    	}
    
    	@Override
    	public void afterChangeValueOf(WebElement element, WebDriver driver) {
    		System.out.println("afterChangeValueOf trigger. " + this.getWebElementInfo(element));
    	}
    
    	@Override
    	public void beforeChangeValueOf(WebElement element, WebDriver driver) {
    		System.out.println("beforeChangeValueOf trigger. " + this.getWebElementInfo(element));
    	}
    	
    	
    	/* This is a util method that can print webelement's information in console. */
    	private String getWebElementInfo(WebElement element)
    	{
    		String ret = "";
    		if(element!=null)
    		{
    			ret = "element name: " + element.getAttribute("name") + " , element value: " + element.getAttribute("value") + " , tag name : " + element.getTagName();
    		}
    		return ret;
    	}
    }
  2. WebDriverEventListenerExample.java
    This is a TestNG class. It create the event listener, EventFiringWebDriver object and register the listener to it. All of the initialization java code in @BeforeClass method as below.

    	@BeforeClass
    	/* Initiate listener, webdriver and regiser listener. */
    	public void beforeClass() {
    		
    		/* Initiate Firefox driver. */
    		this.driver = new FirefoxDriver();
    		
    		/* Initiate wrapper class object. */
    		this.eventWebDriver = new EventFiringWebDriver(this.driver);
    
    		/* Create a event listener object. */
    		eventListener = new Dev2qaWebDriverEventListener();
    
    		/* Register listener. */
    		this.eventWebDriver.register(eventListener);
    	
    		/* Maximaze Firefox browser window. */
    		this.eventWebDriver.manage().window().maximize();
    	}

    The test method.

    	@Test
    	public void testWebDriverEventListener() {
    		/* Browse an example page. Test script will automatically input user name and password and then click submit button.
    		 * When submit there will popup a javascript Alert dialog. To run this example, you should copy below page to your C:/WorkSpace directory. */
    		this.eventWebDriver.get("file://C:/WorkSpace/webdriverEventListener.html");
    		
    		/* Input username.*/
    		By userNameById = By.id("userName");
    		WebElement userNameWebElement = this.eventWebDriver.findElement(userNameById);
    		userNameWebElement.sendKeys("Jerry");
    		
    		/* Input password.*/
    		By passwordById = By.id("password");
    		WebElement passwordWebElement = this.eventWebDriver.findElement(passwordById);
    		passwordWebElement.sendKeys("dev2qa.com");
    		
    		/* Click submit. This will trigger a javascript alert and then submit the form. */
    		By submitById = By.id("submitBtn");
    		WebElement submitWebElement = this.eventWebDriver.findElement(submitById);
    		
    		/* Because after click submit button, current page change to webdriverEventListenerSuccess.html.
    		 * There is no submit button element in webdriverEventListenerSuccess.html, so there will throw an exception
    		 * in the listener's afterClickOn method. Then the listener's onException method will be triggered.
    		 * And it will take a screenshot and save to hard disk.
    		 * */
    		submitWebElement.click();
    		
    	}

    Output
    webdriver event listener output

  3. webdriverEventListener.html
    You can input username and password in this page. After click submit button, it will go to another page webdriverEventListenerSuccess.html . To run attached java example, you need to save these two html page under C:/WorkSpacewebdriverEventListener.html

    <form action="webdriverEventListenerSuccess.html" method="post">
    UserName: <input type="text" id="userName" name="userName"/><br/>
    Password: <input type="password" id="password" name="password"/><br/>
    <input type="submit" value="Submit" id="submitBtn" name="submitBtn"/>
    </form>

Download “WebDriver-Event-Listener-Code-Example.zip” WebDriver-Event-Listener-Code-Example.zip – Downloaded 84 times – 3 KB

(Visited 1,581 times, 2 visits today)
READ :   Check Broken Links With Selenium WebDriver

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.