Skip to content

Commit

Permalink
update test + add parallel example
Browse files Browse the repository at this point in the history
  • Loading branch information
jochen-testingbot committed Mar 6, 2024
1 parent 03b8b57 commit e267aa3
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 16 deletions.
96 changes: 96 additions & 0 deletions NUnit-TestingBot-Sample/ParallelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Safari;

namespace NUnit_TestingBot_Sample;

public class ParallelTests
{
[TestFixture("chrome", "latest", "Windows 10")]
[TestFixture("firefox", "latest", "SONOMA")]
[Parallelizable(ParallelScope.Fixtures)]
public class TbNUnit_ParallelTest
{
private IWebDriver driver;
private string browser;
private string version;
private string os;

public TbNUnit_ParallelTest(String browser, String version, String os)
{
this.browser = browser;
this.version = version;
this.os = os;
}

[SetUp]
public void Init()
{
DriverOptions browserOptions;
if (this.browser == "firefox")
{
browserOptions = new FirefoxOptions()
{
BrowserVersion = this.version,
PlatformName = this.os
};
}
else if (this.browser == "safari")
{
browserOptions = new SafariOptions()
{
BrowserVersion = this.version,
PlatformName = this.os
};
}
else
{
browserOptions = new ChromeOptions()
{
BrowserVersion = this.version,
PlatformName = this.os
};
}

var tbOptions = new Dictionary<string, string>
{
["key"] = Environment.GetEnvironmentVariable("TESTINGBOT_KEY"),
["secret"] = Environment.GetEnvironmentVariable("TESTINGBOT_SECRET"),
["name"] = TestContext.CurrentContext.Test.Name,
["selenium-version"] = "3.14.0"
};

browserOptions.AddAdditionalOption("tb:options", tbOptions);

driver = new RemoteWebDriver(new Uri("https://hub.testingbot.com/wd/hub"), browserOptions.ToCapabilities(), TimeSpan.FromSeconds(600));
}

[Test]
public void GoogleTest()
{
driver.Navigate().GoToUrl("http://www.google.com");
StringAssert.Contains("Google", driver.Title);
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("TestingBot");
query.Submit();
}

[TearDown]
public void CleanUp()
{
bool passed = TestContext.CurrentContext.Result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Passed;
try
{
// Logs the result to TestingBot
((IJavaScriptExecutor)driver).ExecuteScript("tb:test-result=" + (passed ? "passed" : "failed"));
}
finally
{
// Terminates the remote webdriver session
driver.Quit();
}
}
}
}
51 changes: 35 additions & 16 deletions NUnit-TestingBot-Sample/SampleTest.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Safari;

namespace NUnit_TestingBot_Sample;

public class Tests
{
[TestFixture("chrome", "latest", "Windows 10", "", "")]
[TestFixture("chrome", "latest", "Windows 10")]
public class TbNUnit_Test
{
private IWebDriver driver;
private String browser;
private String version;
private String os;
private String deviceName;
private String deviceOrientation;
private string browser;
private string version;
private string os;

public TbNUnit_Test(String browser, String version, String os, String deviceName, String deviceOrientation)
public TbNUnit_Test(String browser, String version, String os)
{
this.browser = browser;
this.version = version;
this.os = os;
this.deviceName = deviceName;
this.deviceOrientation = deviceOrientation;
}

[SetUp]
public void Init()
{
var chromeOptions = new ChromeOptions()
DriverOptions browserOptions;
if (this.browser == "firefox")
{
BrowserVersion = "latest",
PlatformName = "Windows 10"
};
browserOptions = new FirefoxOptions()
{
BrowserVersion = this.version,
PlatformName = this.os
};
}
else if (this.browser == "safari")
{
browserOptions = new SafariOptions()
{
BrowserVersion = this.version,
PlatformName = this.os
};
}
else
{
browserOptions = new ChromeOptions()
{
BrowserVersion = this.version,
PlatformName = this.os
};
}

var tbOptions = new Dictionary<string, string>
{
["key"] = Environment.GetEnvironmentVariable("TESTINGBOT_KEY"),
Expand All @@ -41,13 +60,13 @@ public void Init()
["selenium-version"] = "3.14.0"
};

chromeOptions.AddAdditionalOption("tb:options", tbOptions);
browserOptions.AddAdditionalOption("tb:options", tbOptions);

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

[Test]
public void googleTest()
public void GoogleTest()
{
driver.Navigate().GoToUrl("http://www.google.com");
StringAssert.Contains("Google", driver.Title);
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![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)

## TestingBot - NUnit

TestingBot provides an online grid of browsers and mobile devices to run Automated tests on via Selenium WebDriver.
Expand All @@ -24,6 +26,13 @@ This example demonstrates how to use NUnit to run a test in parallel across seve
### Running your tests from Test Explorer via NUnit Test Adapter
Click Run Unit Tests, you will see the test result in the [TestingBot Dashboard](https://testingbot.com/members/)

### Parallel testing
There's an example on how to perform parallel testing. This will run two or more tests simultaneously, shortening the total test duration.
To run the Parallel test, please use this command:
```
dotnet test --filter "Parallel"
```

### Resources
##### [TestingBot Documentation](https://testingbot.com/support/)

Expand Down

0 comments on commit e267aa3

Please sign in to comment.