In this tutorial for Creating a test automation framework with C#, Selenium 3 and Nunit, we’ll learn how to develop a reporting module so it will support the report deposition which will provide an indication on what failed, what passed and the specific point it occurred.
Tutorial Overview:
Class 1 – Creating an Automated Test Using Selenium WebDriver 3 and C#
- Setting Up the Development Environment for Your Selenium Automation Framework
- How to Inspect Web Elements & Methods to Locate Them with Chrome Devtools
- How to Find Web Elements with Selenium WebDriver
Class 2 – How to Create a Test Automation Framework Architecture with Selenium WebDriver 3
- How to Create a Cross Browser Compatible Testing Framework
- You’re here→Develop a Report Module in a Testing Framework
- Implementing a Report Module in an Automated Framework
- How to Write a Functional Test with a Basic Selenium Automation Framework
Class 3 – Utilizing Test Automation Framework with Advanced Capabilities
- Page Object Pattern: Advantages & Implementation
- Read Data From CSV File in C#
- How to Create a Test Suite in Selenium WebDriver and C#
- How to Use Data Driven with Selenium Test Suite
It’s important for our test automation framework to have a reporting module for a number of reasons:
- Gives us an indication about the location of the failure.
- An ability to compare different executions, assuming something failed in the previous execution.
- Automation developers can send an execution report summary to the relevant R&D team for an indication about the progress of the tests.
The structure of the report will be a CSV document containing the following fields:
- StepDescription
- Pass/Fail
- Exception
Let’s begin with creating a new class called ‘Reports’ and then we’ll paste the following code:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test { public class Reports { private string BrowserType; private string url; private DateTime date; private FileStream fs; private StringBuilder reportcsv; private string filePath; private string fileName; public Reports(string BrowserType, string url) { this.BrowserType = BrowserType; this.url = url; date = DateTime.Now; fileName = date.Date.Date.ToShortDateString() + date.TimeOfDay.Hours.ToString() + date.TimeOfDay.Minutes.ToString(); reportcsv = new StringBuilder(); filePath = @"C:\Users\TestProject\Desktop\Automation\" + fileName + ".csv"; createCsvFile(); } private void createCsvFile() { reportcsv.Append("StepDescription,"); reportcsv.Append("Pass/Fail,"); reportcsv.Append("Exception"); File.AppendAllText(filePath, reportcsv.ToString()); } public void addLine(string description, string result, string exception) { reportcsv.Append(Environment.NewLine); reportcsv.Append(description + ","); reportcsv.Append(result + ","); reportcsv.Append(exception + ","); reportcsv.Append(Environment.NewLine); File.WriteAllText(filePath, reportcsv.ToString()); } } }
There are number of variables and functions:
The names of the variables ‘date’ and ‘url’ are going be a part of the report data.
The purpose of the class builder is to initialize the report details as the tested browser’s type, website’s url, test date eventually turning to the following function:
createCsvFile – This function creates the document of our report, and initializes it with the following headers:
- StepDescription – Desciption of step in test
- Pass/Fail – Result of the test run.
- Exception – If exception was thrown.
addLine – A function that adds a line to our report. The function receives the step’s description, the result and the expected result.
After creating the reporting module, continue to the Next tutorial to Start implementing the reporting class in our test automation framework.
-Don’t hesitate to share your thoughts and testing stories.
Can you please post an example for developing report module in html format