Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# Specflow - BeforeTestRun hooks not executing with multiple project in single solution #207

Open
Archit-Jindal opened this issue Dec 19, 2022 · 3 comments

Comments

@Archit-Jindal
Copy link

Used Visual Studio

Visual Studio 2019

Are the latest Visual Studio updates installed?

Yes

SpecFlow Section in app.config or content of specflow.json

Debug AnyCPU {47BA4404-C1D3-49FA-BB5A-F513CDA20BAB} Library Properties MBV.Foundation.customer.Testing MBV.Foundation.customer.Testing v4.7.1 512 {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 15.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages False UnitTest true full false bin\Debug\ DEBUG;TRACE prompt 4 AnyCPU pdbonly true bin\Release\ TRACE prompt 4 3.3.14.5 4.7.0 3.12.0 1.4.1 6.0.1 1.0.7 3.6.0 3.6.0 3.1.3 25.0.2 3.7.0 1.4.9.5 1.4.9.5 3.0.0 2.7.2 2.7.2 2.7.2 2.1.0 2.1.0 12.0.1 3.11.1 3.11.1 3.16.1 106.11.4 3.141.0 3.141.0 104.0.5112.7900 0.26.0.1 3.150.1 3.3.30 3.3.30 3.3.30 4.3.0 4.3.0 4.3.4 4.3.0 4.3.0 4.3.0 4.0.0 4.3.0 4.3.0 4.3.0 4.3.0 4.4.0 4.4.0 1.3.7 3.9.0 Always Designer {3087EEDF-0173-4D9E-B5F4-BA42D8F31F7E} MBV.Foundation.Accounts {1644e893-9f94-4398-837b-381f95cf40da} MBV.Foundation.api.Testing {80366ec9-5928-44b0-b80d-cc38bc13033d} MBV.Foundation.ecom.Testing {b8c307f7-d49b-475f-857f-30f63948f90c} MBV.Foundation.Specflow.Testing

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

@Archit-Jindal
Copy link
Author

I already went through below issue. However I'm not making such mistake. I'm already using objectcontainer.

SpecFlowOSS/SpecFlow#1741

@Archit-Jindal Archit-Jindal changed the title BeforeTestRun tag not executing with multiple project in single solution C# Specflow - BeforeTestRun hooks not executing with multiple project in single solution Dec 19, 2022
@Archit-Jindal
Copy link
Author

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()

@JeroMiya
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants