Skip to content

Commit

Permalink
Added Extent reporting with complete code changes in Hooks using feat…
Browse files Browse the repository at this point in the history
…urecontext, ScenarioContext and ScenarioStepContext
  • Loading branch information
executeautomation committed Apr 27, 2018
1 parent 2b578ea commit b97a4fd
Show file tree
Hide file tree
Showing 9 changed files with 636 additions and 16 deletions.
452 changes: 452 additions & 0 deletions SpecflowParallelTest/ExtentReport.html

Large diffs are not rendered by default.

80 changes: 80 additions & 0 deletions SpecflowParallelTest/Hooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
using System.Reflection;
using OpenQA.Selenium.Remote;
using TechTalk.SpecFlow;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using AventStack.ExtentReports.Gherkin.Model;

namespace SpecflowParallelTest
{
[Binding]
public class Hooks
{
//Global Variable for Extend report
private static ExtentTest featureName;
private static ExtentTest scenario;
private static ExtentReports extent;

private readonly IObjectContainer _objectContainer;

Expand All @@ -21,10 +28,83 @@ public Hooks(IObjectContainer objectContainer)
_objectContainer = objectContainer;
}

[BeforeTestRun]
public static void InitializeReport()
{
//Initialize Extent report before test starts
var htmlReporter = new ExtentHtmlReporter(@"C:\extentreport\SeleniumWithSpecflow\SpecflowParallelTest\ExtentReport.html");
htmlReporter.Configuration().Theme = AventStack.ExtentReports.Reporter.Configuration.Theme.Dark;
//Attach report to reporter
extent = new ExtentReports();
extent.AttachReporter(htmlReporter);
}

[AfterTestRun]
public static void TearDownReport()
{
//Flush report once test completes
extent.Flush();
}

[BeforeFeature]
public static void BeforeFeature()
{
//Create dynamic feature name
featureName = extent.CreateTest<Feature>(FeatureContext.Current.FeatureInfo.Title);
}

[AfterStep]
public void InsertReportingSteps()
{

var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();

PropertyInfo pInfo = typeof(ScenarioContext).GetProperty("TestStatus", BindingFlags.Instance | BindingFlags.NonPublic);
MethodInfo getter = pInfo.GetGetMethod(nonPublic: true);
object TestResult = getter.Invoke(ScenarioContext.Current, null);

if (ScenarioContext.Current.TestError == null)
{
if (stepType == "Given")
scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text);
else if (stepType == "When")
scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text);
else if (stepType == "Then")
scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text);
else if (stepType == "And")
scenario.CreateNode<And>(ScenarioStepContext.Current.StepInfo.Text);
}
else if (ScenarioContext.Current.TestError != null)
{
if (stepType == "Given")
scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.InnerException);
else if (stepType == "When")
scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.InnerException);
else if (stepType == "Then")
scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.Message);
}

//Pending Status
if (TestResult.ToString() == "StepDefinitionPending")
{
if (stepType == "Given")
scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");
else if (stepType == "When")
scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");
else if (stepType == "Then")
scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).Skip("Step Definition Pending");

}

}


[BeforeScenario]
public void Initialize()
{
SelectBrowser(BrowserType.Firefox);
//Create dynamic scenario name
scenario = featureName.CreateNode<Scenario>(ScenarioContext.Current.ScenarioInfo.Title);
}

