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;
[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); } }
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; }
No comments:
Post a Comment