近要在框架中添加case失敗時(shí),要自動(dòng)截圖,坐下總結(jié)。
1.只針對(duì)webdriver的異常截圖,重寫onException,該方法由于只針對(duì)webdriver拋的異常時(shí)才能截圖,有一定的限制
public void onException(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 = new Date();
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();
}
}
測(cè)試類,要用EventFiringWebDriver ,并注冊(cè)MyListen
public static void main(String args[]) {
String key = "webdriver.chrome.driver";
String value = "D:/BaiduYunDownload/selenium/chromedriver.exe";
System.setProperty(key, value);
WebDriver dr = new ChromeDriver();
EventFiringWebDriver event = new EventFiringWebDriver(dr);
MyListen ml = new MyListen();
event.register(ml);
dr = event;
dr.get("http://www.baidu.com");
dr.findElement(By.id("kw1")).sendKeys("test");
//System.out.println(5/0);
Assert.assertEquals("haha", event.findElement(By.id("su")).getAttribute("value"));
event.quit();
}