From e267aa3db1d94ef446ad1110c98540f98aaab575 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 6 Mar 2024 11:48:12 +0100 Subject: [PATCH] update test + add parallel example --- NUnit-TestingBot-Sample/ParallelTests.cs | 96 ++++++++++++++++++++++++ NUnit-TestingBot-Sample/SampleTest.cs | 51 +++++++++---- README.md | 9 +++ 3 files changed, 140 insertions(+), 16 deletions(-) create mode 100644 NUnit-TestingBot-Sample/ParallelTests.cs diff --git a/NUnit-TestingBot-Sample/ParallelTests.cs b/NUnit-TestingBot-Sample/ParallelTests.cs new file mode 100644 index 0000000..45dd084 --- /dev/null +++ b/NUnit-TestingBot-Sample/ParallelTests.cs @@ -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 + { + ["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(); + } + } + } +} diff --git a/NUnit-TestingBot-Sample/SampleTest.cs b/NUnit-TestingBot-Sample/SampleTest.cs index 297e5e9..16fc473 100644 --- a/NUnit-TestingBot-Sample/SampleTest.cs +++ b/NUnit-TestingBot-Sample/SampleTest.cs @@ -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 { ["key"] = Environment.GetEnvironmentVariable("TESTINGBOT_KEY"), @@ -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); diff --git a/README.md b/README.md index 6962a17..13afc8c 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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/)