Skip to content

Instantly share code, notes, and snippets.

@nakov
Last active February 8, 2021 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nakov/4db8afa21894fe8dd9e73944c66e7bcc to your computer and use it in GitHub Desktop.
Save nakov/4db8afa21894fe8dd9e73944c66e7bcc to your computer and use it in GitHub Desktop.
Run Selenium tests in parallel with NUnit
[TestFixture(typeof(FirefoxOptions))]
[TestFixture(typeof(ChromeOptions))]
[Parallelizable(ParallelScope.All)]
public class MultiSeleniumTest<TOptions> where TOptions : DriverOptions, new()
{
[ThreadStatic]
private static IWebDriver driver;
[SetUp]
public void Setup()
{
var options = new TOptions();
driver = new RemoteWebDriver(
new Uri("http://localhost:4444/wd/hub"), options);
}
[Test]
public void Test_NakovCom_Title()
{
driver.Navigate().GoToUrl("https://nakov.com");
Assert.That(driver.Title.Contains("Svetlin Nakov"));
}
[Test]
public void Test_Wikipedia_Title()
{
driver.Navigate().GoToUrl("https://wikipedia.org");
Assert.That(driver.Title.Contains("Wikipedia"));
}
[Test]
public void Test_Google_Title()
{
driver.Navigate().GoToUrl("https://google.com");
Assert.That(driver.Title.Contains("Google"));
}
[TearDown]
public void Shutdown()
{
driver.Quit();
}
}
@nakov
Copy link
Author

nakov commented Feb 8, 2021

This example will run 6 Web browsers in parallel to execute 6 tests concurrently:

  • 3 Chrome instances
  • 3 Firefox instances

Create a separate IWebDriver instance before each test in [SetUp] and release it in [TearDown].

Define the driver as [ThreadStatic] + static to get a separate instance for each test execution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment