Extent report is not giving proper report on parallel execution

前端 未结 2 645
[愿得一人]
[愿得一人] 2021-01-15 22:58

ReporterClass.Java:

package POM_Classes;

import com.aventstack.extentreports.AnalysisStrategy;
import com.aventstack.extentreports.ExtentReports;
import com         


        
相关标签:
2条回答
  • 2021-01-15 23:07

    Ok, here you go. I haven't tested this yet, but this should allow you to use Extent with parallel usage.

    Reporter:

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

    Base:

    public abstract class BaseClass extends ReporterClass {
    
        // .. abstractions
    
    }
    

    Extent utils:

    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;
        }
    
    }   
    

    Finally, your slim tests. Notice there is 0 lines of reporter code here - see 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);       
        }
    }
    
    0 讨论(0)
  • 2021-01-15 23:11
    //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");   
    
    0 讨论(0)
提交回复
热议问题