Web driver Listeners:
Whenever we talk about Webdriver we are doing some activity like
type, click, navigate etc .These all
events which we perform on our script .
In order to have more logs from the Webdriver so that you can debug your scripts or
may be log more information about your tests.You must have always craved for
more logs from the Webdriver so that you can debug your scripts or may be log
more information about your tests.
When we talk about Webdriver Listener so we should make a note of
some classes and interfaces .
1- WebDriverEventListener – This is an interface, which have some
predefined methods so we will implement all of these methods to catch the webdriver events.
2.EventFiringWebDriver- This is an class that actually fire
Webdriver event
EventFiringWebDriver is used to wrap your webdriver around to
throw events.
Example:
Create a new Class that will implement WebDriverEventListener methods
package com.rameshsoft.rameshselenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import
org.openqa.selenium.support.events.WebDriverEventListener;
public class WebDriverListeners implements WebDriverEventListene r{
@Override
public void
afterChangeValueOf(WebElement arg0, WebDriver arg1,
CharSequence[]
arg2) {
System.out.println("After
change value is :"+arg0.toString());
}
@Override
public void
afterClickOn(WebElement arg0, WebDriver arg1) {
System.out.println("After
click on webElement:"+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
navigate back "+arg0.toString());
}
Now create your simple script, create
EventFiringWebDriver object, and pass your driver object.
Create an object of the class who has
implemented all the method of WebDriverEventListener so in our case WebDriverListeners is a class who
has implemented the same.
Now register that event using register method and pass the object
of WebDriverListeners class
package com.rameshsoft.rameshselenium;
import java.util.concurrent.TimeUnit;
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 WebDriverListenerDemo {
public
static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver
driver=new FirefoxDriver();
EventFiringWebDriver
efwd=new EventFiringWebDriver(driver);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);
WebDriverListeners
event=new WebDriverListeners();
efwd.register(event);
efwd.navigate().to("https://www.gmail.com");
efwd.findElement(By.id("Email")).sendKeys("rameshsoft.selenium");
efwd.findElement(By.id("next")).click();
Thread.sleep(2000);
efwd.findElement(By.id("Passwd")).sendKeys("12345");
efwd.findElement(By.id("signIn")).click();
efwd.quit();
efwd.unregister(event);
System.out.println("End
of the script");
}
}
No comments:
Post a Comment