WebDriverEventListener in Selenium WebDriver
WebDriverEventListener is provided in WebDriver to track events that take place in WebDriver during script execution. But you would be thinking “Why I am talking about this and who cares to take the track on Events in WebDriver script execution”. Then I would say one, “Every automation engineer “and they do care about it and closely want to see how all actions are triggered either this is clicked event on a button, URL or navigating back and forward in browser or some exception occur and Some time they thought like oh God what is happening and some time they talk themselves to find the answer, to take screenshot once some exceptions occur during execution. So WebDriverEventListener is just the answer of all the queries that run into each of the test developer or automation engineer around the globe.
If you want to read more post on Selenium then visit Selenium WebDriver Page and take Selenium WebDriver Quiz to see the area of improvement.
So let’s see how to implement it…So remember these thumb rules
1- Create a class to override events just by Implementing WebDriverEventListener interface or by Extending AbstractWebDriverEventListener abstract class where WebDriverEventListener is an interface which has near about 15 methods that we can override to see after and before tracking of each event that we trigger through our scripts.
2- Initializing WebDriver instance in our actual test class or test script like this
WebDriver driver = new FirefoxDriver();
3- Creating one object of EventFiringWebDriver just by calling one of its constructor which takes an instance of WebDriver as a parameter like this
EventFiringWebDriver eDriver = new EventFiringWebDriver(driver);
4- Registering EventListener with EventFiringWebDriver:
So let’s call the class where we have just implemented WebDriverEventListener or have extended AbstractWebDriverEventListener. Suppose the name of that class is OverrideClass()
So creating one object of this class like this
OverrideClass oc = new OverrideClass();
Now let’s register the object of this event listener class with EventFiringWebDriver
eDriver.register(oc);
Above we have seen all the thumb rules now its time to see its working in real time.
So let’s create the class in which we will override the events function and here i am going to manipulate the methods which are related to driver.navigate().back();
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.events.WebDriverEventListener; public class OverrideClass implements WebDriverEventListener{ @Override public void afterChangeValueOf(WebElement arg0, WebDriver arg1) { // TODO Auto-generated method stub } @Override public void afterClickOn(WebElement arg0, WebDriver arg1) { // TODO Auto-generated method stub } @Override public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) { // TODO Auto-generated method stub } @Override public void afterNavigateBack(WebDriver driver) { System.out.println("After clicking back "+ driver.getCurrentUrl()); } @Override public void afterNavigateForward(WebDriver arg0) { // TODO Auto-generated method stub } @Override public void afterNavigateTo(String arg0, WebDriver arg1) { // TODO Auto-generated method stub } @Override public void afterScript(String arg0, WebDriver arg1) { // TODO Auto-generated method stub } @Override public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) { // TODO Auto-generated method stub } @Override public void beforeClickOn(WebElement arg0, WebDriver arg1) { // TODO Auto-generated method stub } @Override public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) { // TODO Auto-generated method stub } @Override public void beforeNavigateBack(WebDriver driver) { System.out.println("Before clicking Back"+driver.getCurrentUrl()); } @Override public void beforeNavigateForward(WebDriver arg0) { // TODO Auto-generated method stub } @Override public void beforeNavigateTo(String arg0, WebDriver arg1) { // TODO Auto-generated method stub } @Override public void beforeScript(String arg0, WebDriver arg1) { // TODO Auto-generated method stub } @Override public void onException(Throwable arg0, WebDriver arg1) { // TODO Auto-generated method stub } }
Now let’s see the test script where we are going to implement everything that we have seen above
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.events.EventFiringWebDriver; public class ImplementClass { /** * @author = Dwarika */ public static void main(String[] args) { //Initializing instance of Webdriver WebDriver wDriver = new FirefoxDriver(); //Initializing instance of EventFiringWebDriver EventFiringWebDriver driver = new EventFiringWebDriver(wDriver); //Now create object of OverrideClass to register it with EventFiringWebDriver OverrideClass oc = new OverrideClass(); //Registering it with EventFiringWebDriver driver.register(oc); driver.get("http://gmail.com"); driver.get("http://outlook.com"); driver.navigate().back(); /* * Here we are just using driver.navigate().back() then two method of OverrideClass * will be called before and after this event is triggered * */ } }
Once we will execute this program, we will see these results
Before clicking Back https://login.live.com/login.srf?wa=wsignin1.0&ct=1402256444&rver=6.1.6206.0&sa=1&ntprob=-1&wp=MBI_SSL_SHARED&wreply=https:%2F%2Fmail.live.com%2F%3Fowa%3D1%26owasuffix%3Dowa%252f&id=64855&snsc=1&cbcxt=mail After clicking back https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1<mpl=default<mplcache=2&emr=1
So you can see that two URL(s) are printed but from where…then think once again and remember you have written these code in OverridClass and once navigate().back(); is called it first called beforeNavigateBack(WebDriver driver) method and later afterNavigateBack(WebDriver driver) method implicitly..and so these results are in Console.
Please don’t forget to share it if you like this post !!
Reference:
seleniumhq.org
Really useful thnks 🙂
Respected Sir,
The site was really helpful, I want to use Event Listeners in Page Object Patter following Page-factory.
Can you guide me.
Its urgent.
Create a class that returns a event listener and extend it in all places and it will work
Respected Sir,
can you show an example? means any dummy sample. I am new to this.
Respected Sir,
Can you guide me with an example. Any sample