Search code examples
seleniumwebdriverbddspecflow

SpecFlow + Selenium-WebDriver: Sharing a WebDriver Across Steps?


Say I have a scenario that uses steps that are contained in two different classes. Is there a way for both of them to have a handle to the same IWebDriver instance without going through ScenarioContext.Current["webdriverVariableName"]?

That's so ugly. Is there a cleaner way?

I was going to make a class with a public static IWebDriver property that gets assigned at the start of every scenario, so that all my steps could refer to it, but I don't think that will work when I start to run them in parallel, as each scenario would overwrite the global driver.


Solution

  • Specflow offers a Dependency Injection mecanism, so you could get your web driver instance injected in your steps.

    See https://github.com/techtalk/SpecFlow/wiki/Context-Injection

    See the "Avanced options" section.