【问题标题】:How to use screeshotcapture in IReporter interface in Selenium using TestNG?如何使用 TestNG 在 Selenium 的 IReporter 界面中使用 screeshotcapture?
【发布时间】:2019-06-03 03:21:25
【问题描述】:

我在Selenium 中使用IReporter TestNG 接口,但是如何截取屏幕截图并为失败的测试用例添加范围报告?

请帮我找到解决办法。

【问题讨论】:

    标签: java selenium testng listener


    【解决方案1】:

    以下是将失败的测试用例屏幕截图附加到范围报告的代码。

    • 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):在每次测试用例执行后执行,并将失败的测试用例截图附加到Extent报告中。
    @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>
    

    【讨论】:

    • @arun salvi QA:以上答案解决了您的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    相关资源
    最近更新 更多