How to parallelize with NUnit at the method level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
[Parallelizable(ParallelScope.All)] | |
class ParallelAtMethodsWithSelenium | |
{ | |
private sealed class TestScope : IDisposable | |
{ | |
public IWebDriver Driver { get; } | |
private string SauceUserName { get; } | |
private string SauceAccessKey { get; } | |
private Dictionary<string, object> SauceOptions { get; } | |
public TestScope() | |
{ | |
var chromeOptions = new ChromeOptions | |
{ | |
BrowserVersion = "latest", | |
PlatformName = "Windows 10", | |
UseSpecCompliantProtocol = true | |
}; | |
//TODO please supply your Sauce Labs user name in an environment variable | |
SauceUserName = Environment.GetEnvironmentVariable("SAUCE_USERNAME", EnvironmentVariableTarget.User); | |
//TODO please supply your own Sauce Labs access Key in an environment variable | |
SauceAccessKey = Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY", EnvironmentVariableTarget.User); | |
SauceOptions = new Dictionary<string, object> | |
{ | |
["username"] = SauceUserName, | |
["accessKey"] = SauceAccessKey | |
}; | |
SauceOptions.Add("name", TestContext.CurrentContext.Test.Name); | |
chromeOptions.AddAdditionalCapability("sauce:options", SauceOptions, true); | |
Driver = new RemoteWebDriver(new Uri("https://ondemand.saucelabs.com/wd/hub"), | |
chromeOptions.ToCapabilities(), TimeSpan.FromSeconds(600)); | |
} | |
public void Dispose() | |
{ | |
//clean-up code goes here | |
if (Driver == null) | |
return; | |
var passed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Passed; | |
((IJavaScriptExecutor)Driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed")); | |
Driver.Quit(); | |
} | |
} | |
[Test] | |
public void Test1() | |
{ | |
using (var scope = new TestScope()) | |
{ | |
scope.Driver.Navigate().GoToUrl("https://www.saucedemo.com"); | |
Assert.AreEqual("Swag Labs", scope.Driver.Title); | |
} | |
} | |
[Test] | |
public void Test2() | |
{ | |
using (var scope = new TestScope()) | |
{ | |
scope.Driver.Navigate().GoToUrl("https://www.saucedemo.com"); | |
Assert.AreEqual("Swag Labs", scope.Driver.Title); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment