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

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

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

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

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

提前谢谢。

EN

Stack Overflow用户

发布于 2019-10-21 14:21:15

您可以在testng中使用Itestlistener进行截图。testng有一个内置的函数,你可以通过它在失败时截屏。

只需创建一个单独的listerner类并粘贴以下代码:

代码语言:javascript
运行
复制
public class listeners implements ITestListener{

//This "Base " is the main class where you can pass the result of test case and write the //code for screenshot  
//I cam creating an object so I can access the method that I created for screenshot in this base class.  
//I am also getting the result name so I can identify which test case it got failed


Base b = new Base();
public void onTestFailure(ITestResult result) { 
    try {
        //Getting the result name by result.getName() method         
        b.getScreenshot( result.getName());
        System.out.println("The Failed test is=="+result.getName());
    }   
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();  
    }  
}   
}  

我在基类中创建了这个getScreenshot方法,并传递了结果名称

代码语言:javascript
运行
复制
public class Base {

public WebDriver InitializeDr() throws IOException {  
    //Creating the method to take screenshot in the Base class    
    public void getScreenshot(String result) throws IOException
    {  
        File dest=new File("D:\\Work\\KT\\Scr Shot\\"+result+"test.png");  
        File src=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  
        FileUtils.copyFile(src, dest);  
    }  
}  
}
票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35287622

复制
相关文章

相似问题

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