Advanced Test Report With Selenium Web Driver

Knoldus Blog Audio
Reading Time: 4 minutes

No framework is complete without report feature, So this blog will help you to have a Create Advanced Test Report with Selenium Web Driver.

What are the ExtentReport

  • ExtentReports is an open-source reporting library useful for test automation and It can be easily integrated with major testing frameworks like TestNG etc. These reports are high rich HTML reports. It provides results in the form of PIE charts. Using Extent Reports we could generate custom logs, add snapshots. We could use external XML file to provide extra information.

Extent Reports in Selenium contain two major, frequently used classes:

  • ExtentReports class
  • ExtentTest class
  • The ExtentReports class generates HTML reports based on a path specified by the tester.Based on the Boolean flag, the existing report has to be overwritten or a new report must be generated. ‘True’ is the default value, meaning that all existing data will be overwritten.

Using these class, We create Advanced Test Report with Selenium Web Driver.

  • startTest: Executes preconditions of a test case
  • endTest: Executes postconditions of a test case
  • log: Logs the status of each test step onto the HTML report being generated
  • flush: Erases any previous data on a relevant report and creates a whole new report

A Test Status can be indicated by the following values:

  • PASS
  • FAIL
  • SKIP
  • INFO
test.log(LogStatus.PASS,"Test Passed");
test.log(LogStatus.FAIL,"Test Failed");
test.log(LogStatus.SKIP,"Test Skipped");
test.log(LogStatus.INFO,"Test Info");

Add maven dependency for ExtentReport

<dependency>
     <groupId>com.aventstack</groupId>
     <artifactId>extentreports</artifactId>
     <version>4.0.9</version>
</dependency>

How to generate Extent Reports

  • Create a new JAVA class with the below code for Extent Reports.
public class ExtentReport implements IReporter {
    private ExtentReports extent1;

    private ExtentHtmlReporter extentHtmlReporter;

    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
                               String outputDirectory) {
        Properties prop = new Properties();
        FileInputStream FileInputStream = null;
        String reportPath = null;

        try {
            FileInputStream = new FileInputStream(
                    new File("src//main//resources//object.properties"));
            prop.load(FileInputStream);
            reportPath = prop.getProperty("ReportPath");
        } catch (IOException e) {
            e.printStackTrace();
        }

        extent1 = new ExtentReports(reportPath, true);
        for (ISuite suite : suites) {
            Map<String, ISuiteResult> result = suite.getResults();
            for (ISuiteResult r : result.values()) {
                ITestContext context = r.getTestContext();
                try {
                    buildTestNodes(context.getPassedTests(), LogStatus.PASS);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    buildTestNodes(context.getFailedTests(), LogStatus.FAIL);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    buildTestNodes(context.getSkippedTests(), LogStatus.SKIP);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        ExtentAventReporter reporter= new ExtentAventReporter(reportPath);
        extent1.addSystemInfo("Tester", "Prajjawal Kansal");
        extent1.flush();
        extent1.close();
    }
    private void buildTestNodes(IResultMap tests, LogStatus status) throws Exception {
        ExtentTest test1;
        if (tests.size() > 0) {
            for (ITestResult result : tests.getAllResults()) {
                //testCaseCount++;
                test1 = extent1.startTest("For  "+((SheetColumnHeader)result.getParameters()[0]).getFirstname()+" details this  test case ");
                test1.setStartedTime(getTime(result.getStartMillis()));
                test1.setEndedTime(getTime(result.getEndMillis()));

                for (String group : result.getMethod().getGroups()){
                    test1.assignCategory(group);
                }
                if (result.getThrowable() != null) {
                    test1.log(status,  "   failed " + test1.addScreenCapture(System.getProperty("user.dir")+ "/test-output/Screenshot/Screenshot.png"));
                } else {
                    test1.log(LogStatus.PASS,   "   passed");
                }
                extent1.endTest(test1);
            }
        }
    }

    private Date getTime(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return calendar.getTime();
    }
}

How to print the description for each test case in Report

  • Create a New JAVA class, there we generate the getter or setter and string value for each column, So we print any column value in report. let’s see with code.
 public SheetColumnHeader() {
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) { this.firstname = firstname; }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public String getExpectedResult() {
        return expectedResult;
    }
    public void setExpectedResult(String expectedResult) {
        this.expectedResult = expectedResult;
    }
    @Override
    public String toString() {
        return "SheetColumnHeader{" +
                "firstname='" + firstname + '\'' +
                ", lastname='" + lastname + '\'' +
                ", gender='" + gender + '\'' +
                ", number='" + number + '\'' +
                ", expectedResult='" + expectedResult + '\'' +
                '}';
    }
  • Using this line of code,This line will help to attach description for each test case.
extent1.startTest(((SheetColumnHeader)result.getParameters()[0]).getFirstname());

Capture Screenshots

  • By capturing screenshots, testers can better identify what went wrong when the software acted erroneously during a test. Capture screenshots only when a test fails, since they consume a lot of memory.
  • Create a method takeScreenshot which will implement the code to take the screenshot.  This method will return the path where the screenshot is saved.
  • Try capturing screenshots with the code below
 public static void takeScreenshot( String filename) throws IOException {
        File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File(System.getProperty("user.dir")+"/test-output/Screenshot/" + filename + ".png"));

    }
  • Adding the Failed Screenshot in Extent Report.
test1.addScreenCapture(Screenshot-path));
  • We can also use this line of code for attached screenshot in ExtentReport.
MediaEntityBuilder.createScreenCaptureFromPath("1.png").build());

Implement Listeners in testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <listeners>
        <listener class-name="Listeners.ExtentReport"/>
    </listeners>
</suite>
  • Now refresh the project after execution

Test Summary Report

Advantage of using Extent Reports

  • This framework help to add the different – different  descriptions in each test case .
  • If required, screenshots can be captured and displayed for each step in a test
  • They show the time needed for test execution.

References

https://www.softwaretestingmaterial.com/generate-extent-reports-version-3/

Knoldus-blog-footer-image

Written by 

Prajjawal is a QA Consultant. He is familiar with core concepts of manual & automation testing using tools like Selenium and Postman Also having knowledge of Python and Data Science. He is always eager to learn new and advanced concepts in order to improve himself. He likes to watch web series and play cricket.