How to use Extent Reports with TestNG in Selenium Java
What are Extent Reports?
Extent Reports is an open-source HTML reporting library useful for test automation. It can be easily integrated with major testing frameworks like JUnit, NUnit, TestNG, etc. These reports are HTML documents that depict results as pie charts.
Extent Reports in Selenium contain two major, frequently used classes:
- ExtentReports class
- ExtentTest class
Both classes can be used with the following built-in methods:
- 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
Let's get to know few methods of the above classes. First, let's try to create a Project in Eclipse and apply it into the main method class then we can convert it into a TestNG Class.
OK, now let's get started….
1.Adding Dependencies to the pom.xml file
Create a maven project and let's name it “TestNGextentReports”.
Since we are going to create a maven project let's get Extent Reports from the maven dependencies.
First, copy the selenium dependency and set it in the pom.xml file.
https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java/3.141.59
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
Copy the TestNG dependency and set it in the pom.xml file.
https://mvnrepository.com/artifact/org.testng/testng
<! — https://mvnrepository.com/artifact/org.testng/testng →
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
As well as set the WebDriver maven dependency for making our test simple.
https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager/4.4.3
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.4.3</version>
</dependency>
This is the dependency we need to import libraries from Extent Reports.
https://mvnrepository.com/artifact/com.relevantcodes/extentreports
<!-- https://mvnrepository.com/artifact/com.relevantcodes/extentreports -->
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
2.Create a Java class
Create a new class called “ExtentReport”. And include the main()
method.
To create Extent Report references first you need to copy this statement to the main method.
ExtentSparkReporter htmlReporter = new ExtentSparkReporter("extentReport.html");
We have now created an object called “htmlReporter” from the class “ExtentSparkReporter”. Here I gave the file name “extentReport.html” so my results will be store there.
So after the reference has set put this code in the class.
So this code will go to https://www.ebay.com, validate the site by comparing Title and enter “mobile” in the search box and hit enter.
Press CTRL + SHIFT + O to import all classes from the avenstack
package.
You will find this method in the above code.
extent.flush();
Calling the flush
the method writes/updates the test information of your reporter to the destination type. otherwise, if you do not use this you will not see any error in your program because you won’t get any report.
Now you have to refresh your Project and you will see “extentReport.html” file.
now Right-click on it and open it in a browser.
As you can see these are way better than TestNG Reports. It has a very beautiful, clear and informative GUI. It also has two views which are very clear.
You can have the Timeline graph, pass/fail rate, date/time and you will notice that it mentioned pass, info status clearly as we have given them in the code.
So this is how we create Extent Reports in selenium. Now let's try to apply the same test scenario to the TestNG class.
3.Create a TestNG Class
Use the same package and create another class called “ExtendReportTestNG”.
Now create three functions as below. We are going to use TestNG annotations for these functions.
@BeforeSuite
public void setup(){
}@Test
public void test(){
}@AfterSuite
public void tearDown(){
}
@BeforeSuite — In BeforeSuite I have initialized the ExtentSparkReporter
and ChromeDriver
@Test — I have written the code to go to Daraz.lk and ebay.com and the Title will be checked with the actual title. Then it will insert “mobile” inside the textbox and hit enter.”
@AfterSuite — In here browser will be closed and flush()
Copy this code to your project and run it as a “TestNG Test”
I have used two @Test
annotations to run two different sites separately. And have used prioritize method in each @Test
priority = int
means that we can choose which@Test
to run first and which one will run after that. It takes the lowest number as the highest priority. And also it counts minus numbers.
Ex: priority = -1 > priority = 0 (-1 has higher priority than 0)
If we do not mention the priority it takes the alphabetical order of the method name.
Now let's have a look at the extent Reports.
As you can see in the left panel we can see both tests are categorized separately. We can go to each one and view the results.
So that’s it. This is how we use extent reports in our TestNG projects.
When we are dealing with bigger projects in selenium we can prioritize the tests, separate each one and have a clean code.
For your reference find my GitHub project here
THINK QUALITY!