1.创建类LogEventListener.java, 如下:
package com.demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
/**
* 用一个类扩展web driver自带的事件监听器,可以实现许多有趣的功能。
* 比如自动log
* a customer event listener
*/
public class LogEventListener implements WebDriverEventListener {
public void beforeNavigateTo(String url, WebDriver driver) {
System.out.println("转向前URL: '" + url + "'");
}
public void afterNavigateTo(String url, WebDriver driver) {
System.out.println("转向后URL:'" + url + "'");
}
public void beforeChangeValueOf(WebElement element, WebDriver driver) {
System.out.println("Value of the:" + element.toString()
+ " before any changes made");
}
public void afterChangeValueOf(WebElement element, WebDriver driver) {
System.out.println("Element value changed to: " + element.toString());
}
public void beforeClickOn(WebElement element, WebDriver driver) {
System.out.println("试图单击对象: " + element.toString());
}
public void afterClickOn(WebElement element, WebDriver driver) {
System.out.println("单击对象: " + element.toString());
}
public void beforeNavigateBack(WebDriver driver) {
System.out.println("Navigating back to previous page");
}
public void afterNavigateBack(WebDriver driver) {
System.out.println("Navigated back to previous page");
}
public void beforeNavigateForward(WebDriver driver) {
System.out.println("Navigating forward to next page");
}
public void afterNavigateForward(WebDriver driver) {
System.out.println("Navigated forward to next page");
}
public void onException(Throwable error, WebDriver driver) {
System.out.println("Exception occured: " + error);
}
public void beforeFindBy(By by, WebElement element, WebDriver driver) {
System.out.println("试图寻找对象 : " + by.toString());
}
public void afterFindBy(By by, WebElement element, WebDriver driver) {
System.out.println("找到对象 : " + by.toString());
}
/*
* non overridden methods of WebListener class
*/
public void beforeScript(String script, WebDriver driver) {
}
public void afterScript(String script, WebDriver driver) {
}
@Override
public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
// TODO Auto-generated method stub
}
@Override
public void afterNavigateRefresh(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
// TODO Auto-generated method stub
}
@Override
public void beforeNavigateRefresh(WebDriver arg0) {
// TODO Auto-generated method stub
}
}
2.在创建WebEventListener监听类,如下:
package com.demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.AbstractWebDriverEventListener;
public class WebEventListener extends AbstractWebDriverEventListener {
public void beforeNavigateTo(String url, WebDriver driver) {
System.out.println("打开前url: '" + url + "'");
}
public void afterNavigateTo(String url, WebDriver driver) {
System.out.println("打开url:'" + url + "'");
}
public void beforeClickOn(WebElement element, WebDriver driver) {
System.out.println("试图单击对象: " + element.toString());
}
public void afterClickOn(WebElement element, WebDriver driver) {
System.out.println("单击对象: " + element.toString());
}
public void onException(Throwable error, WebDriver driver) {
System.out.println("Error occurred: " + error);
}
}
3.使用监听,如下:
package com.demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class ListenerTest {
private WebDriver driver;
private EventFiringWebDriver e_driver;
private WebEventListener eventListener;
private WebDriverWait wait;
private String appURL = "http://www.google.com";
public int waitTime = 10;
private String headerText = "One account. All of Google.";
private String errMessage = "The email and password you entered don't match.";
@BeforeClass()
public void setUp() {
driver = new FirefoxDriver();
wait = new WebDriverWait(driver, waitTime);
e_driver = new EventFiringWebDriver(driver);
eventListener = new WebEventListener();
e_driver.register(eventListener);
e_driver.manage().window().maximize();
e_driver.get(appURL);
}
@Test
public void testEventsONE() {
System.out.println("***** Executing Test ONE ***** ");
e_driver.findElement(By.linkText("Gmail")).click();
String pageHeaderText = e_driver.findElement(By.tagName("h1")).getText();
Assert.assertTrue(pageHeaderText.equalsIgnoreCase(headerText));
}
@Test
public void testEventsTWO() {
System.out.println("***** Executing Test Two ***** ");
e_driver.findElement(By.id("Email")).sendKeys("username");
e_driver.findElement(By.id("next")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
e_driver.findElement(By.id("Passwd")).sendKeys("password");
e_driver.findElement(By.id("signIn")).click();
String pageHeaderText = e_driver.findElement(By.id("errormsg_0_Passwd")).getText();
Assert.assertTrue(pageHeaderText.equalsIgnoreCase(errMessage));
}
@AfterClass()
public void tearDown() {
if (e_driver != null) {
e_driver.quit();
}
}
}
运行查看控制台打印的信息