Monday, April 13, 2020

Cucumber Selenium test automation framework using C# - Part 7 - Adding a test report with logs

Prerequisites


Please complete Part 6 of this tutorial before continuing.

Adding a test report using Extent Reports


1) Install 'ExtentReports' via Nuget Package Manager


Extent reporting is a html reporting tool which can be used with Cucumber. 

2) Creating the report and closing the report can be done in our Hooks.cs class. In the Hooks.cs class, add BeforeTestRun section.



3) Import following packages.

using AventStack.ExtentReports;
using AventStack.ExtentReports.Gherkin.Model;
using AventStack.ExtentReports.Reporter;

Add a extent-config.xml file to add configurations with following content.

<?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>Test Automation Report</documentTitle>

    <!-- report name - displayed at top-nav -->
    <reportName>Test Automation Report</reportName>

    <!-- timestamp format -->
    <timeStampFormat>MMM dd, yyyy HH:mm:ss</timeStampFormat>

    <!-- custom javascript -->
    <scripts>
      <![CDATA[
                $(document).ready(function() {
                    
                });
            ]]>
    </scripts>

    <!-- custom styles -->
    <styles>
      <![CDATA[
                
            ]]>
    </styles>
  </configuration>
</extentreports>

4) Add the implementation as given below.



5) Add the closing of report to the AfterTestRun method.


6) Add feature information to the report


7) Add scenario information to the report


8) Add step information to the report

var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();
            PropertyInfo pInfo = typeof(ScenarioContext).GetProperty("ScenarioExecutionStatus", BindingFlags.Instance | BindingFlags.Public);
            MethodInfo getter = pInfo.GetGetMethod(nonPublic: true);
            object TestResult = getter.Invoke(ScenarioContext.Current, null);
            if (ScenarioContext.Current.TestError == null)
            {
                if (stepType == "Given")
                {
                    step = scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text);
                }
                else if (stepType == "When")
                {
                    step = scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text);
                }
                else if (stepType == "Then")
                {
                    step = scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text);
                }
            }
            else if (ScenarioContext.Current.TestError != null)
            {
                if (stepType == "Given")
                {
                    step = scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
                }
                else if (stepType == "When")
                {
                    step = scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
                }
                else if (stepType == "Then")
                {
                    step = scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
                }
            }
            if (TestResult.ToString() == "StepDefinitionPending")
            {
                if (stepType == "Given")
                {
                    step = scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");
                    step.Log(Status.Skip, "STEP DEFINITION PENDING");
                }
                else if (stepType == "When")
                {
                    step = scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");
                    step.Log(Status.Skip, "STEP DEFINITION PENDING");
                }
                else if (stepType == "Then")
                {
                    step = scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString() + " " + ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");
                    step.Log(Status.Skip, "STEP DEFINITION PENDING");
                }
            }

9) Add end report and end log statements to AfterTestRun method.


10) Add logs as needed in project


11) Build and run the tests. Navigate to location where report is created.


12) Open MyReport.html to view test report.

13) Open index.html to view the log.



Sunday, April 12, 2020

Cucumber Selenium test automation framework using C# - Part 6 - Filtering tests using tags

Prerequisites


Please complete Part 5 of this tutorial before continuing.


Filtering tests using tags


1) Before this, add a new scenario to the feature file and implement steps for as given below.



2) Add SpecRun profile to the project as given below. SpecRun profile will be used to add configurations on how the test suite should run. 



3) In the SpecRun profile add the filter tag to filter the scenarios you need to run. Filter can be applied to feature file or scenario.


4) Now build the project and see if test explorer shows both scenarios as given below.


5) Remove or change @google tag on one scenario and then rebuild the project. Navigate to test explorer. Test explorer should only show the scenario with google tag name.


Cucumber Selenium test automation framework using C# - Part 5 - Adding assertions

Prerequisites


Please complete Part 4 of this tutorial before continuing.


Adding assertions for each step


When running a test, assertions help to make sure the application is behaving as per expected behavior. For the test framework, assertions can be implemented using NUnit. 

Navigate back to 'And I'm in google home page' step in GoogleSearch.feature file. In this step before entering a value to search text box, we need to make sure search text box is displayed. Unless the search text box is displayed, user will not be able to enter any text to it. 

Selenium provides a way of waiting until an element is present before carrying out an action on it. 

1) In the SeleniunHelper.cs class, implement the following method.



This method will check an element is displayed in UI for a configured time period. If element is not displayed within given time frame, method will throw an exception. 

Use the method in page object class as following.



Then call the implemented method in step class to check whether element is displayed or not before proceeding to next step.



Similarly, selenium provides many ways of implementing assertions as per requirement.