TestNG Report Generation in Selenium WebDriver (1)

TestNG Report Generation in Selenium WebDriver

Table of Contents

TestNG Reports:

TestNG Reports come into picture when we execute the test cases using TestNG Report generation is very important when you perform Manual Testing and Automation Testing. By going through the result, you can easily identify how many test cases are passed, how many are failed and how many test cases are skipped.By looking at the result report, you will know the status of the project.

Selenium WebDriver is used only for automation, but it won’t generate any reports. In this situation, TestNG plays a crucial role right after executing test cases, automatically generating detailed default HTML reports. Enroll in an automation software testing course to learn how to efficiently utilize Selenium and TestNG for comprehensive test automation setups.

These reports provide an organized view of the test results, including information on passed, failed, and skipped test cases, making it easy for testers to quickly assess the status of their test suite. With TestNG’s built-in reporting, you gain an at-a-glance overview of your test outcomes, allowing for efficient debugging and better overall test management without needing any additional configuration.

Using 3 Methods we can generate the TestNG Reports:

  1. emailable-report.html
  2. index.html
  3. Reporter Class

Method 1: emailable-report.html

Generally TestNG will generate default reports. You will get test-output folder when you execute testng.xml file and refresh the project. Right-click on emailable-report.html in test-output folder and select the option open with the web browser.

Let us consider the below scenario which has three methods passTest, failTest and skipTest with @Test annotation.

Step 1: Launch the Eclipse

Step 2: Create a New Package

Step 3: Create a New Java Class “EmailReport”

package testngpackage;

import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.Test;

public class EmailReport {
//To make it pass
@Test
public void passTest(){
Assert.assertTrue(true);
}
//To make it fail
@Test
public void failTest(){
Assert.assertTrue(false);
}
//To make it skip
@Test
public void skipTest(){
throw new SkipException("Skipping - This method is skipped testing ");
}
}

Step 4: Create a TestNG.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="testngpackage" parallel="methods">
<test name="testngTest">
<classes>
<class name="testngpackage.EmailReport" />
</classes>
</test>
</suite>

Step 5: Execute the testng.xml file and refresh the project. You can see your project as the below image.

testng.PNG

Expand ‘test-output’ folder and you should find a report ”emailable-report.html‘ which is the default report generated by TestNG.

report.PNG

Step 6: Right-click on emailable -report.html and select open with and click on web browser

web.PNG

Report will be like as shown below:

pic1.PNG
pic2.PNG

Method 2: index.html

Expand ‘test-output’ folder and you find an option ”index.html‘ which is the default report generated by TestNG.

index.PNG

Right-click on index.html and select open with and click on web browser

web1.PNG

Report will be like as shown below:

index1.PNG

Method 3: Reporter Class

Along with the above two methods to generate reports, you can use the object.properties file to store the system-generated logs as well as user-generated logs. Using Reporter Class is one of the simplest ways to store log information in testing.

Reporter class is an inbuilt class in TestNG. It helps in storing the logs inside the reports which are user-generated or system-generated so that in the future when we look at the report, we can directly view the logs from there instead of rerunning the test cases.

Reporter is a class present in TestNG. It has four different methods to store log information:

  1. Reporter.log(String s);
  2. Reporter.log(String s, Boolean logToStandardOut);
  3. Reporter.log(String s, int level);
  4. Reporter.log(String s, int level, Boolean logToStandardOut);

Following syntax is used to use the reporter class

Reporter.log(string);

Simply we need to call the “log” function of the Reporter class of TestNG.

Example

Create a class as GoogleTest and write the following code inside the classes.

package com.sampletestpackage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Reporter;
//import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;

public class GoogleTest {

WebDriver driver;
@Test(priority = 1) 
public void driverSetup()
{ 
// System.setProperty("webdriver.gecko.driver", "src\\main\\java\\com\\browserdrivers\\geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "src\\main\\java\\com\\browserdrivers\\chromedriver.exe");
//System.setProperty("webdriver.ie.driver", "src\\main\\java\\com\\browserdrivers\\IEDriverServer.exe");

driver=new ChromeDriver(); 
Reporter.log("The browser is opened");

} 
@Test(priority = 2) 
public void getURL()
{ 
  driver.get("https://www.google.com");
     // System.out.println("Launching Google site"); 
      Reporter.log("The Google Site is Launched");

} 
@Test(priority = 3) 
public void getTitle()
{ 
  String title = driver.getTitle(); 
  System.out.println(title); 
      Reporter.log("Prints the web page title");

  } 
@Test(priority = 4) 
public void closeBrowser()
{ 
  driver.close(); 
  //System.out.println("Test successfully passed"); 
      Reporter.log("Close the driver");

  } 

}
  • Now, Create testng.xml file by selecting the class and
  • Select run as and
  • Click on convert to Testng.
  • Then run this testng.xml file by selecting run as and select Testng suite.
  • Then refresh the project and open the test-output folder.
  1. In the test-output folder and Right-click on open the emailable-report.html. It will look like:
email.PNG
email1.PNG

2. In test-output folder, Right-click on index.html and select open with and click on web browser

index2.PNG

Click on Reporter output. It will open logging information that is written in the test methods.

index3.PNG

Click on “Times”. It will show how much time it took to run the test method present in class.

index4.PNG

Generating Allure Reports using TestNG in Selenium Automation

Following are the steps to generate Allure Reports using TestNG

