51Testing软件测试论坛
标题:
testng对失败时截图处理
[打印本页]
作者:
天天酷派哦
时间:
2018-4-10 15:23
标题:
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方法即可,
复制代码
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2