[AfterScenario]
Expand Down
2 changes: 1 addition & 1 deletion SpecflowParallelTest/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: Parallelizable(ParallelScope.Fixtures)]
//[assembly: Parallelizable(ParallelScope.Fixtures)]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
Expand Down
41 changes: 34 additions & 7 deletions SpecflowParallelTest/SpecflowParallelTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,55 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="ExtentReports, Version=3.1.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ExtentReports.3.1.3\lib\ExtentReports.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack, Version=1.4.9.5, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.4.9.5\lib\Net45\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="ImpromptuInterface, Version=6.2.2.0, Culture=neutral, PublicKeyToken=0b1781c923b2975b, processorArchitecture=MSIL">
<HintPath>..\packages\ImpromptuInterface.6.2.2\lib\net40\ImpromptuInterface.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="MongoDB.Bson, Version=2.4.0.70, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDB.Bson.2.4.0\lib\net45\MongoDB.Bson.dll</HintPath>
</Reference>
<Reference Include="MongoDB.Driver, Version=2.4.0.70, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDB.Driver.2.4.0\lib\net45\MongoDB.Driver.dll</HintPath>
</Reference>
<Reference Include="MongoDB.Driver.Core, Version=2.4.0.70, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDB.Driver.Core.2.4.0\lib\net45\MongoDB.Driver.Core.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=3.7.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.7.1\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="RazorEngine, Version=3.9.0.0, Culture=neutral, PublicKeyToken=9ee697374c7e744a, processorArchitecture=MSIL">
<HintPath>..\packages\RazorEngine.3.9.0\lib\net45\RazorEngine.dll</HintPath>
</Reference>
<Reference Include="SpecFlow.Assist.Dynamic, Version=1.3.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SpecFlow.Assist.Dynamic.1.3.1\lib\45\SpecFlow.Assist.Dynamic.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.0.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll</HintPath>
</Reference>
<Reference Include="TechTalk.SpecFlow, Version=2.2.0.0, Culture=neutral, PublicKeyToken=0778194805d6db41, processorArchitecture=MSIL">
<HintPath>..\packages\SpecFlow.2.2.0\lib\net45\TechTalk.SpecFlow.dll</HintPath>
</Reference>
<Reference Include="WebDriver, Version=3.11.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Selenium.WebDriver.3.11.0\lib\net45\WebDriver.dll</HintPath>
<Reference Include="WebDriver, Version=3.11.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Selenium.WebDriver.3.11.2\lib\net45\WebDriver.dll</HintPath>
</Reference>
<Reference Include="WebDriver.Support, Version=3.11.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Selenium.Support.3.11.0\lib\net45\WebDriver.Support.dll</HintPath>
<Reference Include="WebDriver.Support, Version=3.11.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Selenium.Support.3.11.2\lib\net45\WebDriver.Support.dll</HintPath>
</Reference>
</ItemGroup>
<Choose>
Expand Down Expand Up @@ -107,13 +132,15 @@
<Generator>SpecFlowSingleFileGenerator</Generator>
<LastGenOutput>UserForm.feature.cs</LastGenOutput>
</None>
<None Include="license" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="..\packages\WebDriver.GeckoDriver.0.16.1\content\geckodriver.exe">
<Content Include="..\packages\WebDriver.GeckoDriver.0.20.1\content\geckodriver.exe">
<Link>geckodriver.exe</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="extent-config.xml" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
Expand All @@ -135,12 +162,12 @@
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Selenium.WebDriver.ChromeDriver.2.31.0\build\Selenium.WebDriver.ChromeDriver.targets" Condition="Exists('..\packages\Selenium.WebDriver.ChromeDriver.2.31.0\build\Selenium.WebDriver.ChromeDriver.targets')" />
<Import Project="..\packages\Selenium.WebDriver.ChromeDriver.2.38.0.1\build\Selenium.WebDriver.ChromeDriver.targets" Condition="Exists('..\packages\Selenium.WebDriver.ChromeDriver.2.38.0.1\build\Selenium.WebDriver.ChromeDriver.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Selenium.WebDriver.ChromeDriver.2.31.0\build\Selenium.WebDriver.ChromeDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Selenium.WebDriver.ChromeDriver.2.31.0\build\Selenium.WebDriver.ChromeDriver.targets'))" />
<Error Condition="!Exists('..\packages\Selenium.WebDriver.ChromeDriver.2.38.0.1\build\Selenium.WebDriver.ChromeDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Selenium.WebDriver.ChromeDriver.2.38.0.1\build\Selenium.WebDriver.ChromeDriver.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
5 changes: 4 additions & 1 deletion SpecflowParallelTest/Steps/LoginSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using SpecflowParallelTest.Pages;
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Assist;
using AventStack.ExtentReports.Reporter;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Gherkin.Model;

