2

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.

Blue
  • 22,608
  • 7
  • 62
  • 92
Piers Gwynn
  • 43
  • 1
  • 5

3 Answers3

3

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.

Gaspar Nagy
  • 4,422
  • 30
  • 42
  • Hi Gaspar, thanks a lot for these links. Sadly we don't have another BeforeScenario hook set anywhere however we'll definitely take a look at setting the order of the hooks to see if this helps. Your second link also looks interesting - will read through and see if we can apply some changes that help us out. – Piers Gwynn Sep 02 '16 at 14:11
  • In this case it can happen that it is a bug (although I haven't seen such error, although I use this pattern). Which SpecFlow version produces this? If it would be possible to create a small repro, please post it to github. (The Autofac solution could still fix it.) – Gaspar Nagy Sep 05 '16 at 06:37
1

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.

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32
  • We've changed very little in the solution recently - only simple changes to things like button Ids in our UI tests etc. One weird thing is that occasionally we can try changing something - like removing and re-adding a reference and the error will go away, but will then reappear if we close and re-open the solution. I'll try rolling specflow, selenium, webdriver and nunit back a few versions to see if that helps, although all of these packages are official packages from nuget and it's all been installed as per the Specflow docs. Thanks – Piers Gwynn Aug 25 '16 at 08:40
1

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135