Execute Automation

With C#

Generate automation report using ExtentReports in C#

What and why we use ExtentReports:

ExtentReports is a library which we can integrate into our automation test to generate a test report with HTML format. Using ExtentReports makes our test reports  look more detail with a lot of features:

  • Represents test case status with the help of PIE Charts.
  • Maintain execution history with step-by-step test case summary, what step is pass and what step fails.
  • Attach Screenshots of test execution to the report.
  •  Filter reports based on test status (how many test cases are pass/ fail/ error)

Capture

How do we generate a report using ExtentReports:

In this post, we use automation test project with Selenium and NUnit framework (which has [SetUp], [Test], [TearDown] attributes).

  1.  Use Manage NuGet Packages in visual Studio to find and install ExtentReports (version 2.410)
  2. Go to http://extentreports.com/samples/extent.html to save HTML file to your local drive (file path ). Your report information will be written in this file.
  3. Create an instance of ExtentReports (shows how the report looks like) in  [SetUp] attribute, and ExtentTest (write information about the execution test to the report).
    It is possible to initialize ExtentReports in the following different ways:

    var extent = ExtentReports(string filePath)
    var extent = ExtentReports(string filePath, bool replaceExisting)
    var extent = ExtentReports(string filePath, DisplayOrder displayOrder)
    var extent = ExtentReports(string filePath, DisplayOrder displayOrder, bool replaceExisting)
    
    • filePath – path of the file, in .htm or .html format
    • replaceExisting – Setting to overwrite the existing file or append to it (default value is True).
    • displayOrder:  set the order of the report
      • OLDEST_FIRST (default) – oldest test at the top, newest at the end
      • NEWEST_FIRST – newest test at the top, oldest at the end

    4. Using the instance of ExtentTest to write test information including starting the test and step logs: pass, fail, error for each test case.

    // starting test
    var test = extent.StartTest("Test Name", "Description");
    // step log
    test.Log(LogStatus.Pass, "Step details");
    test.Log(LogStatus.Info, "Click on UI button link);

    We can insert a screenshot, by simply call addScreenCapture() function

    test.Log(LogStatus.Info, "Screenshot: " + test.addScreenCapture("screenshot-path"));

5.   Call EndTest () to finish our test in and Flush() in the [TearDown] area to writing information to the report.

// ending test
extent.EndTest(test);
// Flush test
extent.Flush();

6. Run the test and open the report file to check the test execution.

Here is the example of using ExtentReports with Selenium and NUnit framework project.

 [TestFixture]
 class ButtonPageTest
 { // Initial Report and Test instance
 public static ExtentReports report;
 public static ExtentTest test;
 /*-----------------------------------*/
 [SetUp]
 public void LoginTest()
 {
 // Define the Report 
 report = new ExtentReports(Base.reportFile, CultureInfo.GetCultureInfo("es-ES"), false, DisplayOrder.NewestFirst); 
 
 //Init and define chrome driver
 Base.driver = new ChromeDriver();

 // Call Login
 LoginPage objLogin = new LoginPage();
 objLogin.LoginTest();
 }
/*----------------------------------------*/
 [Test]
 public void AddButtonTest()
 {
 // Define the extent report test
 test = report.StartTest("This test case is to test Add button feature");

 //Call Add funtion:
 ButtonsPage objButton = new ButtonsPage();
 objButton.AddButton();
 }
/*-------------------------------------*/
 [TearDown]
 public static void CloseBrowser()
 {
 // Close browser
 Base.driver.Close();

 // End test ans flush report
 report.EndTest(test);
 report.Flush();
 }
 }

Inside each test function, we use ExtentTest instance to write information of test execution including logs of passed, failed or error steps.

Leave a Reply

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