如果有人知道如何使用reportNG在selenium webdriver中拍摄失败的测试/方法的屏幕截图,那么请帮助。
如果你提供了代码,那么它是有帮助的,或者提供一些想法来解决这个问题。
提前谢谢。
发布于 2019-10-21 14:21:15
您可以在testng中使用Itestlistener进行截图。testng有一个内置的函数,你可以通过它在失败时截屏。
只需创建一个单独的listerner类并粘贴以下代码:
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方法,并传递了结果名称
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);  
    }  
}  
}https://stackoverflow.com/questions/35287622
复制相似问题