namespace SpecflowParallelTest.Steps
{
Expand Down Expand Up @@ -53,7 +56,7 @@ public void ThenIShouldSeeUserLoggedInToTheApplication()
Assert.Multiple(() =>
{
//Assert.That(element.Text, Is.Null, "Header text not found !!!");
Assert.That(element.Text, Is.Not.Null, "Header text not found !!!");
Assert.That(element.Text, Is.Null, "Header text not found !!!");
});
}

Expand Down
6 changes: 6 additions & 0 deletions SpecflowParallelTest/Steps/UserFormSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public void GivenIVerifyTheEnteredUserFormDetailsInTheApplicationDatabase(Table

}

[Then(@"I logout of application")]
public void ThenILogoutOfApplication()
{
ScenarioContext.Current.Pending();
}


}

Expand Down
44 changes: 44 additions & 0 deletions SpecflowParallelTest/extent-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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>Extent</documentTitle>

<chartVisibilityOnOpen>true</chartVisibilityOnOpen>

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

<!-- location of charts in the test view -->
<!-- top, bottom -->
<testViewChartLocation>bottom</testViewChartLocation>

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

<!-- custom styles -->
<styles>
<![CDATA[
]]>
</styles>
</configuration>
</extentreports>
Empty file added SpecflowParallelTest/license
Empty file.
22 changes: 15 additions & 7 deletions SpecflowParallelTest/packages.config
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ExtentReports" version="3.1.3" targetFramework="net452" />
<package id="HtmlAgilityPack" version="1.4.9.5" targetFramework="net452" />
<package id="ImpromptuInterface" version="6.2.2" targetFramework="net452" />
<package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net452" />
<package id="MongoDB.Bson" version="2.4.0" targetFramework="net452" />
<package id="MongoDB.Driver" version="2.4.0" targetFramework="net452" />
<package id="MongoDB.Driver.Core" version="2.4.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
<package id="NUnit" version="3.7.1" targetFramework="net452" />
<package id="NUnit.Console" version="3.7.0" targetFramework="net452" />
<package id="NUnit.ConsoleRunner" version="3.7.0" targetFramework="net452" />
<package id="NUnit.Extension.NUnitProjectLoader" version="3.5.0" targetFramework="net452" />
<package id="NUnit.Extension.NUnitV2Driver" version="3.6.0" targetFramework="net452" />
<package id="NUnit.Extension.NUnitV2ResultWriter" version="3.5.0" targetFramework="net452" />
<package id="NUnit.Extension.TeamCityEventListener" version="1.0.2" targetFramework="net452" />
<package id="NUnit.Extension.VSProjectLoader" version="3.5.0" targetFramework="net452" />
<package id="Selenium.Support" version="3.11.0" targetFramework="net452" />
<package id="Selenium.WebDriver" version="3.11.0" targetFramework="net452" />
<package id="Selenium.WebDriver.ChromeDriver" version="2.31.0" targetFramework="net452" />
<package id="NUnit.Extension.NUnitV2ResultWriter" version="3.6.0" targetFramework="net452" />
<package id="NUnit.Extension.TeamCityEventListener" version="1.0.3" targetFramework="net452" />
<package id="NUnit.Extension.VSProjectLoader" version="3.7.0" targetFramework="net452" />
<package id="RazorEngine" version="3.9.0" targetFramework="net452" />
<package id="Selenium.Support" version="3.11.2" targetFramework="net452" />
<package id="Selenium.WebDriver" version="3.11.2" targetFramework="net452" />
<package id="Selenium.WebDriver.ChromeDriver" version="2.38.0.1" targetFramework="net452" />
<package id="SpecFlow" version="2.2.0" targetFramework="net452" />
<package id="SpecFlow.Assist.Dynamic" version="1.3.1" targetFramework="net452" />
<package id="WebDriver.GeckoDriver" version="0.16.1" targetFramework="net452" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.0.0" targetFramework="net452" />
<package id="WebDriver.GeckoDriver" version="0.20.1" targetFramework="net452" />
</packages>

0 comments on commit b97a4fd

Please sign in to comment.