SpecFlow.VisualStudio
SpecFlow.VisualStudio copied to clipboard
C# Specflow - BeforeTestRun hooks not executing with multiple project in single solution
Used Visual Studio
Visual Studio 2019
Are the latest Visual Studio updates installed?
Yes
SpecFlow Section in app.config or content of specflow.json
Issue Description
when I use [BeforeTestRun], the method is not even called during run in CI in teamcity. While this is working fine in debugging. I'm using hook file. I searched here for solution in many questions. Also I'm using multiple projects in single solution. Each project has its own hook file.
using AventStack.ExtentReports; using AventStack.ExtentReports.Gherkin.Model; using AventStack.ExtentReports.Reporter; using BoDi; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Edge; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Remote; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Reflection; using TechTalk.SpecFlow;
namespace MBV.Foundation.ecom.Testing.GeneralHooks { [Binding] public class Generic { private static ScenarioContext _scenarioContext; private static ExtentReports _extentReports; private static ExtentHtmlReporter _extentHtmlReporter; private static ExtentTest _feature; private static ExtentTest _scenario; private readonly IObjectContainer _objectContainer; private IWebDriver driver;
public Generic(IObjectContainer objectContainer)
{
_objectContainer = objectContainer;
}
[BeforeTestRun]
public static void BeforeTestRun()
{
_extentHtmlReporter = new ExtentHtmlReporter(String.Format(Constants.DataSheets.TestingOnlyReports, $"{Environment.UserName}_{System.DateTime.Now.ToString("yyyyMMddHHmmss")}"));
_extentHtmlReporter.Configuration().Theme = AventStack.ExtentReports.Reporter.Configuration.Theme.Dark;
_extentReports = new ExtentReports();
_extentReports.AttachReporter(_extentHtmlReporter);
//_extentHtmlReporter.LoadConfig(Constants.DataSheets.XmlPath);
}
[BeforeFeature]
public static void BeforeFeatureStart(FeatureContext featureContext)
{
if (null != featureContext)
{
_feature = _extentReports.CreateTest<Feature>(featureContext.FeatureInfo.Title, featureContext.FeatureInfo.Description);
}
}
[BeforeScenario]
public void BeforeScenarioStart(ScenarioContext scenarioContext)
{
//var browser = TestContext.Parameters.Get("BrowserType");
var browser = ConfigurationManager.AppSettings["BrowserType"];
SelectBrowser(browser);
if (null != scenarioContext)
{
_scenarioContext = scenarioContext;
_scenario = _feature.CreateNode<Scenario>(scenarioContext.ScenarioInfo.Title, scenarioContext.ScenarioInfo.Description);
}
}
[AfterStep]
public void AfterEachStep()
{
ScenarioBlock scenarioBlock = _scenarioContext.CurrentScenarioBlock;
switch (scenarioBlock)
{
case ScenarioBlock.Given:
CreateNode<Given>();
break;
case ScenarioBlock.When:
CreateNode<When>();
break;
case ScenarioBlock.Then:
CreateNode<Then>();
break;
default:
CreateNode<And>();
break;
}
}
public void CreateNode<T>() where T : IGherkinFormatterModel
{
Console.WriteLine(_scenarioContext.ScenarioExecutionStatus.ToString());
if (_scenarioContext.ScenarioExecutionStatus.ToString() == "TestSkipped")
{
_scenario.CreateNode<T>(_scenarioContext.StepContext.StepInfo.Text).Skip("Step Definition Pending");
}
else if (_scenarioContext.TestError != null)
{
_scenario.CreateNode<T>(_scenarioContext.StepContext.StepInfo.Text).Fail(_scenarioContext.TestError.Message);
//_feature.AddScreenCaptureFromPath(Directory.GetParent(Assembly.GetExecutingAssembly().Location).Parent.Parent.FullName + "\\Reports\\pic.jpg").Fail(_scenarioContext.TestError.Message, MediaEntityBuilder.CreateScreenCaptureFromPath(".\\" + Directory.GetParent(Assembly.GetExecutingAssembly().Location).Parent.Parent.FullName + "\\Reports\\pic.jpg").Build());
}
else
{
_scenario.CreateNode<T>(_scenarioContext.StepContext.StepInfo.Text);
}
}
[AfterScenario]
public void CleanUp()
{
driver.Quit();
_objectContainer.Dispose();
}
[AfterTestRun]
public static void TearDownReport()
{
_extentReports.Flush();
}
public void SelectBrowser(String browserType)
{
switch (browserType)
{
case "Chrome":
DesiredCapabilities caps = new DesiredCapabilities();
ChromeOptions options = new ChromeOptions();
options.AddArguments(
"start-maximized",
"enable-automation",
"--no-sandbox", //this is the relevant other arguments came from solving other issues
"--disable-infobars",
"--disable-dev-shm-usage",
"--disable-browser-side-navigation",
"--disable-gpu",
"--ignore-certificate-errors");
Dictionary<string, object> prefs = new Dictionary<string, object>();
prefs.Add("profile.default_content_settings.geolocation", 1);
caps.SetCapability(ChromeOptions.Capability, prefs);
driver = new ChromeDriver(options);
_objectContainer.RegisterInstanceAs<IWebDriver>(driver);
driver.Manage().Window.Maximize();
break;
default:
throw new NotSupportedException($"{browserType} is not a supported browser");
}
}
}
}
Steps to Reproduce
Due to above [BeforeTestRun] not executing, I'm getting error in [BeforeFeatureStart] for '_extentReports' variable is null. Because this is being initialized in [BeforeTestRun] as per above hook file.
Link to Repository Project
No response
I already went through below issue. However I'm not making such mistake. I'm already using objectcontainer.
https://github.com/SpecFlowOSS/SpecFlow/issues/1741
I'm getting below error message during execution from team server. 14:48:08 MBV.Foundation.customer.Testing.Features.ProductListingPageFeature.MIRUKMVE_T3019_2TheUserDeletesTheirCookiesOnValueMyVehicleForm 14:48:08 System.NullReferenceException : Object reference not set to an instance of an object. 14:48:08 at MBV.Foundation.customer.Testing.GeneralHooks.Generic.BeforeFeatureStart(FeatureContext featureContext) in C:\BuildAgent\work\79c82bdb2de10fc1\src\Foundation\Testing\customer\GeneralHooks\Generic.cs:line 55 at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration) in D:\a\1\s\TechTalk.SpecFlow\Bindings\BindingInvoker.cs:line 69 at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.InvokeHook(IBindingInvoker invoker, IHookBinding hookBinding, HookType hookType) in D:\a\1\s\TechTalk.SpecFlow\Infrastructure\TestExecutionEngine.cs:line 352 at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireEvents(HookType hookType) in D:\a\1\s\TechTalk.SpecFlow\Infrastructure\TestExecutionEngine.cs:line 338 at MBV.Foundation.customer.Testing.Features.ProductListingPageFeature.FeatureSetup()
I was experiencing this issue as well, but I found the reason was that I was incorrectly referencing SpecFlow.NUnit.Runners in a class library in the solution that I referenced from the actual test project. I fixed the issue by ONLY referencing the core SpecFlow package in the class library, and only referencing SpecFlow.NUnit.Runners from the actual test project. Issue cleared up after that and BeforeTestRun/AfterTestRun worked as expected. I have multiple test projects in my solution as well and they're all working now.
I'm not sure if this is the same issue others are experiencing, but hopefully this helps.