简体   繁体   中英

Specflow Context Injection of IWebDriver failing

We have a suite of UI tests being run using Specflow and Selenium WebDriver. Overnight these suddenly stopped working and now throw the following error on each Scenario:


BoDi.ObjectContainerException : Interface cannot be resolved: OpenQA.Selenium.IWebDriver (resolution path: Steps class )

We're using the Specflow Context Injection on register our webdriver before each scenario, which we then use in each of our steps classes:

[Binding]
public class Base
{
    private readonly IObjectContainer _objectContainer;
    private IWebDriver _webDriver;

    public Base(IObjectContainer objectContainer)
    {
        _objectContainer = objectContainer;
    }

    [BeforeScenario]
    public void Setup()
    {
       _webDriver = new ChromeDriver();
       _objectContainer.RegisterInstanceAs<IWebDriver>(_webDriver);
    }

    ....
}

Steps file:

[Binding]
public class ProductSteps : TechTalk.SpecFlow.Steps
{
    private readonly IWebDriver _driver;

    public ProductSteps(IWebDriver driver)
    {
        _driver = driver;
    }
}

Looking online at the Specflow documentation I can see nothing wrong - and I can also find little to show anyone else ever having this problem!

I've spent a fair bit of time trying to get to the bottom of this but have had no luck whatsoever.

We're using NUnit as our test runner and have all the latest updates via nuget.

My guess is that you have another BeforeScenario hook on the ProductSteps class that might run earlier and forces the creating of the instance earlier than the other BeforeScenatio is triggered, so the web driver is not registered yet.

You can control the order of the execution of the hooks, you can use the Order parameter of the attribute (see http://www.specflow.org/documentation/Hooks/ ): [BeforeScenario(Order = 0)] .

You can also check my post at http://gasparnagy.com/2016/08/specflow-tips-customizing-dependency-injection-with-autofac/ that gives a more robust solution with more complex dependencies using Autofac.

Things don't just stop working over night.

I would look at what changed between yesterday and today. Did you update any of the packages used? Any changes related to your IOC? Your error message is pointing in that direction.

Try to roll back to what you had when it was still working and bring changes in one by one to see which caused the issue. Then you can take it from there.

I found solution here https://stackoverflow.com/a/26402692/10148657 . Basically I got rid of RegisterInstanceAs and wrapped IWebDriver in SeleniumContext class which can now be freely passed as injected dependency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM