Skip to content

Instantly share code, notes, and snippets.

@nadvolod
Created May 19, 2020 22:17
Show Gist options
  • Save nadvolod/012dab256c0a848b4de92de89ad56227 to your computer and use it in GitHub Desktop.
Save nadvolod/012dab256c0a848b4de92de89ad56227 to your computer and use it in GitHub Desktop.
How to parallelize with NUnit at the method level
[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