Specflow Context Injection of IWebDriver failing











up vote
2
down vote

favorite
1












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.










share|improve this question




























    up vote
    2
    down vote

    favorite
    1












    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.










    share|improve this question


























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      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.










      share|improve this question















      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.







      c# selenium webdriver specflow






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 24 '16 at 15:09









      FrankerZ

      15.6k72859




      15.6k72859










      asked Aug 24 '16 at 8:41









      Piers Gwynn

      133




      133
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          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.






          share|improve this answer





















          • 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 2 '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 5 '16 at 6:37


















          up vote
          1
          down vote













          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.






          share|improve this answer





















          • 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 8:40


















          up vote
          0
          down vote













          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.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39118432%2fspecflow-context-injection-of-iwebdriver-failing%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            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.






            share|improve this answer





















            • 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 2 '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 5 '16 at 6:37















            up vote
            3
            down vote



            accepted










            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.






            share|improve this answer





















            • 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 2 '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 5 '16 at 6:37













            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 31 '16 at 9:37









            Gaspar Nagy

            3,7622436




            3,7622436












            • 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 2 '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 5 '16 at 6:37


















            • 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 2 '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 5 '16 at 6:37
















            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 2 '16 at 14:11




            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 2 '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 5 '16 at 6:37




            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 5 '16 at 6:37












            up vote
            1
            down vote













            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.






            share|improve this answer





















            • 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 8:40















            up vote
            1
            down vote













            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.






            share|improve this answer





















            • 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 8:40













            up vote
            1
            down vote










            up vote
            1
            down vote









            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 24 '16 at 8:53









            Andrei Dragotoniu

            3,23211118




            3,23211118












            • 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 8:40


















            • 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 8:40
















            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 8:40




            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 8:40










            up vote
            0
            down vote













            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.






            share|improve this answer



























              up vote
              0
              down vote













              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.






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                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.






                share|improve this answer














                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 8 at 0:47









                Stephen Rauch

                27.4k153156




                27.4k153156










                answered Nov 8 at 0:27









                Dariusz Kijania

                12




                12






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39118432%2fspecflow-context-injection-of-iwebdriver-failing%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()