Using Extent Report with TestNG

Alper Yigit
Beyn Technology
Published in
6 min readMar 28, 2023

Reporting is very important for all business field. Because we want to see result of our works and show them to teammates.

There are some options for reporting test automation projects. One of them is extent report. Today, I will try to explain “What is extent report” and “How to use it with a TestNG project”. Firstly, I will explain extent report and then, I will create a simple TestNG project. After that, I will show you how we can use extent report on this automation project.

1) Extent Report

Firstly, we have to add “extent report” dependency to pom.xml file from maven repository.

Dependency is here.

I used 4.0.9 version
pom.xml view

Now, we have to define extent report.

We have methods that we want to run before and after the test starts. To seperate them, we will use @BeforeTest and @AfterTest annotations.

1.1) @BeforeTest

@BeforeTest annotation has been created and also setUpTest method has been created. In this method, we created an object call extentReports. Also, we set the name of the report file and determined its path. Lastly, to add what we want to see in the report, we used “setSystemInfo” and “config”.

1.2) @AfterMethod

@AfterMethod annotation has been created. In this way, if the test will be failed or skipped, we will see messages about them in report. Also, a screenshot will be created in screenshots file if the test will be failed.

1.3) @AfterTest

@AfterTest annotation has been created and also tearDownTest method has been created. We add “extentReports.flush” in this method to finish reporting after test running.

2) TestNG Project

Before start, let’s add relevant dependencies to pom.xml file from maven repository.

Selenium dependency ;

I used 4.7.2 version

Webdrivermanager dependency ;

I used 5.3.0 version

TestNG dependency ;

I used 7.7.1 version

Final view of pom.xml file;

I will create a simple automation project on amazon website. Now, we should create a structure for our project. I will use POM for this project. In this way, our project will be more readable, useable and maintainable. Firstly, necessary classes must be created.

2.1)Classes

2.1.1)AmazonPage

Here, will be all the web elements we will use.

2.1.2) AmazonTest

The class in which the tests will be written step by step.

2.1.3) ConfigReader

This class must be created for read to configuration.properties file.

2.1.4) Driver

We need to create this class for can manage driver and browser.

2.1.5) TestBaseReport

Finally, we should create this class for can manage extent report. We should add the codes we wrote in the first section to this class.

2.1.6) ReusableMethods

In this class, we will create methods which we will use again and again. Also, we can use this class if we dont want to see some methods another classes. For this project, we will use this class for screenshot method. Thanks to this method, if the test will be failure, screenshots file will be created in target file and there will be a screenshot about reason of failure in screenshots file..

2.1.7) configuration.properties

This file should be created for make dynamic our some values.

browser = chrome
amazonUrl = https://www.amazon.com
searchText = Java

Final view of our project structure;

Now, we are ready to add extent report to our project.

3) Adding Extent Report to Our Project

Firstly, we have to extend TestBaseReport class to AmazonTest class. Then we use extent report where all steps in test method.

We have to create an “extentTest” object. This object will start reporting and we can add test name and description in this object. First value is test name, second value is description.

extentTest = extentReports.createTest("Seaching Java","The user should be able to search and add products to the cart.");

Extent report is allowed us to specify our test report how we want. Generally four features are used. We can call these features with extentTest object. These features are;

  • extentTest.pass : This is how we write the message we want to see after the pass tests. Generally use for assertions. For example, extentTest.pass(“Title includes expected content”).
  • extentTest.fail : This is how we write the message we want to see after the fail tests. For example, extentTest.fail(“Register Test Failed”).
  • extentTest.info : It allows us to write information about each step in the test. For example, extentTest.info(“Clicked the login button.”)
  • extentTest.skip : It allows us to indicate skipped tests in the report. For example, extentTest.skip(“The step of checking the header was skipped.”)

Our test after added the report steps;

4) Run the Test

Let’s run the test and see the result.

Test passed and “test-output” file was created. Now, we have to left clicked on report and choose a browser.

I chose chrome browser

4.1) If the Test Will be Pass

We see that the test be passed. Also, we can see the title, test name, description, test time and test steps in mainpage.

Mainpage of Report

And we can see other informations in dashboard page.

4.2) If the Test Will be Fail

For the test will be fail, I change an assertion on project.

Assert.assertEquals(amazonPages.addedToCartText.getText(), "Added 123 to Cart");
extentTest.pass("'Added to Cart' text was appeared");

Expected content was changed.

In report, we see that the test was failed.

Also a screenshots file was created automatically and there is a screenshot in it. You can see it in target file.

Test was failed on this amazon page.

In conclusion, extent report is one of the good reporting methods. If we work on a TestNG automation project, we can create useable test reports using extent report and we can specify this report how we want.

That’s All

I hope, this article will help you. If you have a question, you can contact me via my e-mail address below.

İbrahim Alper Yiğit, Test Engineer

ialper.yigit@gmail.com

Github | Linkedin

--

--