基于Eclipse Java EE IDE for Web Developers.
Version: Mars.2 Release (4.5.2)
新建test4.java
package ST;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver.*;
import org.openqa.selenium.firefox.*;
public class test4 {
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
org.openqa.selenium.support.events.EventFiringWebDriver
eventDriver = new org.openqa.selenium.support.events.EventFiringWebDriver(driver);
//注册事件
eventDriver.register(new MyWebDriverListener());
//打开百度页面,输入selenium,点击搜索
eventDriver.navigate().to("http://www.baidu.com");
eventDriver.findElement(By.id("kw")).sendKeys("selenium");
eventDriver.findElement(By.id("su")).click();
//写一个异常
try{
eventDriver.findElement(By.id("XXXX"));
}catch(Exception e){}
}
}
新建 MyWebDriverListener .java 文件
package ST;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
public class MyWebDriverListener implements WebDriverEventListener{
@Override
public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("更改 后的值为"+arg0.getAttribute("value"));
}
@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
try{
Thread.sleep(4000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("单击的页面元素后的URL为:" + arg1.getCurrentUrl() );
}
@Override
public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
// TODO Auto-generated method stub
System.out.println("找到元素,其条件为"+arg0.toString());
}
@Override
public void afterNavigateBack(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void afterNavigateForward(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void afterNavigateRefresh(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void afterNavigateTo(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("页面发生跳转后的URL为:"+arg1.getCurrentUrl());
}
@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
System.out.println("更改前的值为:"+arg0.getAttribute("value"));
}
@Override
public void beforeClickOn(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("要单击的页面元素为:" + arg0.getAttribute("value"));
}
@Override
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
// TODO Auto-generated method stub
System.out.println("查找元素时的条件为:"+arg0.toString());
}
@Override
public void beforeNavigateBack(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeNavigateForward(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeNavigateRefresh(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeNavigateTo(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("页面发生跳转前的URL为:"+arg1.getCurrentUrl());
}
@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
java.util.Date currentTime = new java.util.Date();
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
String dateString = formatter.format(currentTime);
java.io.File scrFile = ((org.openqa.selenium.TakesScreenshot)arg1).getScreenshotAs(org.openqa.selenium.OutputType.FILE);
try{
java.io.File screenshot = new java.io.File("D:\\"+dateString+".png");
org.apache.commons.io.FileUtils.copyFile(scrFile,screenshot);
}catch(java.io.IOException e){
e.printStackTrace();
}
System.out.println("Error:" + arg0.getMessage());
System.out.println("Picture in:"+ "D:\\"+dateString+".png");
}
}