What is Webdriver Listeners-
Hello Welcome to Selenium tutorial in this post we will talk about WebDriver Listener,EventFiringWebDriver and WebDriverEventListener in detail.
I know all of you might have heard of Listeners but what exactly Listeners is let us discuss today.
In general, terms, Listeners are whom that listen to you and my favorite quotes is “Be a better listener”.
what is listeners in selenium webdriver
If you talk about Webdriver Listener so you should make a note of some classes and interfaces that we will use so will talk about it.
1- WebDriverEventListener – This is an interface, which have some predefined methods so we will implement all of these methods.
2-EventFiringWebDriver- This is an class that actually fire Webdriver event.
Why we are using Webdriver Listeners
If you talk about Webdriver we are doing some activity like type, click, navigate etc this is all your events which you are performing on your script so we should have activity which actually will keep track of it.
Take an example if you perform click then what should happen before click and after click.
To capture these events we will add listener that will perform this task for us.
How to implement Listener in our Script
Program for what is listeners in selenium webdriver
Step 1- Create a new Class that will implement WebDriverEventListener methods
package listerDemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
public class ActivityCapture implements WebDriverEventListener {
@Override
public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
}
@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {
System.out.println("After click "+arg0.toString());
}
@Override
public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
System.out.println("After FindBy "+arg0.toString());
}
@Override
public void afterNavigateBack(WebDriver arg0) {
System.out.println("After navigating back "+arg0.toString());
}
@Override
public void afterNavigateForward(WebDriver arg0) {
System.out.println("After navigating forword "+arg0.toString());
}
@Override
public void afterNavigateTo(String arg0, WebDriver arg1) {
System.out.println("After navigating "+arg0.toString());
System.out.println("After navigating "+arg1.toString());
}
@Override
public void afterScript(String arg0, WebDriver arg1) {
}
@Override
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
}
@Override
public void beforeClickOn(WebElement arg0, WebDriver arg1) {
System.out.println("before click "+arg0.toString());
}
@Override
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
System.out.println("before FindBY "+arg0.toString());
}
@Override
public void beforeNavigateBack(WebDriver arg0) {
System.out.println("Before navigating back "+arg0.toString());
}
@Override
public void beforeNavigateForward(WebDriver arg0) {
System.out.println("Before navigating Forword "+arg0.toString());
}
@Override
public void beforeNavigateTo(String arg0, WebDriver arg1) {
System.out.println("Before navigating "+arg0.toString());
System.out.println("Before navigating "+arg1.toString());
}
@Override
public void beforeScript(String arg0, WebDriver arg1) {
}
@Override
public void onException(Throwable arg0, WebDriver arg1) {
System.out.println("Testcase done"+arg0.toString());
System.out.println("Testcase done"+arg1.toString());
}
Let’s Discuss one of these methods
@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {
System.out.println(“After click “+arg0.toString());
}
In above method we are simply printing on console and this method will automatically called once click events done. In same way you have to implement on methods.
Note- We generally use Listener to generate log events
Step 2- Now create your simple script, create EventFiringWebDriver object, and pass your driver object.
EventFiringWebDriver event1=new EventFiringWebDriver(driver);
Step 3- Create an object of the class who has implemented all the method of WebDriverEventListener so in our case ActivityCapture is a class who has implemented the same.
ActivityCapture handle=new ActivityCapture();
Step 4- Now register that event using register method and pass the object of ActivityCapture class
event1.register(handle);
We are done now using event1 object write your script so now let us implement the same
Implementation of Webdriver listener
package testcases;
import listerDemo.ActivityCapture;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
public class ListnerDemo {
public static void main(String []args){
System.out.println("Started");
WebDriver driver=new FirefoxDriver();
EventFiringWebDriver event1=new EventFiringWebDriver(driver);
ActivityCapture handle=new ActivityCapture();
event1.register(handle);
event1.navigate().to("http://www.facebook.com");
event1.findElement(By.id("email")).sendKeys("asdsadsa");
event1.findElement(By.id("loginbutton")).click();
event1.quit();
event1.unregister(handle);
System.out.println("End");
}
Hello Welcome to Selenium tutorial in this post we will talk about WebDriver Listener,EventFiringWebDriver and WebDriverEventListener in detail.
I know all of you might have heard of Listeners but what exactly Listeners is let us discuss today.
In general, terms, Listeners are whom that listen to you and my favorite quotes is “Be a better listener”.
what is listeners in selenium webdriver
If you talk about Webdriver Listener so you should make a note of some classes and interfaces that we will use so will talk about it.
1- WebDriverEventListener – This is an interface, which have some predefined methods so we will implement all of these methods.
2-EventFiringWebDriver- This is an class that actually fire Webdriver event.
Why we are using Webdriver Listeners
If you talk about Webdriver we are doing some activity like type, click, navigate etc this is all your events which you are performing on your script so we should have activity which actually will keep track of it.
Take an example if you perform click then what should happen before click and after click.
To capture these events we will add listener that will perform this task for us.
How to implement Listener in our Script
Program for what is listeners in selenium webdriver
Step 1- Create a new Class that will implement WebDriverEventListener methods
package listerDemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
public class ActivityCapture implements WebDriverEventListener {
@Override
public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
}
@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {
System.out.println("After click "+arg0.toString());
}
@Override
public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
System.out.println("After FindBy "+arg0.toString());
}
@Override
public void afterNavigateBack(WebDriver arg0) {
System.out.println("After navigating back "+arg0.toString());
}
@Override
public void afterNavigateForward(WebDriver arg0) {
System.out.println("After navigating forword "+arg0.toString());
}
@Override
public void afterNavigateTo(String arg0, WebDriver arg1) {
System.out.println("After navigating "+arg0.toString());
System.out.println("After navigating "+arg1.toString());
}
@Override
public void afterScript(String arg0, WebDriver arg1) {
}
@Override
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
}
@Override
public void beforeClickOn(WebElement arg0, WebDriver arg1) {
System.out.println("before click "+arg0.toString());
}
@Override
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
System.out.println("before FindBY "+arg0.toString());
}
@Override
public void beforeNavigateBack(WebDriver arg0) {
System.out.println("Before navigating back "+arg0.toString());
}
@Override
public void beforeNavigateForward(WebDriver arg0) {
System.out.println("Before navigating Forword "+arg0.toString());
}
@Override
public void beforeNavigateTo(String arg0, WebDriver arg1) {
System.out.println("Before navigating "+arg0.toString());
System.out.println("Before navigating "+arg1.toString());
}
@Override
public void beforeScript(String arg0, WebDriver arg1) {
}
@Override
public void onException(Throwable arg0, WebDriver arg1) {
System.out.println("Testcase done"+arg0.toString());
System.out.println("Testcase done"+arg1.toString());
}
Let’s Discuss one of these methods
@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {
System.out.println(“After click “+arg0.toString());
}
In above method we are simply printing on console and this method will automatically called once click events done. In same way you have to implement on methods.
Note- We generally use Listener to generate log events
Step 2- Now create your simple script, create EventFiringWebDriver object, and pass your driver object.
EventFiringWebDriver event1=new EventFiringWebDriver(driver);
Step 3- Create an object of the class who has implemented all the method of WebDriverEventListener so in our case ActivityCapture is a class who has implemented the same.
ActivityCapture handle=new ActivityCapture();
Step 4- Now register that event using register method and pass the object of ActivityCapture class
event1.register(handle);
We are done now using event1 object write your script so now let us implement the same
Implementation of Webdriver listener
package testcases;
import listerDemo.ActivityCapture;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
public class ListnerDemo {
public static void main(String []args){
System.out.println("Started");
WebDriver driver=new FirefoxDriver();
EventFiringWebDriver event1=new EventFiringWebDriver(driver);
ActivityCapture handle=new ActivityCapture();
event1.register(handle);
event1.navigate().to("http://www.facebook.com");
event1.findElement(By.id("email")).sendKeys("asdsadsa");
event1.findElement(By.id("loginbutton")).click();
event1.quit();
event1.unregister(handle);
System.out.println("End");
}
No comments:
Post a Comment