Here is a post that is long over due, and is finally getting published by request!
Concurrent WebDrivers
A WebDriver is not thread safe, but it is not required to be a singleton either. If you instantiate multiple drivers, they can all be run at the same time. If you cast a series of drivers into a collection of the IWebDriver interface, and then throw that into a Parallel ForEach, you have yourself one fun toy to play with!
So what are some uses of this?
Admittedly it is not a particularly common or practical use case to have multiple automated browser sessions running at the same time, but it can still come in handy. One fun application is to test website concurrency. You can do a minor load test of a webpage, or your can have two sessions trying to race each other for a limited resource.
...also, running WebDrivers in parallel can make for a killer demo!
Sample Code (xUnit Test)
public class ParallelDemo : IDisposable
{
public IList<IWebDriver> Drivers { get; private set; }
public ParallelDemo()
{
Drivers = new List<IWebDriver>
{
new InternetExplorerDriver(),
new FirefoxDriver(),
new ChromeDriver()
};
}
public void Dispose()
{
foreach (var driver in Drivers)
{
driver.Close();
driver.Dispose();
}
}
[Fact]
public void ParallelSearch()
{
Parallel.ForEach(Drivers, SearchForTom);
}
private static void SearchForTom(IWebDriver driver)
{
driver.Url = "https://www.google.com/#q=tom+dupont";
var firstResult = driver.FindElement(By.CssSelector("h3 > a.l"));
Assert.Contains("Tom DuPont .NET", firstResult.Text);
}
}
Enjoy,
Tom