Skip to main content

Quality Assurance

Enhancing Test Reporting using ExtentReports in Selenium

Coworkers Team At Work. Group Of Young Business People In Trendy Casual Wear Working Together In Creative Office

Selenium, a widely-used automation testing framework, can be augmented with powerful reporting tools to provide detailed insights into test execution. One such tool is ExtentReports, a versatile reporting library that seamlessly integrates with Selenium to enhance the visibility and comprehensibility of test results.

 

Understanding ExtentReports

ExtentReports is an open-source reporting library for Java and .NET that facilitates the creation of interactive and detailed reports for test automation. It is particularly beneficial for Selenium-based projects, offering features like:

 

Rich Visualization: ExtentReports generates visually appealing HTML reports with interactive charts and graphs, providing a comprehensive overview of test execution.

Test Categorization: Tests can be categorized and labeled, making it easier to filter and analyze results based on different criteria such as functionality, priority, or status.

Log Generation: ExtentReports allows the logging of events during test execution, providing additional context and facilitating troubleshooting.

Screenshot Integration: Screenshots can be embedded in reports, aiding in the visual validation of test results.

 

Integrating ExtentReports with Selenium

To leverage the benefits of ExtentReports in Selenium, follow these steps:

 

Step 1: Add Dependencies

Add the necessary dependencies for ExtentReports in your Selenium project. You can include the ExtentReports JAR file in your project or use build tools like Maven or Gradle.

For Maven, add the following dependency:

<dependency>

    <groupId>com.aventstack</groupId>

    <artifactId>extentreports</artifactId>

    <version>4.1.6</version>

</dependency>

 

Step 2: Create ExtentReports Instance

In your Selenium test script, create an instance of ExtentReports at the class level. This instance will be used to create and manage the test reports.

import com.aventstack.extentreports.ExtentReports;

import com.aventstack.extentreports.ExtentTest;

import com.aventstack.extentreports.reporter.ExtentHtmlReporter;


public class TestClass {

    ExtentReports extent;

    ExtentTest test;

    public void setup() {

        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extent-report.html");

        extent = new ExtentReports();

        extent.attachReporter(htmlReporter);

    }

}

 

Step 3: Create ExtentTest Instances

For each test case, create an instance of ExtentTest. This instance represents a specific test and will be used to log information and mark the test status.

public void executeTest() {

    test = extent.createTest("MyTest", "Description of MyTest");

    // Test steps and assertions go here

    test.pass("Test passed");

}

 

Step 4: Log Events and Capture Screenshots

During the test execution, use the ExtentTest instance to log events and capture screenshots if needed.

public void executeTestWithLogs() {

    test = extent.createTest("MyTestWithLogs", "Description of MyTestWithLogs");

    test.info("Step 1: Performing action");

    // Perform action

    test.info("Step 2: Verifying result");

    // Verify result

    test.pass("Test passed");

}

 

Step 5: Generate and View Reports

After test execution, generate the HTML report using the following code. The report will be saved in the specified location (in this case, “extent-report.html”).

public void tearDown() {

    extent.flush();

}

 

By following these steps, you can seamlessly integrate ExtentReports into your Selenium tests, enhancing the overall test reporting experience. The generated reports provide a clear and detailed view of test execution, making it easier for teams to analyze results and make informed decisions.

 

Parallel Execution and Threads:

Parallel execution of tests is a common practice in test automation to reduce the overall execution time. However, handling parallel execution and ensuring thread safety in reporting can be challenging. ExtentReports provides features to address these challenges:

 

Thread Safety:

ExtentReports ensures thread safety by creating a separate instance of the ExtentReports class for each thread. This means that each thread has its own reporting context, preventing conflicts and ensuring that reports accurately represent the results of individual tests.

 

Setting Up Reports for Parallel Execution:

When setting up ExtentReports for parallel execution, it’s essential to create a new ExtentReports instance for each thread. This can be done by initializing a new ExtentHtmlReporter and ExtentReports object for each thread:

ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extent-report-thread1.html");

ExtentReports extent1 = new ExtentReports();

extent1.attachReporter(htmlReporter);

 

For subsequent threads, create separate instances of ExtentHtmlReporter and ExtentReports.

 

Thread-Specific Tests:

Each thread should create its own ExtentTest instances to ensure that test information is logged into the correct report. For example:

ExtentTest test1 = extent1.createTest("TestThread1");

test1.pass("Test passed in Thread 1");

 

Reporting in Multi-Threaded Environment:

ExtentReports automatically handles the generation of separate HTML reports for each thread. After the parallel execution is complete, individual reports can be aggregated or analyzed separately, depending on the testing requirements.

 

Emailing Reports:

Emailing ExtentReports can be crucial for sharing test results with stakeholders, team members, or clients. ExtentReports doesn’t have built-in email functionality, but you can integrate it with email services or CI/CD pipelines:

 

Configuring Email Settings:

To email reports, you need to configure email settings within your automation framework or CI/CD tool. You can use Java libraries like JavaMail or built-in features of your chosen CI/CD platform.

 

Attaching Reports to Emails:

After your test execution is complete, the generated ExtentReports HTML file(s) can be attached to an email. Most email libraries or CI/CD platforms allow you to attach files easily.

 

// Example using JavaMail

MimeMessage message = new MimeMessage(session);

message.setSubject("Test Results");

message.setFrom(new InternetAddress("sender@example.com"));

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com"));

// Attach ExtentReports HTML file

message.attach(new MimeBodyPart(new FileDataSource("path/to/extent-report.html")));

 

Conclusion:

By configuring email settings and integrating ExtentReports with your CI/CD pipeline, you can ensure that stakeholders receive timely and automated updates on test results.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Jeet Palan

Jeet Palan is an Technical Consultant at Perficient. He has experience in Manual and Automation testing. In addition to this, he is willing to learn different types of testing and likes to know and learn about new trending technologies.

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram