0% found this document useful (0 votes)
20 views

CommonSteps Cs

The document defines a CommonSteps class used for test automation with SpecFlow. The class contains methods annotated with BeforeTestRun, AfterTestRun, BeforeFeature, AfterStep and AfterScenario hooks. The methods set up reporting with ExtentReports, generate report nodes for steps, capture screenshots on failure, and clean up the test run.

Uploaded by

Ola Tairu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

CommonSteps Cs

The document defines a CommonSteps class used for test automation with SpecFlow. The class contains methods annotated with BeforeTestRun, AfterTestRun, BeforeFeature, AfterStep and AfterScenario hooks. The methods set up reporting with ExtentReports, generate report nodes for steps, capture screenshots on failure, and clean up the test run.

Uploaded by

Ola Tairu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

using CourseManagementUITestAutomation.

Hooks;
using System.IO;
using TechTalk.SpecFlow;
using AventStack.ExtentReports.Reporter;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Gherkin.Model;
using System;

namespace CourseManagementUITestAutomation.StepDefinitions
{
[Binding]
public class CommonSteps
{
Context _context;
static ExtentTest feature;
static ExtentTest scenario;
static ExtentReports report;
ScenarioContext _scenarioContext;

public CommonSteps(Context context, ScenarioContext


scenarioContext)
{
_context = context;
_scenarioContext = scenarioContext;
}

[Given(@"that CMS application is loaded")]


public void GivenThatCMSApplicationIsLoaded()
{
_context.LoadCMSApplication();
scenario =
feature.CreateNode<Scenario>(_scenarioContext.ScenarioInfo.Title);
}

[BeforeTestRun]
public static void ReportGenerator()
{
var testResultReport = new
ExtentV3HtmlReporter(AppDomain.CurrentDomain.BaseDirectory + @"\
TestResult.html");
testResultReport.Config.Theme =
AventStack.ExtentReports.Reporter.Configuration.Theme.Dark;
report = new ExtentReports();
report.AttachReporter(testResultReport);
}

[AfterTestRun]
public static void ReportCleaner()
{
report.Flush();
}

[BeforeFeature]
public static void BeforeFeature(FeatureContext featureContext)
{
feature =
report.CreateTest<Feature>(featureContext.FeatureInfo.Title);
}
[AfterStep]
public void StepsInTheReport()
{
var typeOfStep =
ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();
//Cater for a step that passed
if (_scenarioContext.TestError == null)
{
if (typeOfStep.Equals("Given"))
{

scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text);
}
else if (typeOfStep.Equals("When"))
{

scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text);
}
else if (typeOfStep.Equals("Then"))
{

scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text);
}
}
//Cater for a step that failed
if (_scenarioContext.TestError != null)
{
if (typeOfStep.Equals("Given"))
{

scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Fai
l(_scenarioContext.TestError.Message);
}
else if (typeOfStep.Equals("When"))
{

scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Fail
(_scenarioContext.TestError.Message);
}
else if (typeOfStep.Equals("Then"))
{

scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).Fail
(_scenarioContext.TestError.Message);
}
}
//Cater for a step that has not been implemented
if
(_scenarioContext.ScenarioExecutionStatus.ToString().Equals("StepDefiniti
onPending"))
{
if (typeOfStep.Equals("Given"))
{

scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Ski
p("Step Definition Pending");
}
else if (typeOfStep.Equals("When"))
{

scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Skip
("Step Definition Pending");
}
else if (typeOfStep.Equals("Then"))
{

scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).Skip
("Step Definition Pending");
}
}
}

[AfterScenario]
public void CloseAUT()
{
try
{
if (_scenarioContext.TestError != null) //this condition
will always be true when a test fails
{
string scenarioName =
_scenarioContext.ScenarioInfo.Title;
string directory =
AppDomain.CurrentDomain.BaseDirectory + @"\Screenshots\";

_context.TakeScreenshotAtThePointOfTestFailure(directory, scenarioName);
}
}
catch (Exception)
{

}
finally
{
_context.ShutDownCMSApplication();
}

}
}
}

You might also like