首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用ReportNG在selenium webdriver中截取测试失败的屏幕截图

如何使用ReportNG在selenium webdriver中截取测试失败的屏幕截图
EN

Stack Overflow用户
提问于 2016-02-09 16:55:07
回答 4查看 736关注 0票数 1

如果有人知道如何使用reportNG在selenium webdriver中拍摄失败的测试/方法的屏幕截图,那么请帮助。

如果你提供了代码,那么它是有帮助的,或者提供一些想法来解决这个问题。

提前谢谢。

EN

回答 4

Stack Overflow用户

发布于 2016-02-09 17:12:46

是的,这是可能的。

代码语言:javascript
运行
复制
WebDriver firfoxDriver = new FirefoxDriver();
firfoxDriver.get("https://stackoverflow.com/");
File file = ((TakesScreenshot)firfoxDriver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(file, new File("E:\\Data\\ss.png"));
票数 0
EN

Stack Overflow用户

发布于 2016-02-09 17:34:48

或者,您可以使用以下命令:

代码语言:javascript
运行
复制
@AfterMethod(alwaysRun=true)
public void catchExceptions(ITestResult result){
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
String methodName = result.getName();
if(!result.isSuccess()){
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    try {
        FileUtils.copyFile(scrFile, new File((String)     PathConverter.convert("failure_screenshots/"+methodName+"_"+formater.format(calendar.getTime())+".png")));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2016-02-09 19:06:27

好的,这取决于你的框架,在方法失败后控件的去向。

假设在您的例子中是在catch异常块中,然后将代码复制到catch块中。

代码语言:javascript
运行
复制
File file = ((TakesScreenshot)firfoxDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("YOUR PATH"+Filename+.JPG));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35287622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档