6

I am using Specflow with NUnit and Selenium and want to share instance of driver across all tests. I can do do this up to feature level with FeatureContext but can't see anything for all tests. I am aware that this is probably not the right way to go but I want to know if there is a way.

Please help with example(s).

Thanks

Idothisallday
  • 311
  • 1
  • 3
  • 14

1 Answers1

19

There are a few ways to do this. Most are covered on this page

What I personally would probably do is define a SeleniumContext class and require this class in all my Step class constructors, then tell SpecFlow's IOC to use the same instance in every scenario:

First create the class to hold the selenium driver instance

public class SeleniumContext
{
     public SeleniumContext()
     {
          //create the selenium context
          WebDriver = new ...create the flavour of web driver you want
     }

     public IWebDriver WebDriver{get; private set;} 
}

then setup the IOC to return the same instance every time

[Binding]
public class BeforeAllTests
{
    private readonly IObjectContainer objectContainer;
    private static SeleniumContext seleniumContext ;

    public BeforeAllTests(IObjectContainer container)
    {
        this.objectContainer = container;
    }

    [BeforeTestRun]
    public static void RunBeforeAllTests()
    {
        seleniumContext = new SeleniumContext();
     }

    [BeforeScenario]
    public void RunBeforeScenario()
    {            
        objectContainer.RegisterInstanceAs<SeleniumContext>(seleniumContext );
    }
}

Then ensure your step classes always ask for the context in their constructors (you need to do this in every step class you have)

[Bindings]
public class MySteps
{
    private SeleniumContext seleniumContext;

    public MyClass(SeleniumContext seleniumContext)
    {
         //save the context so you can use it in your tests
         this.seleniumContext = seleniumContext;
    }

    //then just use the seleniumContext.WebDriver in your tests
}

alternatively if you are already storing the instance in the feature context then you can just use the BeforeFeature hook to save the same instance:

[Binding]
public class BeforeAllTests
{
    private static WebDriver webDriver;

    [BeforeTestRun]
    public static void RunBeforeAllTests()
    {
        webDriver = new WebDriver();
     }

    [BeforeFeature]
    public static void RunBeforeFeature()
    {
        FeatureContext["WebDriver"] = webDriver;
     }

}
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • Brilliant thanks..didn't think of using IOC. I couldn't make BeforeAllTest work properly. #Sam Holder is there any other way you can think of? – Idothisallday Oct 18 '14 at 19:31
  • I couldn't share Webdriver instance when running this for all tests(between features). I could save and share instance within feature using FeatureContext and similarly use ScenarioContext for scenarios, not sure what to use between feature files? – Idothisallday Oct 18 '14 at 19:50
  • have you put a breakpoint in the `[BeforeTestRun]` method? Is this hit more than once? What test runner are you using? – Sam Holder Oct 18 '14 at 20:04
  • and which technique above did you use? the first one with the `SeleniumContext` or the second one with the `FeatureContext`? – Sam Holder Oct 18 '14 at 20:07
  • I am using unit with resharper. I can use FeatureContext.Current to save the instance of webdriver and reuse that instance for all scenario within a feature but how do I make this accessible for all features. As this state is lost when we move from feature to another – Idothisallday Oct 18 '14 at 20:31
  • Where are you creating the Web driver instance? As long as you create it in the BeforeTestRun method and the Web driver instance is placed in a static field then it should be the same in every test regardless of feature. Please update your question with the code you are using. – Sam Holder Oct 18 '14 at 21:03
  • 1
    In the BeforeAllTests method declaration, you declare a parameter IObjectContainer container, but you don't seem to use that, using objectContainer instead. Was this intended? – Sakamoto Kazuma Dec 04 '18 at 16:04