Parkyster
Parkyster

Reputation: 155

Setting up selenium webdriver for parallel execution

I am trying to execute a large suite of selenium tests via xUnit console runner in parallel.

These have executed and I see 3 chrome windows open, however the first send key commands simply executes 3 times to one window, resulting in test failure.

I have registered my driver in an objectcontainer before each scenario as below:

[Binding]
public class WebDriverSupport
{
    private readonly IObjectContainer _objectContainer;

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

    [BeforeScenario]
    public void InitializeWebDriver()
    {
        var driver = GetWebDriverFromAppConfig();                       
        _objectContainer.RegisterInstanceAs<IWebDriver>(driver);
    }

And then call the driver in my specflow step defintions as:

_driver =   (IWebDriver)ScenarioContext.Current.GetBindingInstance(typeof(IWebDriver));
            ScenarioContext.Current.Add("Driver", _driver);

However this has made no difference and it seems as if my tests are trying to execute all commands to one driver.

Can anyone advise where I have gone wrong ?

Upvotes: 1

Views: 1183

Answers (2)

Vincent Sels
Vincent Sels

Reputation: 2889

You shouldn't be using ScenarioContext.Current in a parallel execution context. If you're injecting the driver through _objectContainer.RegisterInstanceAs you will receive it through constructor injection in your steps class' constructor, like so:

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

More info:

In my opinion this is horribly messy.

Upvotes: 1

Sam Holder
Sam Holder

Reputation: 32936

This might not be an answer, but is too big for a comment.

why are you using the IObjectContainer if you are just getting it from the current scenario context and not injecting it via the DI mechanism? I would try this:

[Binding]
public class WebDriverSupport
{
    [BeforeScenario]
    public void InitializeWebDriver()
    {
        var driver = GetWebDriverFromAppConfig();                       
        ScenarioContext.Current.Add("Driver",driver);
    }
}

then in your steps:

_driver =   (IWebDriver)ScenarioContext.Current.Get("Driver");

As long as GetWebDriverFromAppConfig returns a new instance you should be ok...

Upvotes: 0

Related Questions