Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize webdriver inside BeforeFeature method

I have following problem. I need to register my webdriver instance inside of the specflow BeforeFeature method using context injection, but this method must be static. I have an error that my global container object have to be static. Is there any way to use this context injection in static method?

Here is my code:

[Binding]
public class SpecflowHooks
{
    private readonly IObjectContainer container;

    public SpecflowHooks(IObjectContainer container)
    {
        this.container = container;
    }

    [BeforeFeature]
    public static void OneTime()
    {
        ChromeOptions options = new ChromeOptions();
        options.AddArgument("--ignore-ssl-errors=yes");
        options.AddArgument("--ignore-certificate-errors");
        ChromeDriver driver = new ChromeDriver();
        driver.Manage().Window.Maximize();
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);

        container.RegisterInstanceAs<IWebDriver>(driver);
    }

    [BeforeScenario]
    public void SetUp()
    {

    }

    [AfterScenario]
    public void TearDown()
    {

    }

    [AfterFeature]
    public static void FeatureTearDown()
    {
        IWebDriver driver = container.Resolve<IWebDriver>();

        driver.Close();
        driver.Dispose();
    }
}
like image 346
Bohdan Avatar asked Mar 15 '26 23:03

Bohdan


1 Answers

I presume you want to initialize the web driver only once per feature, and reuse an existing web driver object for each scenario in the feature.

As you noted, the BeforeFeature hook is static. You can still use this to initialize the web driver, but assign it to a static field on your hooks class. Then inside a BeforeScenario, register the static web driver object with the dependency injection container:

[Binding]
public class SpecflowHooks
{
    private static IWebDriver driver;
    private readonly IObjectContainer container;

    public SpecflowHooks(IObjectContainer container)
    {
        this.container = container;
    }

    [BeforeFeature]
    public static void OneTime()
    {
        ChromeOptions options = new ChromeOptions();
        options.AddArgument("--ignore-ssl-errors=yes");
        options.AddArgument("--ignore-certificate-errors");

        driver = new ChromeDriver(options); // <-- don't forget to pass 'options' here
        driver.Manage().Window.Maximize();
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);

    }

    [BeforeScenario]
    public void SetUp()
    {
        container.RegisterInstanceAs<IWebDriver>(driver);
    }

    [AfterScenario]
    public void TearDown()
    {
    }

    [AfterFeature]
    public static void FeatureTearDown()
    {
        if (driver == null)
            return;

        driver.Close();
        driver.Dispose();
        driver = null;
    }
}
like image 161
Greg Burghardt Avatar answered Mar 18 '26 11:03

Greg Burghardt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!