最近要在框架中添加case失败时,要自动截图,主要又两种方式,思想都是在抛异常的时候,捕获到异常,并作页面截图处理。今天坐下总结。
一、第一种方式,重写onException方法
只针对webdriver的异常截图,该方法由于只针对webdriver抛的异常时才能截图,有一定的限制
a.继承AbstractWebDriverEventListener类,重写onException方法,
public voidonException(java.lang.Throwable throwable,
WebDriver driver){
Throwable cause=throwable.getCause();
/*
String cause = throwable.getClass().getName();
ScreenshotException ec = new ScreenshotException(cause);
*/
System.out.println(throwable.getClass().getName());
System.out.println("onException=" +cause);
Date currentTime= newDate();
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
String dateString=formatter.format(currentTime);
File scrFile=((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);try{
File screenshot= new File("D:/ddd/"
+ dateString + ".png");
FileUtils.copyFile(scrFile,screenshot);
}catch(IOException e) {
e.printStackTrace();
}
}
b.测试类,要用EventFiringWebDriver ,并注册MyListen
public static voidmain(String args[]) {
Strin