【问题标题】:extent report only shows the last run result for testng maven selenium java framework范围报告仅显示 testng maven selenium java 框架的最后运行结果
【发布时间】:2020-09-18 00:50:19
【问题描述】:

我正在使用 testng 6.9.10,extentreports 3.1.5。使用 maven surefire 插件并使用 forkcount 和重用 forks ,测试并行运行。 (即当我设置 forkcount -> 2 和reuseforks-> true 时,打开两个chrome浏览器实例,两个测试类并行运行)

mvn test -Dgroups=group1 

(有两个测试类属于 group1)。问题是范围报告仅显示上次运行的结果。

我只在 pom.xml 中包含了侦听器类(不在其他任何地方,不在 @beforeclass 或 @afterclass 作为 BaseTest 类的一部分)

  <property>
    <name>listener</name>
    <value>util.listener.TestExtentListener</value>
  </property>


   <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.0</version>
    <forkCount>2</forkCount>
    <reuseForks>true</reuseForks>

请问有什么办法吗?

public class ExtentManager {

private static ExtentReports extent;
private static String reportFileName = "Test-Automaton-Report"+".html";
private static String fileSeperator = System.getProperty("file.separator");
private static String reportFilepath = System.getProperty("user.dir") +fileSeperator+ "TestReport";
private static String reportFileLocation =  reportFilepath +fileSeperator+ reportFileName;


public static ExtentReports getInstance() {
    if (extent == null)
        createInstance();
    return extent;
}

//Create an extent report instance
public static ExtentReports createInstance() {
    String fileName = getReportPath(reportFilepath);

    ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
    htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
    htmlReporter.config().setChartVisibilityOnOpen(true);
    htmlReporter.config().setTheme(Theme.STANDARD);
    htmlReporter.config().setDocumentTitle(reportFileName);
    htmlReporter.config().setEncoding("utf-8");
    htmlReporter.config().setReportName(reportFileName);
    htmlReporter.config().setTimeStampFormat("EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'");

    extent = new ExtentReports();

    extent.attachReporter(htmlReporter);
    //Set environment details
    extent.setSystemInfo("OS", "Mac");
    extent.setSystemInfo("AUT", "QA");

    return extent;
 }

//Create the report path
private static String getReportPath (String path) {
    File testDirectory = new File(path);
    if (!testDirectory.exists()) {
        if (testDirectory.mkdir()) {
            System.out.println("Directory: " + path + " is created!" );
            return reportFileLocation;
        } else {
            System.out.println("Failed to create directory: " + path);
            return System.getProperty("user.dir");
        }
    } else {
        System.out.println("Directory already exists: " + path);
    }
    return reportFileLocation;
}

}

public class ExtentTestManager {
static Map<Integer, ExtentTest> extentTestMap = new HashMap<Integer, ExtentTest>();
static ExtentReports extent = ExtentManager.getInstance();

public static synchronized ExtentTest getTest() {
    return (ExtentTest) extentTestMap.get((int) (long) (Thread.currentThread().getId()));
}

public static synchronized void endTest() {
    extent.flush();
}

public static synchronized ExtentTest startTest(String testName) {
    ExtentTest test = extent.createTest(testName);
    extentTestMap.put((int) (long) (Thread.currentThread().getId()), test);
    return test;
}
}

public class TestExtentListener implements ITestListener {

ExtentTest test;
private static ThreadLocal<ExtentTest> extentTestThreadLocal = new ThreadLocal<ExtentTest>();


public void onStart(ITestContext context) {
    System.out.println("*** Test Suite " + context.getName() + " started ***");
}

public void onFinish(ITestContext context) {
    System.out.println(("*** Test Suite " + context.getName() + " ending ***"));
    ExtentTestManager.endTest();
    ExtentManager.getInstance().flush();
}

public void onTestStart(ITestResult result) {
    System.out.println(("*** Running test method " + result.getMethod().getMethodName() + "..."));
    test = ExtentTestManager.startTest(result.getMethod().getMethodName());
    extentTestThreadLocal.set(test);
}

public void onTestSuccess(ITestResult result) {
    System.out.println("*** Executed " + result.getMethod().getMethodName() + " test successfully...");
    extentTestThreadLocal.get().log(Status.PASS, "Test passed");
}

public void onTestFailure(ITestResult result) {
    System.out.println("*** Test execution " + result.getMethod().getMethodName() + " failed...");
    extentTestThreadLocal.get().log(Status.FAIL, "Test Failed");
}

public void onTestSkipped(ITestResult result) {
    System.out.println("*** Test " + result.getMethod().getMethodName() + " skipped...");
    extentTestThreadLocal.get().log(Status.SKIP, "Test Skipped");
}

public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
    System.out.println("*** Test failed but within percentage % " + result.getMethod().getMethodName());
}

}