Step1: Create a New Maven project and Update pom.xml with required dependencies from https://mvnrepository.com/

  • TestNG
  • Selenium Java
  • Webdrivermanager
  • Allure-TestNG

TestNG Dependencies

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.1.0</version>
    <scope>test</scope>
</dependency>

Selenium Java

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0-alpha-6</version>
</dependency>

Webdrivermanager

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>4.0.0</version>
</dependency>

Allure-TestNG

<!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-testng -->
<dependency>
    <groupId>io.qameta.allure</groupId>
    <artifactId>allure-testng</artifactId>
    <version>2.13.3</version>
</dependency>

Step2: Download Maven and Allure Binaries then set the path

Go to https://maven.apache.org/download.cgi  and download and extract the zip folder (apache-maven-3.6.3-bin.zip)

Click on Extracted folder and go to bin folder and copy the path and set the maven path in environment variables

Right-click on My PC->Properties -> Click Advance System Settings -> Click Environment Variables – > Click Path under System Variables and click Edit -> Click New and paste the path C:\apache-maven-3.6.3\bin

To check whether maven is configured or not, go to the command prompt and type the command mvn –version.

To download Allure Binaries go to https://docs.qameta.io/allure/#_installing_a_commandline and click Maven Central under Manual Installation and download the latest version 2.13.0 and click on the ZIP folder allure.commandline-2.13.0.zip

Click on Extracted folder and go to bin folder and copy the path and set the path in environment variables.

Right-click on My PC->Properties -> Click Advance System Settings -> Click Environment Variables – > Click Path under System Variables and click Edit -> Click New and paste the path C:\allure-2.13.0\bin

To check whether Allure is configured or not, go to the command prompt and type the command  allure –version.

Step3: Create TestNG Test Cases and run through TestNG.XML

  1. Create a New Package (allureReports)
  2. Create a New Class (Tests)
Package allureReports;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;

Public class Tests {
WebDriver driver;
@BeforeClass
Public void setup()
{
WebDriverManager.chromedriver().setup();
driver = new  ChromeDriver();
driver.manage().timeouts.implicitywait(10,TimeUnit.SECONDS);

driver.get(“https://demo.nopcommerce.com/”);
driver.manage().window().maximize();
}
@Test(priority=1)
Public void logoPresence()
{
Boolean disstatus = driver.findElement(By.xpath(“//div[@class=’header-logo’]//a//img”)).isDisplayed();
Assert.assertEquals(disstatus, true);
}
@Test(priority=2)
Public void loginTest()
{
driver.findElement(By.linkText(“Log in”)).click();
driver.findElement(By.id(“Email”)).sendKeys(“[email protected]”);
driver.findElement(By.id(“Password”)).sendKeys(“xxxabc”);
driver.findElement(By.xpath(“//input[@class=’button-1 login-button’]”)).click();
Assert.assertEquals(driver.getTitle(), “nopCommerce demo store”);


}
@AfterClass
Public void teardown()
{
driver.quit();
}
}

Step4: Create a TestNG.xml  file

Right-click on your Testcase(Tests)- >Go to TestNG-> Click Convert to TestNG and click Finish

<?xml  version “1.0” encoding-“UTF-8”?>
<!DOCTYPE suite SYSTEM http://testng.org/testng-1.0.dtd>
<suite name = “Suite”>
<test thread-count=”2” name=”Test”/>
<classes>
<class name = “allureReports.Tests”/>
</classes>
</test> <!- - Test- - >
</suite> <!- - Suite - - >

Right-click on TestNG.xml file ->Run As->TestNg Suite

Refresh your project and you can see a new folder allure results. Expand the allure results and you can see json files. To see the allure reports which are generated for test case through TestNG.xml we need to run a command allure serve

Right-click on allure results folder – > Go to Properties – > Copy the Location

Open Command prompt and type the command

Allure serve C:\Users\admin\eclipse-workspace\AllureReporting\allure-results.

It will automatically generate the Allure Reports.

Conclusion

In conclusion, TestNG report generation in Selenium WebDriver is a valuable tool for enhancing the testing process. By providing detailed insights into test execution, including passed, failed, and skipped tests, TestNG reports make it easier to analyze results and identify areas that need improvement. The ability to customize reports, add screenshots, and generate logs ensures that testers have a comprehensive view of test outcomes, which is essential for maintaining high-quality applications.

Implementing TestNG with Selenium WebDriver greatly enhances the efficiency of your testing process by allowing you to manage, group, and execute test cases with ease. TestNG’s powerful features, such as parallel execution, parameterization, and detailed reporting, enable a more organized and systematic approach to testing.

This structured setup not only saves time but also helps teams maintain a clear view of testing progress and results, making it easier to share insights and collaborate effectively. By integrating TestNG, you create a more reliable and scalable automation framework, turning it into an essential tool for quality-focused testing teams.

Call to Action

Ready to take your Selenium testing to the next level? Learn how to generate and customize detailed TestNG reports with H2K Infosys! Our expert-led courses will guide you through the essentials of TestNG report generation, helping you gain insights into test outcomes, improve debugging, and streamline your automation process. Enhance your skills and boost your career with hands-on experience in Selenium WebDriver and TestNG. Join H2K Infosys today and become a proficient automation tester!

With TestNG report generation, you’ll gain a clear view of your test results, making it easy to track performance and identify issues quickly. By mastering TestNG with Selenium WebDriver, you can enhance the quality and reliability of your tests, ensuring that every test run is accurately documented and actionable.

One Response

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.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class