Skip to content

Commit e267aa3

Browse files
update test + add parallel example
1 parent 03b8b57 commit e267aa3

File tree

3 files changed

+140
-16
lines changed

3 files changed

+140
-16
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using OpenQA.Selenium;
2+
using OpenQA.Selenium.Chrome;
3+
using OpenQA.Selenium.Firefox;
4+
using OpenQA.Selenium.Remote;
5+
using OpenQA.Selenium.Safari;
6+
7+
namespace NUnit_TestingBot_Sample;
8+
9+
public class ParallelTests
10+
{
11+
[TestFixture("chrome", "latest", "Windows 10")]
12+
[TestFixture("firefox", "latest", "SONOMA")]
13+
[Parallelizable(ParallelScope.Fixtures)]
14+
public class TbNUnit_ParallelTest
15+
{
16+
private IWebDriver driver;
17+
private string browser;
18+
private string version;
19+
private string os;
20+
21+
public TbNUnit_ParallelTest(String browser, String version, String os)
22+
{
23+
this.browser = browser;
24+
this.version = version;
25+
this.os = os;
26+
}
27+
28+
[SetUp]
29+
public void Init()
30+
{
31+
DriverOptions browserOptions;
32+
if (this.browser == "firefox")
33+
{
34+
browserOptions = new FirefoxOptions()
35+
{
36+
BrowserVersion = this.version,
37+
PlatformName = this.os
38+
};
39+
}
40+
else if (this.browser == "safari")
41+
{
42+
browserOptions = new SafariOptions()
43+
{
44+
BrowserVersion = this.version,
45+
PlatformName = this.os
46+
};
47+
}
48+
else
49+
{
50+
browserOptions = new ChromeOptions()
51+
{
52+
BrowserVersion = this.version,
53+
PlatformName = this.os
54+
};
55+
}
56+
57+
var tbOptions = new Dictionary<string, string>
58+
{
59+
["key"] = Environment.GetEnvironmentVariable("TESTINGBOT_KEY"),
60+
["secret"] = Environment.GetEnvironmentVariable("TESTINGBOT_SECRET"),
61+
["name"] = TestContext.CurrentContext.Test.Name,
62+
["selenium-version"] = "3.14.0"
63+
};
64+
65+
browserOptions.AddAdditionalOption("tb:options", tbOptions);
66+
67+
driver = new RemoteWebDriver(new Uri("https://hub.testingbot.com/wd/hub"), browserOptions.ToCapabilities(), TimeSpan.FromSeconds(600));
68+
}
69+
70+
[Test]
71+
public void GoogleTest()
72+
{
73+
driver.Navigate().GoToUrl("http://www.google.com");
74+
StringAssert.Contains("Google", driver.Title);
75+
IWebElement query = driver.FindElement(By.Name("q"));
76+
query.SendKeys("TestingBot");
77+
query.Submit();
78+
}
79+
80+
[TearDown]
81+
public void CleanUp()
82+
{
83+
bool passed = TestContext.CurrentContext.Result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Passed;
84+
try
85+
{
86+
// Logs the result to TestingBot
87+
((IJavaScriptExecutor)driver).ExecuteScript("tb:test-result=" + (passed ? "passed" : "failed"));
88+
}
89+
finally
90+
{
91+
// Terminates the remote webdriver session
92+
driver.Quit();
93+
}
94+
}
95+
}
96+
}

NUnit-TestingBot-Sample/SampleTest.cs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
11
using OpenQA.Selenium;
22
using OpenQA.Selenium.Chrome;
3+
using OpenQA.Selenium.Firefox;
34
using OpenQA.Selenium.Remote;
5+
using OpenQA.Selenium.Safari;
46

57
namespace NUnit_TestingBot_Sample;
68

79
public class Tests
810
{
9-
[TestFixture("chrome", "latest", "Windows 10", "", "")]
11+
[TestFixture("chrome", "latest", "Windows 10")]
1012
public class TbNUnit_Test
1113
{
1214
private IWebDriver driver;
13-
private String browser;
14-
private String version;
15-
private String os;
16-
private String deviceName;
17-
private String deviceOrientation;
15+
private string browser;
16+
private string version;
17+
private string os;
1818

19-
public TbNUnit_Test(String browser, String version, String os, String deviceName, String deviceOrientation)
19+
public TbNUnit_Test(String browser, String version, String os)
2020
{
2121
this.browser = browser;
2222
this.version = version;
2323
this.os = os;
24-
this.deviceName = deviceName;
25-
this.deviceOrientation = deviceOrientation;
2624
}
2725

2826
[SetUp]
2927
public void Init()
3028
{
31-
var chromeOptions = new ChromeOptions()
29+
DriverOptions browserOptions;
30+
if (this.browser == "firefox")
3231
{
33-
BrowserVersion = "latest",
34-
PlatformName = "Windows 10"
35-
};
32+
browserOptions = new FirefoxOptions()
33+
{
34+
BrowserVersion = this.version,
35+
PlatformName = this.os
36+
};
37+
}
38+
else if (this.browser == "safari")
39+
{
40+
browserOptions = new SafariOptions()
41+
{
42+
BrowserVersion = this.version,
43+
PlatformName = this.os
44+
};
45+
}
46+
else
47+
{
48+
browserOptions = new ChromeOptions()
49+
{
50+
BrowserVersion = this.version,
51+
PlatformName = this.os
52+
};
53+
}
54+
3655
var tbOptions = new Dictionary<string, string>
3756
{
3857
["key"] = Environment.GetEnvironmentVariable("TESTINGBOT_KEY"),
@@ -41,13 +60,13 @@ public void Init()
4160
["selenium-version"] = "3.14.0"
4261
};
4362

44-
chromeOptions.AddAdditionalOption("tb:options", tbOptions);
63+
browserOptions.AddAdditionalOption("tb:options", tbOptions);
4564

46-
driver = new RemoteWebDriver(new Uri("https://hub.testingbot.com/wd/hub"), chromeOptions.ToCapabilities(), TimeSpan.FromSeconds(600));
65+
driver = new RemoteWebDriver(new Uri("https://hub.testingbot.com/wd/hub"), browserOptions.ToCapabilities(), TimeSpan.FromSeconds(600));
4766
}
4867

4968
[Test]
50-
public void googleTest()
69+
public void GoogleTest()
5170
{
5271
driver.Navigate().GoToUrl("http://www.google.com");
5372
StringAssert.Contains("Google", driver.Title);

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Tests](https://github.com/testingbot/nunit-example/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/testingbot/nunit-example/actions/workflows/test.yml)
2+
13
## TestingBot - NUnit
24

35
TestingBot provides an online grid of browsers and mobile devices to run Automated tests on via Selenium WebDriver.
@@ -24,6 +26,13 @@ This example demonstrates how to use NUnit to run a test in parallel across seve
2426
### Running your tests from Test Explorer via NUnit Test Adapter
2527
Click Run Unit Tests, you will see the test result in the [TestingBot Dashboard](https://testingbot.com/members/)
2628
29+
### Parallel testing
30+
There's an example on how to perform parallel testing. This will run two or more tests simultaneously, shortening the total test duration.
31+
To run the Parallel test, please use this command:
32+
```
33+
dotnet test --filter "Parallel"
34+
```
35+
2736
### Resources
2837
##### [TestingBot Documentation](https://testingbot.com/support/)
2938

0 commit comments

Comments
 (0)