javaselenium-webdriverextentreportsselenium-extent-report

Read the 2 extent reports and create a consolidated report by merging the test results


I am running the multiple automation suites in parallel and those all are creating the different extent reports. I want to my code to read the existing extent reports and create a consolidated one. is there any way to read the 2 extent reports and create a consolidated report by merging the test results?

I tried below code but it is not merging the test results correctly.

I am using 4.0.9 version for Extent Report.

   import com.aventstack.extentreports.ExtentReports;
   import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
   import com.aventstack.extentreports.reporter.configuration.Protocol;
   import com.aventstack.extentreports.reporter.configuration.Theme;

   public class ExtentReportMerger1 {
        public static void main(String[] args) {
        String file1Path = "D:\\CodeAutomation15\\ui-automation- codebase\\logs\\Reports\\ExtentReports1.html";
        String file2Path = "D:\\CodeAutomation15\\ui-automation-codebase\\logs\\Reports\\ExtentReports2.html";

        
        // Load the first report
        ExtentReports extentReports = new ExtentReports();
        ExtentHtmlReporter htmlReporter1 = new ExtentHtmlReporter(file1Path);
        extentReports.attachReporter(htmlReporter1);

        // Load the second report
        ExtentHtmlReporter htmlReporter2 = new ExtentHtmlReporter(file2Path);

        // Append the details from the second report to the first report
        extentReports.attachReporter(htmlReporter2);

        
        htmlReporter1.config().setTheme(Theme.STANDARD);
        htmlReporter1.config().setDocumentTitle("Merged Report");
        htmlReporter1.config().setEncoding("UTF-8");
        htmlReporter1.config().setProtocol(Protocol.HTTPS);
        

        // Flush the reports
        extentReports.flush();
       

        System.out.println("Merged Extent reports successfully!");
    }
}

I tried the above code and it appended the details from 2nd file to 1st but it was not correctly merged.

enter image description here


Solution

  • You can append multiple reports in Extent 4.1.7 version of extent using json files generated by extent If you have the .json file generated by extent Report , we can use those to create a single html report with all merged results Below is a sample where i am running two test cases and generating the json for the extent and them I create a final html from ht generated json For Your use case i would suggest to generate the json for every html report and then in the end you can consolidate into a single report

    Below is a sample use case to Achieve this-

    #1 First we create a common output Folder for extent json files so that we can merge them all from a common folder #2 Them we generate two sample html Reports with there extent json files which are then written to common json folder

    JsonFormatter json2 = new JsonFormatter(opFolder + "/Report2.json");
    

    #3 Then we Create a Third extent Report , Take all the generated json files from folder and add their data into this and it will create a merged Report

     extentMerged.createDomainFromJsonArchive(jsonFile.getPath());
    

    Full Code

    import com.aventstack.extentreports.ExtentReports;
    import com.aventstack.extentreports.reporter.ExtentSparkReporter;
    import com.aventstack.extentreports.reporter.JsonFormatter;
    import java.io.File;
    import java.io.IOException;
    import java.util.Arrays;
    
    
    public class TestBase {
    
        public static void main(String... a) throws IOException {
    
            //Creating a Directory for Extent Json Output and create it if doesn't exist
            File file = new File("ExtentJson");
            if (!file.exists()) {
                file.mkdir();
            }
            String opFolder = file.getPath();
    
            //Creating individual Report Number 1
            ExtentSparkReporter spark = new ExtentSparkReporter("Report1.html");
            JsonFormatter json = new JsonFormatter(opFolder + "/Report1.json");
            ExtentReports extent = new ExtentReports();
            extent.createTest("test1").assignCategory("cat").pass("Step 1 from test 1")
                .fail("step 2 from test 1");
            extent.attachReporter(json, spark);
            extent.flush();
    
            //Creating individual Report Number 2
            ExtentSparkReporter spark2 = new ExtentSparkReporter("Report2.html");
            JsonFormatter json2 = new JsonFormatter(opFolder + "/Report2.json");
            ExtentReports extent2 = new ExtentReports();
            extent2.createTest("test2").assignCategory("cat").pass("Step 1 from test 2")
                .fail("step 2 from test 2");
            extent2.attachReporter(json2, spark2);
            extent2.flush();
    
            ExtentSparkReporter mergedSpark = new ExtentSparkReporter("spark.html");
            ExtentReports extentMerged = new ExtentReports();
    
            //Replace below logic to get all the .json files generated by extent in opFolder
            File jsonOPDirectory = new File("ExtentJson");
            if (jsonOPDirectory.exists()) {
                Arrays.stream(jsonOPDirectory.listFiles()).forEach(jsonFile -> {
    
                    try {
                        extentMerged.createDomainFromJsonArchive(jsonFile.getPath());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            }
    
            extentMerged.attachReporter(mergedSpark);
            extentMerged.flush();
        }
    
    }
    

    Reference Combine Multiple Reports