I am running Selenium tests in parallel using C# and NUnit. The way in which the test currently run is up to 4 tests can be run in parallel (possibly 1 or 2 more depending on how many cores the system running it has), and the remaining tests are queued. Once one test is complete, the queued test then run. Currently, instead of opening a new browser and closing that browser after each individual test, the queued test takes hold of one of the open browsers. The problem with this is when I dispose of the driver after the test, it closes the browser and so each subsequent queued test fails.
Each test is within its own class, and each test class inherits from a SetupTeardown class.
My question is this: Is there a way I can modify the DriverFactory class or the inherited SetupTeardown class so those queued tests do not reuse the initial four tests browser instances? I really want each test to open its own browser on Setup and close on Teardown.
This is my driver class:
public class DriverFactory
{
private DriverFactory()
{
//Do nothing
}
private static DriverFactory instance = new DriverFactory();
public static DriverFactory getInstance()
{
return instance;
}
ThreadLocal<IWebDriver> driver = BuildThreadLocal();
private static ThreadLocal<IWebDriver> BuildThreadLocal()
{
return new ThreadLocal<IWebDriver>(() =>
{
return new ChromeDriver(PathToChromeDriver);
});
}
public IWebDriver getDriver()
{
return driver.Value;
}
public void removeDriver()
{
driver.Value.Dispose();
}
}
And this is my SetupTeardown class that each test class inherits from:
[TestFixture]
public class SetupTeardown
{
public string testname;
public IWebDriver driver;
[OneTimeSetUp]
public static void AssemblyInitialize()
{
//Code to initialize test reporting
}
[SetUp]
public void Setup()
{
driver = DriverFactory.getInstance().getDriver();
driver.Navigate().GoToUrl(url);
}
[TearDown]
public void TearDown()
{
var status = TestContext.CurrentContext.Result.Outcome.Status;
if (status == TestStatus.Failed)
{
//Code to add failure status to reporting
}
else
{
//Code to add pass status to reporting
}
}
[OneTimeTearDown]
public static void AssemblyCleanup()
{
//Code that aggregates test data run and places it in a report
}
And here is how my test class is set up:
class TestClass1
{
[Parallelizable]
public class _TestClass1 : SetupTeardown
{
[Test]
public void
{
//Test code
}
}
}