TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Selenium

EventFiringWebDriver

Introduction

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

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 :

beforeNavigateTo();

afterNavigateTo();

beforeFindBy();

afterFindBy();

beforeScript();

afterScript();

MyCustomizationListener

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.

Listing

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.

 

Related Posts

Windows 10 Settings

Selenium /

Add Microsoft Webdriver on Windows OS

Download Selenium Components

Selenium /

Getting Started with Selenium Webdriver

LambdaTest Testing Cloud SaaS Platform

Selenium /

LambdaTest – Testing Cloud SaaS Platform

Selenium /

Selenium 3.0 and Mozilla GeckoDriver

SauceLabs Website

Selenium /

Run an Example Test on SauceLabs

‹ Timeouts Interface in WebDriver API› Selenium Find Element using By ID Attribute

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version