范围报告没有给出关于并行执行的正确报告

Extent report is not giving proper report on parallel execution

ReporterClass.Java:

package POM_Classes;

import com.aventstack.extentreports.AnalysisStrategy;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;

public class ReporterClass {

    public static ExtentHtmlReporter html;
    public ExtentReports extent;
    public ExtentTest test, suiteTest;
    public String testCaseName, testNodes, testDescription, category, authors;

    public void startResult() {
        html = new ExtentHtmlReporter("./reports/result.html");
        html.setAppendExisting(true);
        extent = new ExtentReports();

        extent.attachReporter(html);        
    }

    /*public ExtentTest startTestModule(String testCaseName, String testDescription) {
        suiteTest = extent.createTest(testCaseName, testDescription);
        return suiteTest;
    }*/

    public ExtentTest startTestCase(String testName) {
        System.out.println(testName);
        test = extent.createTest(testName);
        return test;
    }

    public void reportStep(String desc, String status) {
        if (status.equalsIgnoreCase("PASS")) {
            test.pass(desc);
        } else if (status.equalsIgnoreCase("FAIL")) {
            test.fail(desc);
        } else if (status.equalsIgnoreCase("WARNING")) {
            test.warning(desc);
        } else if (status.equalsIgnoreCase("INFO")) {
            test.info(desc);
        }
    }

    public void endTestcase() {
        extent.setAnalysisStrategy(AnalysisStrategy.CLASS);
    }

    public void endResult() {
        extent.flush();
    }

}

用法:

@Test   
    public void 961_NavigateToMyAlertsAndAddNewAlerts()
            throws IOException, InterruptedException, ATUTestRecorderException, APIException {

        driver = launchTargetUrl(this.getClass().getSimpleName().toString());
        startResult();
        test = startTestCase(Thread.currentThread().getStackTrace()[1].getMethodName().toString()); 
        LoginApplication(driver,transactionusername, transactionuserpassword,test);
        HomePage homePage = new HomePage(driver,test);
        homePage.MyAlerts.click();
        MyAlerts myalerts = new MyAlerts(driver,test);
        String selectedcardName;
        selectedcardName = driver.findElement(myalerts.cardName).getText().trim();
        System.out.println(selectedcardName);       
    }



    @AfterMethod
    public void afterMethod(ITestResult result) throws ATUTestRecorderException {   
        resultOfTest(result);
        endTestcase();
        endResult();
        closeBrowsers(driver);
    }

第一个完成的测试用例有报告,如果另一个测试用例完成,那么该结果将覆盖旧的。

如果我更改 public static ExtentReports 范围;然后它只维护线程,所以它只记录一个测试用例,而另一个并行执行甚至没有记录。如何解决这个问题?

好的,给你。我还没有对此进行测试,但这应该允许您并行使用 Extent。

记者:

public abstract class ReporterClass {

    private static final ExtentReports EXTENT = ExtentManager.getInstance();

    public ExtentTest test, suiteTest;
    public String testCaseName, testNodes, testDescription, category, authors;

    public synchronized ExtentTest startTestCase(String testName) {
        System.out.println(testName);
        return ExtentTestManager.createTest(testName);
    }

    public synchronized void reportStep(String desc, String status) {
        if (status.equalsIgnoreCase("PASS")) {
            ExtentTestManager.getTest().pass(desc);
        } else if (status.equalsIgnoreCase("FAIL")) {
            ExtentTestManager.getTest().fail(desc);
        } else if (status.equalsIgnoreCase("WARNING")) {
            ExtentTestManager.getTest().warning(desc);
        } else if (status.equalsIgnoreCase("INFO")) {
            ExtentTestManager.getTest().info(desc);
        }
    }

    public synchronized void endResult() {
        EXTENT.flush();
    }

    @BeforeMethod
    public synchronized void beforeMethod(Method method) {
        startTestCase(method.getName());
    }

