testng对失败时截图处理
1.截图类:public class ScreenShot {
public WebDriver driver;
public ScreenShot(WebDriver driver) {
this.driver = driver;
}
private void takeScreenshot(String screenPath) {
try {
File scrFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(screenPath));
} catch (IOException e) {
System.out.println("Screen shot error: " + screenPath);
}
}
public void takeScreenshot() {
String screenName = String.valueOf(new Date().getTime()) + ".jpg";
File dir = new File("test-output/snapshot");
if (!dir.exists())
dir.mkdirs();
String screenPath = dir.getAbsolutePath() + "/" + screenName;
this.takeScreenshot(screenPath);
}
}
2.我们可以用testng的一个监听器来监听错误时截图:
public class DotTestListener extends TestListenerAdapter {
@Override
public void onTestFailure(ITestResult tr) {
}
}
3.也就是说我们只需要在onTestFailure方法里面调用ScreenShot类里面的takeScreenshot方法即可,
页:
[1]