如何使用TestNG在Selenium的IReporter界面中使用screeshotcapture?

时间:2019-06-03 03:21:25

标签: java selenium testng listener

我在IReporter TestNG中使用Selenium界面,但是如何捕获屏幕截图并在范围报告中添加失败的测试用例?

请帮助我找到解决方法。

1 个答案:

答案 0 :(得分:0)

下面是将失败的测试用例屏幕快照附加到范围报告的代码。

  • MyReporterClass实现IReporter接口:它迭代测试套件中的测试用例并保存每个测试用例的状态。
public class MyReporterClass implements IReporter {

   @Override
   public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
      //Iterating over each suite included in the test
      for (ISuite suite : suites) {
         //Following code gets the suite name
         String suiteName = suite.getName();
         //Getting the results for the said suite
         Map<String, ISuiteResult> suiteResults = suite.getResults();
         for (ISuiteResult sr : suiteResults.values()) {
            ITestContext tc = sr.getTestContext();
            System.out.println("Passed tests for suite '" + suiteName +
               "' is:" + tc.getPassedTests().getAllResults().size());
            System.out.println("Failed tests for suite '" + suiteName +
               "' is:" + tc.getFailedTests().getAllResults().size());
            System.out.println("Skipped tests for suite '" + suiteName +
               "' is:" + tc.getSkippedTests().getAllResults().size());
         }
      }
   }
}
  • getScreenshot()方法:捕获屏幕快照并返回屏幕快照的目标路径。
public class ExtentReportsClass{
public static String getScreenshot(WebDriver driver, String screenshotName) throws Exception {
//below line is just to append the date format with the screenshot name to avoid duplicate names 
String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
 TakesScreenshot ts = (TakesScreenshot) driver;
 File source = ts.getScreenshotAs(OutputType.FILE);
//after execution, you could see a folder "FailedTestsScreenshots" under src folder
 String destination = System.getProperty("user.dir") + "/FailedTestsScreenshots/"+screenshotName+dateName+".png";
 File finalDestination = new File(destination);
 FileUtils.copyFile(source, finalDestination);
//Returns the captured file path
 return destination;
  }
}
  • @AfterMethod public void getResult(ItestResult result):在每次测试用例执行后执行,并将失败的测试用例屏幕快照附加到范围报告。
@AfterMethod
 public void getResult(ITestResult result) throws IOException{
 if(result.getStatus() == ITestResult.FAILURE){
 logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
 logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
 //To capture screenshot path and store the path of the screenshot in the string "screenshotPath"
String screenshotPath = ExtentReportsClass.getScreenshot(driver, result.getName());
 //To add it in the extent report 
 logger.log(LogStatus.FAIL, logger.addScreenCapture(screenshotPath));
 }else if(result.getStatus() == ITestResult.SKIP){
 logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
 }
  • testng.xml文件:在xml文件中包括以下侦听器标记。
<listeners>
  <listener class-name="packagename.MyReporterClass" />
</listeners>