Selenium provides a number of event listening functions to track events during script execution.
How it works?
These listener can listen events in the Webdriver registered with listener. Events can be any type of event or action, such as element click, which changes the value of the input box or even the exceptions.
To enable this feature:
1. Create a user-defined event listener class.
2. Create a eventfiringwebdriverobject by implementing the Webdriver interface
3. And register the Listener to the Eventfiringwebdriver instance.
Q: How do I create my own event listener class?
A: By implements the Webdrivereventlistener interface (not recommended).
or by inheriting the Abstractweddrivereventlistener class. (recommended)
First show how to implement the Webdrivereventlistener interface (not recommended):
PackageMain.test.org.seleniummonster.com.demo.eventlistenerdemo;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.support.events.WebDriverEventListener; Public classEventListenerType1ImplementsWebdrivereventlistener {@Override Public voidafterchangevalueof (webelement arg0, Webdriver arg1) {//Things to being done after changing the value in the webelement Argo} @Override Public voidAfterclickon (webelement arg0, Webdriver arg1) {//Things to being done after clicking an element arg0} @Override Public voidAfterfindby (by arg0, Webelement arg1, Webdriver arg2) {//Things to being done after FindBy of webelement arg1} @Override Public voidAfternavigateback (Webdriver arg0) {//things to being done after navigating back} @Override Public voidAfternavigateforward (Webdriver arg0) {//Things to being done after navigating forward} @Override Public voidAfternavigateto (String arg0, Webdriver arg1) {//things to being done after navigating to the given URL say, arg0} @Override Public voidAfterscript (String arg0, Webdriver arg1) {//Things to is done after script execution} @Override Public voidbeforechangevalueof (webelement arg0, Webdriver arg1) {//Things to being done before changing value in the Webelement arg0} @Override Public voidBeforeclickon (webelement arg0, Webdriver arg1) {//Things to being done before clicking an element arg0} @Override Public voidBeforefindby (by arg0, Webelement arg1, Webdriver arg2) {//Things to being done before a Findby of a Web element arg1} @Override Public voidBeforenavigateback (Webdriver arg0) {//Things to being done before navigating back} @Override Public voidBeforenavigateforward (Webdriver arg0) {//Things to being done before Navigating forward} @Override Public voidBeforenavigateto (String arg0, Webdriver arg1) {//Things to being done before navigating to a URL arg0} @Overridepublicvoid Beforescript (String arg0, Webdriver arg1) {//Things to being done before JavaScript execution} @Override Public voidonexception (Throwable arg0, Webdriver arg1) {System.out.println ("There is a exception in the script, please find the below error description"); Arg0.printstacktrace ();}}
In this way, we only rewrite the onexception (), and the method has not changed, but it is overridden again. No way.
Then we'll introduce the recommended method. Abstractwebdrivereventlistener is an abstract class that implements the Webdrivereventlistener interface, It webdrivereventlistener all of the methods (except, of course, an empty override). We can inherit Abstractwebdrivereventlistener class to define the method we are interested in, see the following example: Eventlistenertype2.java
PackageMain.test.org.seleniummonster.com.demo.eventlistenerdemo;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.support.events.AbstractWebDriverEventListener; Public classEventListenerType2extendsAbstractwebdrivereventlistener {/*** @authorSelenium Monster*/@Override Public voidonexception (Throwable arg0, Webdriver arg1) {System.out.println ("There is a exception in the script, please find the below error description"); Arg0.printstacktrace ();}}
The code is short, and the next step is registering the Listener with Webdriver instance
Here's an example: Create an instance of the Eventfiringwebdriver class, we define an instance of the listener class. Register our defined listener on the Eventfiringwebdriver object using the Register () method. Can register as many listener as you whish.
* If you had definitions for a action in all the listener and all of them would be executed.*
PackageMain.test.org.seleniummonster.com.demo.eventlistenerdemo;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.openqa.selenium.support.events.EventFiringWebDriver; Public classEventfiringwebdriverexample {/*** @authorSelenium Monster*/ Public Static voidMain (string[] args) {//Create a Webdriver instanceWebdriver Driver =Newfirefoxdriver ();//Creating an Eventfiringwebdriver instanceeventfiringwebdriver eventfiringwd= new eventfiringwebdriver (driver); //Creating instance of EventListener, that we just definedEventListenerType2EventListener1=NewEventListenerType2 ();//Register the Listener with the event firing driver eventfiringwd.register(eventListener1); Eventfiringwd.get ("Https://www.google.co.in/"); Eventfiringwd.findelement (By.classname ("Nosuchclassname"));}}
How to de-enroll:
eventFiringWD=eventFiringWD.unregister(eventListener1);
The main purpose of the monitoring is to reduce the effort to maintain the script, so that the information of the error, more easily locate the problem.
Eventfiringwebdriver Web Event Monitoring (i)