简体   繁体   English

如何使用ReportNG在Selenium WebDriver中拍摄失败的测试的屏幕截图

[英]How to take screenshot of failed test in selenium webdriver using ReportNG

If anyone know how to take screenshot of failed test/methods in selenium webdriver using reportNG then please help. 如果有人知道如何使用reportNG拍摄硒webdriver中失败的测试/方法的屏幕截图,请提供帮助。

If you provide code then its helpfull or provide some ideas to solve this problem. 如果您提供代码,那么它会有所帮助或提供一些想法来解决此问题。

Thanks in advance. 提前致谢。

Yes, It is possible. 对的,这是可能的。

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"));

Or You can use this: 或者您可以使用以下方法:

@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();
        }
    }
}

Well It's depend on your framework that where the control is going after failing the method. 好吧,方法失败后控件的去向取决于您的框架。

Suppose in your case it is going in catch exception block then copy that code in catch block. 假设在您的情况下,它将进入catch异常块,然后将该代码复制到catch块中。

File file = ((TakesScreenshot)firfoxDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("YOUR PATH"+Filename+.JPG));

You can use Itestlistener in testng to take screenshot. 您可以在testng使用Itestlistener截屏。 testng has an in built function by which you can take screenshot when it fails. testng具有内置功能,通过它您可以在失败时进行屏幕截图。

Just create a seperate listerner class and paste this code: 只需创建一个单独的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();  
    }  
}   
}  

I created this getScreenshot method in the base class and passing the result name 我在基类中创建了此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);  
    }  
}  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM