0

I'm having an issue trying to do as the title says. When i run this locally, it does launch 2 chrome instances, however it uses only one of the browsers for both tests, as opposed to using each browser for each test. Any ideas how how to set this up correctly?

public class BaseClass
{
    public IWebDriver driver; 

    [SetUp]
    public void BaseSetUp() 
    {
        driver = new ChromeDriver();
        driver.Manage().Window.Maximize();
    } 

    [TearDown]
    public void BaseTearDown() 
    {
        driver.Quit();
    }
}
[Parallelizable(ParallelScope.All)]
[TestFixture]

public class DerivedClass : BaseClass
{
    [TestCase("https://www.python.org/", "Welcome to Python.org")]
    [TestCase("https://www.w3.org/", "World Wide Web Consortium (W3C)")]
    public void Test3(string url, string title)
    {
        driver.Navigate().GoToUrl(url);
        Thread.Sleep(4500);
        Assert.AreEqual(driver.Title, title);
    }
}

1 Answer 1

1

You are indeed creating the driver twice, but you are storing both instances in the same member field, driver. The reason this happens is that NUnit uses the same instance of a test class for all the tests within that class. (A new feature will be available in a future release to use a separate instance, but that's no help to you right now.)

As the tests run in parallel, the first test to run performs its setup and stores the driver in that field. Then the second test starts and stores it's instance in the same field. It's not possible to predict exactly when - during the execution of the tests- that replacement will take place. However, it will most likely happen consistently as you re-run on the same machine.

In the case of the example, the solution is simple. If you want each test case to use a separate driver instance, create the driver within the test itself, perhaps by calling a method that centralizes all your initialization.

An important thing to remember is that the ParallelizableAttribute is used to tell NUnit that the test may be run in parallel. NUnit accepts that as a promise, but if you share state between tests they won't run correctly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.