Search code examples
c#seleniumautomated-testsnunit-3.0

Is it possible to run TestCases (not Tests) in Parallel in Nunit 3?


I would like to run multiple test cases in a test in parallel. At the moment, when I do this, a browser per test case is opened but the tests seem to all try to run in the same browser. It then goes on to just open new browsers and keep testing sequentially. What am I doing wrong? Thanks in advance!

[TestFixture]
public class AuthorisationTests : BaseTests
{
    [Test, Parallelizable(scope: ParallelScope.Children)]
    [TestCase("AccountAdministration")]
    [TestCase("AdvancedConfiguration")]
    [TestCase("AdvancedEntry")]
    [TestCase("AdvancedReporting")]
    [TestCase("BasicConfiguration")]
    [TestCase("DataEntry")]
    [TestCase("Reporting")]
    [TestCase("Revenue")]
    [TestCase("SysAdmin")]
    public void logged_in_user_should_be_able_to_access_only_appropriate_pages(string userType)
    {

The code used to initialise the driver:

        [SetUp]
    public void BeforeEach()
    {
        new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig());
        driver = new ChromeDriver();

        _loginPage.GoToLoginPage();
        driver.Manage().Window.Maximize();
    }

Solution

  • I believe you need to setup the webdriver in such a way, that it would run each test on different thread. This is how you can do it:

    ThreadLocal<IWebDriver> driver = new ThreadLocal<IWebDriver>();
    

    Then all the commands where you access the driver object change a bit:

    driver.Value = new ChromeDriver();