ExtentReports basic and useful methods. (Session-4)

ExtentReports is vast and there are lots or methods and utility available. In this blog will learn basic and useful methods to generate HTML reports.

ExtentReports methods with sample code

Creating Simple Test Cases

public void defaultTC()
	{
		logger.createTest("Default method to create test case")
		.log(Status.PASS, "Test case Passed");
		logger.flush();
	}
	public void createTC()
	{
		// Simple method to create test case and set status result.
		logger.createTest("First Test Case").pass("This TC passed.");
		logger.flush();
	}

Multiple steps in a single Test Case

We can add multiple events like INFO, Warning, Pass, Fail status in single test case. If any of the event is Fail then end result of Test Case would be fail.

public void multStepTC()
	{
		// Recording each step under single Test Case using ExtentTest Class.
		
		// Creating main test case
		ExtentTest test=logger.createTest("Second Test Case");
		
		// Adding multiple steps/events in above test case
		test.info("Values of a and b initalized");
		int a=10;
		test.pass("Value of a "+a);
		int b=20;
		test.pass("Value of b "+b);
		int c=a+b;
		String result="Sum of "+a+" "+b+"="+c;
		System.out.println(result);
		if(c>50)
		{
			test.pass("Value of c is greater than 50. c="+c);
		}
		else
		{
			test.fail("Value of c is less than 50. c="+c);
		}
		
		logger.flush();
		
	}
Multiple steps in a single Test Case

Creating Multiple sections within a Test Case

We can also create multiple sections under a test case using createNode() method. Here also we can add multiple events/steps in a single test case.

public void TC_Nodes()
	{
		// Recording each step under seperate section(node) in single Test Case
		// Using ExtentTest class createNode method.
		
				ExtentTest test=logger.createTest("Test Cases with Nodes");
				
				// Created first node under ExtentTest object.
				ExtentTest subTest1=test.createNode("Initialization Node");
				subTest1.info("Values of a and b initalized");
				int a=10;
				subTest1.pass("Value of a "+a);
				int b=20;
				subTest1.pass("Value of b "+b);
				int c=a+b;
				String result="Sum of "+a+" "+b+"="+c;
				System.out.println(result);
				
				// Created Second node under ExtentTest object.
				ExtentTest subTest2=test.createNode("Result Node");
				
				if(c>50)
				{
					subTest2.pass("Value of c is greater than 50. c="+c);
				}
				else
				{
					subTest2.fail("Value of c is less than 50. c="+c);
				}
				
				logger.flush();
	}
Creating Multiple sections within a Test Case

Recording Exception detail in Test Case

Whenever any test case failed, it is necessary to add relevant details of the exception for reference. To achieve this we sometimes we need to record exception along with the test case result.

public void recordingExceptions()
	{		
		// Create an object for Throwable class
		Throwable error=new RuntimeException("User defined Exception");
		
		logger.createTest("Exception Example")
		.log(Status.FAIL, error);
		
		logger.flush();
	}
Recording Exception detail in Test Case

Adding Screenshots in the Test Case

Sometimes regular path won’t work while declaring the file path in that case use System.getProperty(“user.dir”) as mentioned below.

public void addScreenshotTest()
	{
		//Sometimes regular path won't work in that case use System.getProperty("user.dir")
		// Locate first screenshot
		String screenshotPath1=System.getProperty("user.dir")+"\\screenshots\\AshotFile.jpg";
		// Locate second screenshot
		String screenshotPath2=System.getProperty("user.dir")+"\\screenshots\\currentScreenSize.jpg";
		
		logger.createTest("Test case with screenshot.")
		.addScreenCaptureFromPath(screenshotPath1, "First screeshot")
		.log(Status.FAIL, "Test case is failed.",
				MediaEntityBuilder.createScreenCaptureFromPath(screenshotPath2, "Second screenshot").build());
		
		logger.flush();
	}
Adding Screenshots in the Test Case

Adding Screenshots in Multiple Events

public void addingScreenshotsMultEvents()
	{
		//First write a code to capture the screenshot and save in the hard drive.
		
		//Sometimes regular path won't work in that case use System.getProperty("user.dir")
		
		// Locate first screenshot
		String screenshotPath1=System.getProperty("user.dir")+"\\screenshots\\AshotFile.jpg";
		
		ExtentTest test=logger.createTest("Test Case with Screenshots Multi Events");
		test.fail(MediaEntityBuilder.createScreenCaptureFromPath(screenshotPath1, "Test case failed.").build());
		
		// Locate second screenshot
		String screenshotPath2=System.getProperty("user.dir")+"\\screenshots\\currentScreenSize.jpg";
		
		test.pass(MediaEntityBuilder.createScreenCaptureFromPath(screenshotPath2, "Test case is passed").build());
		
		logger.flush();
	}
Adding screenshots to Multiple Events.

Adding ‘Tags’ in HTML Report

We can also create Tags to segregate or group the test cases. And we can add multiple test cases under one tag. This Tag section will appear as a separate Tab in the HTML report in the Left Panel.