【问题讨论】:

    标签: maven selenium-webdriver testng extentreports selenium-extent-report


    【解决方案1】:

    您可能没有在 java 中使用线程本地类来使范围报告线程安全。否则对象将被覆盖,并且范围报告仅显示活动测试结果。

    你可以这样做:

    ExtentReports extent = ExtentReportGenerator.ExtentReport();
        ExtentTest test;;
    
        private static ThreadLocal<ExtentTest> extent_test = new ThreadLocal<ExtentTest>();
    

    更多细节可以参考这篇博客。

    https://www.automationinja.com/post/thread-safe-extent-report-in-selenium

    【讨论】:

      【解决方案2】:
          If you use xml file to your testclasses all report will appear in extent report 
          
          This is my XML 
          
              <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
          <suite name="TC_WorldAirFares Automation Test Suite">
          
              <listeners>
                  <listener class-name="ExtentReports.ExtentReporterNG" />
              </listeners>
        <suite name="Suite" parallel="instances" thread-count="2">
          <test name="TC Automation Test Suite">
                  <classes>
          
                      <class name="Footerlinks" />
                      <class name="HeaderLinks" />
                      <class name="BookYourFlightsNow" />
                    
                  </classes>
              </test>
          
             
          </suite>
          
          
          keep class for extentreports
          
          here extent report class (XML file listen your extentreport class and generate reports for you test classes
          
              package ExtentReports;
          
          import com.relevantcodes.extentreports.ExtentReports;
          import com.relevantcodes.extentreports.ExtentTest;
          import com.relevantcodes.extentreports.LogStatus;
          import org.testng.*;
          import org.testng.xml.XmlSuite;
          
          import java.io.File;
          import java.util.Calendar;
          import java.util.Date;
          import java.util.List;
          import java.util.Map;
          
          public class ExtentReporterNG implements IReporter {
              private ExtentReports extent;
          
              public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
                                         String outputDirectory) {
                  extent = new ExtentReports(outputDirectory + File.separator
                          + "Extent.html", true);
          
                  for (ISuite suite : suites) {
                      Map<String, ISuiteResult> result = suite.getResults();
          
                      for (ISuiteResult r : result.values()) {
                          ITestContext context = r.getTestContext();
          
                          buildTestNodes(context.getPassedTests(), LogStatus.PASS);
                          buildTestNodes(context.getFailedTests(), LogStatus.FAIL);
                          buildTestNodes(context.getSkippedTests(), LogStatus.SKIP);
                      }
                  }
          
                  extent.flush();
                  extent.close();
              }
          
              private void buildTestNodes(IResultMap tests, LogStatus status) {
                  ExtentTest test;
          
                  if (tests.size() > 0) {
                      for (ITestResult result : tests.getAllResults()) {
                          test = extent.startTest(result.getMethod().getMethodName());
          
                          test.setStartedTime(getTime(result.getStartMillis()));
                          test.setEndedTime(getTime(result.getEndMillis()));
          
                          for (String group : result.getMethod().getGroups())
                              test.assignCategory(group);
          
                          if (result.getThrowable() != null) {
                              test.log(status, result.getThrowable());
                          } else {
                              test.log(status, "Test " + status.toString().toLowerCase()
                                      + "ed");
                          }
          
                          extent.endTest(test);
                      }
                  }
              }
          
              private Date getTime(long millis) {
                  Calendar calendar = Calendar.getInstance();
                  calendar.setTimeInMillis(millis);
                  return calendar.getTime();
              }
          }
      

      【讨论】:

      • 我无法从您的 xml 中看到它正在并行运行。我只在并行运行时遇到问题。对于单线程,它会创建正确的报告。对于并行运行,它只显示上次运行的结果。例如 class1 有 2 个方法标记为 Test。 Class2 有 3 个方法标记为测试。在那种情况下,说 class2 最后完成。所以它只显示了class2的3个方法。它覆盖了 class1 的结果。
      • 当我使用上面的 xml 时,它不会并行运行,但是如果我通过带有 fork 计数的surefire插件运行,虽然它并行运行,但它总是给我一个结果。在目标目录中的 junitreports 内(在 surefire 报告文件夹内)给出正确数量的 xml。 (每个类一个 xml)。然而 testng-results.xml 总是给出一个类结果(类最后结束)。
      猜你喜欢
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      相关资源
      最近更新 更多