Report Module Implementation in a Test Automation Framework

In this tutorial for Creating a test automation framework with C#, Selenium 3 and Nunit, we will learn how to implement the report module we created in our test automation framework in the previous tutorial . First, we’ll begin with the creation of a new class –  ‘ReportsManager’ which will be responsible for generating the reports and writing in the report. This will be the only class that will use the reporting management module built in the previous tutorial.


Tutorial Overview:

Class 1 – Creating an Automated Test Using Selenium WebDriver 3 and C#

Class 2 – How to Create a Test Automation Framework Architecture with Selenium WebDriver 3

Class 3 – Utilizing Test Automation Framework with Advanced Capabilities


The class’s code:

public class ReportsManager
{
    Reports report;
    string browser;
    string url;
    IWebDriver driver = Browsers.getDriver;

    public ReportsManager(string browser, string url)
    {
        this.browser = browser;
        this.url = url;
        report = new Reports(browser, url);
    }

    public void verifyURL(string url)
    {
        string PageURL = driver.Url;
        string message = "The Current Url and Expected Url are not equals";

        if (PageURL.Equals(url))
            report.addLine("Verify url", "PASS", "Url are Equals");
        else
            report.addLine("Verify url", "FAIL", message);

        Assert.AreEqual(PageURL, url, message);
    }

    public void verifyElementNotAppear(IWebElement element)
    {
        if(!element.Displayed)
        {
            string message = "Element does not exist";
            Assert.Fail(message);
            addLog("Looking For Element" + element + "", "FAIL", element + " Should Be On The Page");
        }
    }
    public void addLog(string des, string res, string exp)
    {
        report.addLine(des, res, exp);
    }
}

Lets get into more detail of the code, starting with the properties.

Reports report;
string browser;
string url;
IWebDriver driver = Browsers.getDriver;

The report type variable is our reporting class. The variables browser, url and driver we’ll receive from our Browsers class built in the previous tutorial. Next, let’s move on to the functions of the class: the constructor will receive the Browser’s type and the website’s url. We’ll create a new instance of the reporting system and transfer the parameters to the report.

verifyURL – A function receiving the website’s url and verifies that we are on the actual page found by the browser. In order to obtain the current url of the browser, we’ll use driver.Url. This is the reason behind creating a variable of the driver type in the beginning. Afterwards, we will compare the urls and write a relevant message for the report.

VerifyElementNotAppear A function that checks if element does not exist on page and writes the result in the report.

addLine – A function which writes a line inside our report.

Obviously the example presented above is merely a basic demonstration of the reporting system and it’s highly recommended  to add more functions for writing more robust logs.

In order to execute the reporting class inside our automated testing framework, let’s move on to the Browsers class:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Firefox;
using System.Configuration;
namespace Test
{
    public class Browsers
    {
        private static IWebDriver webDriver;
        public static ReportsManager reports; // adding the report vaiable
        private static string baseURL = ConfigurationManager.AppSettings["url"];
        private static string browser = ConfigurationManager.AppSettings["browser"];

        public static void Init()
        {
            switch (browser)
            {
                case "Chrome":
                    webDriver = new ChromeDriver();
                 break;
                case "IE":
                    webDriver = new InternetExplorerDriver();
                break;
                case "Firefox":
                    webDriver = new FirefoxDriver();
                break;
            }
            webDriver.Manage().Window.Maximize();
            reports = new ReportsManager(browser, baseURL);  // Creating New Instance of report
            Goto(baseURL);
       }
       public static string Title
       {
            get { return webDriver.Title; }
       }
       public static IWebDriver getDriver
       {
            get { return webDriver; }
       }

       public static void Goto(string url)
       {
            webDriver.Url = url;
            reports.verifyURL(url); // Verifying the URL
       }
       public static void Close()
       {
            webDriver.Quit();
       }
    }
}

We’ve declared a new reporting object called ‘reports’.

Created new instance when the browsers is launched:

reports = new ReportsManager(browser, baseURL);

Every time we’ll navigate to the new url, we’ll verify that its at the right location:

reports.verifyURL(url);


The next part in the tutorial is Creating a functional test with the basic test automation framework we’ve built. In the upcoming tutorials, we’ll see more examples for using our report module.

Please leave a comment and ask your questions about this report module.