We can add a test case with single or multiple tags using assignCategory(tagName) method. In the below example we will create two tags “Admin Module” and “Guest Module” and will add test cases based on the requirements.

public void createTags()
	{
		// Test Case added to two categories.
		logger.createTest("Login")
		.assignCategory("Admin Module","Guest Module")
		.pass("User logged in Successfully");
		logger.flush();
		
		// Test Case added to one category.
		logger.createTest("Edit Profile")
		.assignCategory("Admin Module")
		.pass("Profile edit successfull");
		logger.flush();
		
		// Test Case added to two categories.
		logger.createTest("View Profile")
		.assignCategory("Admin Module","Guest Module")
		.fail("Test Case Failed");
		logger.flush();
	}
Creating Tags in the HTML Report.

Adding ‘Authors’ in HTML Report

Similar to ‘Tags’ we can segregate or group Test cases based on the Author name or Developer name. For this we can use assignAuthor(authorName) method.

This section will also be added in the left panel of the Report.

public void createAuthors()
	{
		// Test Case added to two Authors.
				logger.createTest("Login")
				.assignAuthor("Developer 1","Developer 2")
				.pass("User logged in Successfully");
				logger.flush();
				
				// Test Case added to one Author.
				logger.createTest("Update Password")
				.assignAuthor("Developer 1")
				.pass("Password updated successfull");
				logger.flush();
				
				// Test Case added to two Authors.
				logger.createTest("Delete Profile")
				.assignAuthor("Developer 1","Developer 2")
				.fail("Test Case Failed");
				logger.flush();
	}
Assigning authors to the Test Cases.

Adding ‘Devices’ in HTML report

There is one more categorization is available which is based on the devices or platforms. We could achieve this via using assignDevice(deviceName) method.

public void createDevices()
		{
			// Test Case added to two Devices/Platforms.
					logger.createTest("Login")
					.assignDevice("Windows 10","MAC OS")
					.pass("User logged in Successfully");
					logger.flush();
					
					// Test Case added to one Author.
					logger.createTest("Update Password")
					.assignDevice("MAC OS")
					.pass("Password update successfull");
					logger.flush();
					
					// Test Case added to two Authors.
					logger.createTest("Delete Profile")
					.assignDevice("Windows 10","MAC OS")
					.fail("Test Case Failed");
					logger.flush();
		}
Adding devices to the Test cases.

Adding System Information in the Report

We can add additional information to the HTML Report like Project Name, System Name, Project Duration etc. All these values would be added under the DASHBOARD view. We can add as many as information based on the Key/Value pair.

public class OtherMethods {

	static ExtentSparkReporter reporter;
	static ExtentReports logger;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		reporter=new ExtentSparkReporter("Reports\\result_1.html");
		
		logger=new ExtentReports();
		logger.attachReporter(reporter);
		
		// Adding additional information based on the Key/Value pair.
		logger.setSystemInfo("Operating System", "Windows 10");
		logger.setSystemInfo("Browser Name", "Chrome");
		logger.setSystemInfo("Project", "Training Project");
		logger.setSystemInfo("Duration", "2 Months");
		}
}
Adding System Information in the Report.

Customize Views and View Order in the Report

In the reports, all the Tabs appearing in the left panes is called Views. By default, there would be two views available when we log any event or test case, Test and Dashboard view.

But when we generate or log the different type of results in the report these Tabs/Views automatically getting added in the below order:

  1. TEST: Contains all the events, test cases irrespective of tags/categories, devices, author, etc.
  2. CATEGORY: This tab contains all the ‘Tags’.
  3. DEVICE: Contains results based on the assigned devices/platforms.
  4. AUTHOR: Contains results based on the assigned Author/Developer.
  5. EXCEPTION: This tab contains all the Test Cases where exceptions generated during program execution.
  6. DASHBOARD: This will contain summarized view like
    • Pie charts based on Test cases, steps, events.
    • Detailed report and percentage of Passed/Failed/Skipped test cases of different views like Tags, Author, Device, etc.
    • System Information set via setSystemInfo() method.
  7. LOG: Contains custom logs or user-defined logs.
public class OtherMethods {

	static ExtentSparkReporter reporter;
	static ExtentReports logger;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		reporter=new ExtentSparkReporter("Reports\\result_1.html");
		
		// Customize the View and Order of View in the HTML Report
		ViewName[] userView={ViewName.DASHBOARD,ViewName.DEVICE,ViewName.TEST,ViewName.CATEGORY};
		reporter.viewConfigurer()
		.viewOrder()
		.as(userView)
		.apply();
		
		logger=new ExtentReports();
		logger.attachReporter(reporter);
}
}
Customizing views in the HTML Report.

Custom Logs and Markup

Other than all these methods and functionality Extent Reports provides the facility of custom logs. Users can generate custom logs. Also, it provides many Markups which can be used to generate user-defined logs. Please refer to the below link to learn about Custom Logs and Markup.


ExtentReports:


Leave a Comment

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

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.