Generating Extent Reports – CSharp
Extent reports are very rich HTML reports for the selenium webdriver. This report will give so much of information about the execution status. The extra information can be provided from the external XML file. Once you create the XML file need to load in the program to read the configuration.
We can provide below information to the report using XML file:
- Report Title
- Report Name
- Report Headline
- Date Format
- Time Format
Apart from the above we can provide HostName, Environment Information and User Name etc… from the program.
Below is the sample program:
using NUnit.Framework; using NUnit.Framework.Interfaces; using RelevantCodes.ExtentReports; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ExtentReportsDemo { [TestFixture] public class BasicReport { public ExtentReports extent; public ExtentTest test; [OneTimeSetUp] public void StartReport() { string path = System.Reflection.Assembly.GetCallingAssembly().CodeBase; string actualPath = path.Substring(0, path.LastIndexOf("bin")); string projectPath = new Uri(actualPath).LocalPath; string reportPath = projectPath + "Reports\\MyOwnReport.html"; extent = new ExtentReports(reportPath, true); extent .AddSystemInfo("Host Name", "Krishna") .AddSystemInfo("Environment", "QA") .AddSystemInfo("User Name", "Krishna Sakinala"); extent.LoadConfig(projectPath + "extent-config.xml"); } [Test] public void DemoReportPass() { test = extent.StartTest("DemoReportPass"); Assert.IsTrue(true); test.Log(LogStatus.Pass, "Assert Pass as condition is True"); } [Test] public void DemoReportFail() { test = extent.StartTest("DemoReportFail"); Assert.IsTrue(false); test.Log(LogStatus.Pass, "Assert Fail as condition is False"); } [TearDown] public void GetResult() { var status = TestContext.CurrentContext.Result.Outcome.Status; var stackTrace = "<pre>" + TestContext.CurrentContext.Result.StackTrace + "</pre>"; var errorMessage = TestContext.CurrentContext.Result.Message; if (status == TestStatus.Failed) { test.Log(LogStatus.Fail, stackTrace + errorMessage); } extent.EndTest(test); } [OneTimeTearDown] public void EndReport() { extent.Flush(); extent.Close(); } } }
In the above program, we have used two classes called ExtentReports, ExtentTest. These two classes objects will help us to create a rich HTML reports with lot of useful information. ExtentReports will take the HTML report file path as an argument and it will create the report file with the mentioned name in the mentioned location. And ExtentTest will be used to log the so much information to the report like PASS, FAIL and SKIP status of the executed tests.
Important points to be remember:
- ExtentReports will create the report file.
- ExtentTest will log the information in the report.
- StartTest() method of ExtentReports class is the starting point of the test and it will return the ExtentTest object.
- We need to capture that object into ExtentTest object.
- Used this reference to log the information into the report.
- ExtentReports object will be used to add the report information like Title, Header and Theme etc..
- And the above configuration need to passed from the external XML file using LoadConfig() method of ExtentReports class. It will take the XML file path as argument.
- EndTest() method of ExtentReports will stop capturing information about the test log.
- Flush() method of ExtentReports will push/write everything to the document.
- Close() method of ExtentReports will clear/close all resource of the ExtentReports object.
Below is the sample configuration file:
<?xml version="1.0" encoding="UTF-8"?> <extentreports> <configuration> <!-- report theme --> <!-- standard, dark --> <theme>standard</theme> <!-- document encoding --> <!-- defaults to UTF-8 --> <encoding>UTF-8</encoding> <!-- protocol for script and stylesheets --> <!-- defaults to https --> <protocol>https</protocol> <!-- title of the document --> <documentTitle>AutomationTesting.in</documentTitle> <!-- report name - displayed at top-nav --> <reportName>Automation Testing Report</reportName> <!-- report headline - displayed at top-nav, after reportHeadline --> <reportHeadline>- QA Environment</reportHeadline> <!-- global date format override --> <!-- defaults to yyyy-MM-dd --> <dateFormat>yyyy-MM-dd</dateFormat> <!-- global time format override --> <!-- defaults to HH:mm:ss --> <timeFormat>HH:mm:ss</timeFormat> <!-- custom javascript --> <scripts> <![CDATA[ $(document).ready(function() { }); ]]> </scripts> <!-- custom styles --> <styles> <![CDATA[ ]]> </styles> </configuration> </extentreports>
This ways we can configure the report information and can generate the extent reports.
Output of the above program is:
Please watch the YouTube video of this blog for better understanding.