September 15, 2022

How to append Pass/Fail status to name of extent report HTML itself

  September 15, 2022

ExtentReport is an open-source reporting library used in test automation. It provides an HTML report after execution which contains different details like Pass/Fail Status, Execution time,Screenshots, Test environment details etc

For the default extent report , we can decide the name of the HTML file to be generated but to see if the test is a Pass or Fail we need to open the extent report.

This would be a difficult task if we have multiple test reports and we need to find Pass/Fail reports separately.

We can simply solve this by appending the Pass/Fail status to the HTML file name itself.

After this, your reports will look like this in the file explorer.

To implement this first, we need to set a flag whenever a fail is logged. It wan be done as below when logging.

Fail case of extent report logger method is given below

case "fail":

	Screenshot fileFail = ((ITakesScreenshot)driver).GetScreenshot();
	string imageFail = fileFail.AsBase64EncodedString;
	exParent.Fail(msg, MediaEntityBuilder.CreateScreenCaptureFromBase64String(imageFail).Build());
	System.Diagnostics.Trace.WriteLine("FAIL >>>>  " + msg);
	
	//Set the Fail flag here
	boolFailFlag = true;
	
	break;

boolFailFlag which is a global boolean variable having a default value of false will be set as true whenever a fail log is added in the report.

Complete detail on creating the extent report logger method can be found at the link below


The next step is to rename the HTML file based on the flag value we got.

This can be done from the TestCleanup hook of the framework

[TestCleanup]
public void AfterTest()
{
	// Pass if boolFailFlag is false
	String resStatus = "PASS";
	if (boolFailFlag == true || !(TestContext.CurrentTestOutcome == UnitTestOutcome.Passed))
	{
		// Fail even if one log is fail or total outcome is fail
		WriteLog("fail", "Test Outcome :" + TestContext.CurrentTestOutcome+ " - Refer stack trace for details");
		resStatus = "FAIL";
	}

	extent.Flush();
	driver.Close();
	driver.Quit();
	driver.Dispose();

	// attaching report file to test run after renaming
	TestContext.AddResultFile(changeResultFileStatus(RunFile, resStatus));

	if (boolFailFlag == true || !(TestContext.CurrentTestOutcome == UnitTestOutcome.Passed))
	{
		Assert.Fail("Test failed due to follwing issues:" + strFailLogs);
	}

}

Based on the flag value and the initial name of the extent report, we can rename the report and replace it in the same location.

The following code can be used for this.

public string changeResultFileStatus(string fileName, string ResStatus)
{
	string newFileName = fileName;

	if (ResStatus.ToLower().Equals("pass"))
	{
	newFileName = fileName.Replace(".html", " [PASS].html");
	}
	else
	{
	newFileName = fileName.Replace(".html", " [FAIL].html");
	}

	System.IO.File.Move(fileName, newFileName);

	return newFileName;
}


logoblog

Thanks for reading How to append Pass/Fail status to name of extent report HTML itself

Previous
« Prev Post

No comments:

Post a Comment

How to send email from current Outlook instance using Java + VBScript

If we are using .Net, we can easily access outlook and send emails using its own libraries. But what if we are using Java? Here we can seek ...