    @AfterMethod
    public synchronized void afterMethod(ITestResult result) throws ATUTestRecorderException {   
        reportStep(result.getThrowable(), result.getStatus());
        endResult();
        closeBrowsers(driver);
    }

}

基地:

public abstract class BaseClass extends ReporterClass {

    // .. abstractions

}

扩展实用程序:

public class ExtentTestManager {

    static Map<Integer, ExtentTest> extentTestMap = new HashMap<Integer, ExtentTest>();
    private static final ExtentReports EXTENT = ExtentManager.getInstance();

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

    public static synchronized ExtentTest createTest(String testName) {
        return createTest(testName, "");
    }

    public static synchronized ExtentTest createTest(String testName, String desc) {
        ExtentTest test = EXTENT.createTest(testName, desc);
        extentTestMap.put((int) (long) (Thread.currentThread().getId()), test);

        return test;
    }

}


public class ExtentManager {

    private static ExtentReports extent;

    public synchronized static ExtentReports getInstance() {
        if (extent == null) {
            createInstance("reports/extent.html");
        }

        return extent;
    }

    public synchronized static ExtentReports createInstance(String fileName) {
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
        htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
        htmlReporter.config().setChartVisibilityOnOpen(true);
        htmlReporter.config().setTheme(Theme.STANDARD);
        htmlReporter.config().setDocumentTitle(fileName);
        htmlReporter.config().setEncoding("utf-8");
        htmlReporter.config().setReportName(fileName);
        htmlReporter.setAppendExisting(true);

        extent = new ExtentReports();
        extent.setAnalysisStrategy(AnalysisStrategy.CLASS);
        extent.attachReporter(htmlReporter);

        return extent;
    }

}   

最后,你的苗条测试。注意这里有 0 行记者代码——见 ReporterClass。

public class MyTestsClass extends BaseClass {
    @Test   
    public void 961_NavigateToMyAlertsAndAddNewAlerts()
            throws IOException, InterruptedException, ATUTestRecorderException, APIException {
        driver = launchTargetUrl(this.getClass().getSimpleName().toString());
        LoginApplication(driver,transactionusername, transactionuserpassword,test);
        HomePage homePage = new HomePage(driver,test);
        homePage.MyAlerts.click();
        MyAlerts myalerts = new MyAlerts(driver,test);
        String selectedcardName;
        selectedcardName = driver.findElement(myalerts.cardName).getText().trim();
        System.out.println(selectedcardName);       
    }
}
//Add below class in your Project. First you need to add the respective object and 
//call them respectively. Declaring Extent and Driver as static is a big problem in 
//Parallel/execution.
//Avoid using static as far as possible by using the below class.

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.WebDriver;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;

public class WebDriverFactory {

private static ThreadLocal<WebDriver> drivers=new ThreadLocal<>();
private static List<WebDriver> storeDrivers=new ArrayList<WebDriver>();
private static List<ExtentTest> extent=new ArrayList<ExtentTest>();
private static ThreadLocal<ExtentTest> reports=new ThreadLocal<ExtentTest>();

static 
{
    Runtime.getRuntime().addShutdownHook(new Thread(){
        public void run()
        {
            storeDrivers.stream().forEach(WebDriver::quit);
        }
    });
 }


public static WebDriver getDriver() 
{
 return drivers.get();
 }
 public static ExtentTest getextentReportObject()   
 {
   return reports.get();
 }

 public static void addDriver(WebDriver driver) 
 {

    storeDrivers.add(driver);
   drivers.set(driver);
 }

 public static void addExtentReportObject(ExtentTest report)    
 {

   extent.add(report);
   reports.set(report);
  }

  public static void removeDriver()
 {
  storeDrivers.remove(drivers.get());
  drivers.remove();

  }
  }
//Add and Invoke the object in the following way


  /*** Add and invoke the object in the below fashion **/

    WebDriverFactory.addExtentReportObject(extent.createTest("Monitor Scenario 
   ").createNode("Monitor Page Validation"));
   WebDriverFactory.getextentReportObject().assignCategory("@SmokeTest");