C# Automated Testing
With TestingBot you can easily run your automated tests with any C# test framework.
Download Selenium WebDriver (selenium-dotnet-x.x.x.zip) and include it in your project library.
Or use the Selenium.WebDriver
NuGet package to download the .NET bindings for Selenium WebDriver.
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace SeleniumTest {
class Program {
static void Main(string[] args) {
IWebDriver driver;
DesiredCapabilities capability = DesiredCapabilities.Firefox();
capability.SetCapability("key", "key");
capability.SetCapability("secret", "secret");
capability.SetCapability("version", "latest-1");
driver = new RemoteWebDriver(
new Uri("https://hub.testingbot.com/wd/hub/"), capability
);
driver.Navigate().GoToUrl("https://www.google.com/ncr");
Console.WriteLine(driver.Title);
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("TestingBot");
query.Submit();
Console.WriteLine(driver.Title);
driver.Quit();
}
}
}
This test will start a Firefox browser on Windows in our cloud, go to Google, and search for TestingBot. It will then output the title.
This test does not make any assertions or verifications, but it's a quick intro in how to run a C# test on TestingBot.
Make sure to always stop your test (
driver.Quit()
), otherwise it will continue running, leading to a timeout.
Selenium 4 Example
Since Selenium 3, the usage of Desired Capabilities is deprecated. The example below illustrates how to set the capabilities for C# using Selenium 3 and 4:
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System;
namespace SeleniumNunit
{
[TestFixture]
public class ExampleTest
{
IWebDriver Driver;
[Test]
public void SampleTest()
{
ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability(CapabilityType.Version, "latest", true);
options.AddAdditionalCapability(CapabilityType.Platform, "WIN10", true);
options.AddAdditionalCapability("key", "key", true);
options.AddAdditionalCapability("secret", "secret", true);
options.AddAdditionalCapability("name", TestContext.CurrentContext.Test.Name, true);
Driver = new RemoteWebDriver(new Uri("http://hub.testingbot.com/wd/hub"), options.ToCapabilities(),
TimeSpan.FromSeconds(600));
Driver.Navigate().GoToUrl("https://www.google.com");
Assert.Pass();
}
[TearDown]
public void CleanUpAfterEveryTestMethod()
{
var passed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Passed;
((IJavaScriptExecutor)Driver).ExecuteScript("tb:test-result=" + (passed ? "passed" : "failed"));
if(Driver != null)
Driver.Quit();
}
}
}
Depending on the browser you want to test on, you may want to use a different DriverOptions class:
browserName | className |
---|---|
Safari | OpenQA.Selenium.Safari.SafariOptions |
Chrome | OpenQA.Selenium.Chrome.ChromeOptions |
Firefox | OpenQA.Selenium.Firefox.FirefoxOptions |
IE | OpenQA.Selenium.IE.InternetExplorerOptions |
Edge | OpenQA.Selenium.Edge.EdgeOptions |
Opera | OpenQA.Selenium.Opera.OperaOptions |
Specify Browsers & Devices
To let TestingBot know on which browser/platform/device you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.
Testing Internal Websites
We've built TestingBot Tunnel, to provide you with a secure way to run tests against your staged/internal webapps.
Please see our TestingBot Tunnel documentation for more information about this easy to use tunneling solution.
The example below shows how to easily run a C# test with our Tunnel:
1. Download our tunnel and start the tunnel:
2. Adjust your test: instead of pointing to 'hub.testingbot.com/wd/hub'
like the example above - change it to point to your tunnel's IP address.
Assuming you run the tunnel on the same machine you run your tests, change to 'localhost:4445/wd/hub'
. localhost is the machine running the tunnel, 4445 is the default port of the tunnel.
This way your test will go securely through the tunnel to TestingBot and back:
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace SeleniumTest {
class Program {
static void Main(string[] args) {
IWebDriver driver;
DesiredCapabilities capability = DesiredCapabilities.Firefox();
capability.SetCapability("key", "key");
capability.SetCapability("secret", "secret");
capability.SetCapability("version", "latest-1");
driver = new RemoteWebDriver(
new Uri("http://localhost:4445/wd/hub/"), capability
);
driver.Navigate().GoToUrl("https://www.google.com/ncr");
Console.WriteLine(driver.Title);
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("TestingBot");
query.Submit();
Console.WriteLine(driver.Title);
driver.Quit();
}
}
}
Run tests in Parallel
Parallel Testing means running the same test, or multiple tests, simultaneously. This greatly reduces your total testing time.
You can run the same tests on all different browser configurations or run different tests all on the same browser configuration.
TestingBot has a large grid of machines and browsers, which means you can use our service to do efficient parallel testing. It is one of the key features we provide to greatly cut down on your total testing time.
Please see our PNUnit documentation for parallel testing.
Queuing
Every plan we provide comes with a limit of parallel tests.
If you exceed the number of parallel tests assigned to your account, TestingBot will queue the additional tests (for up to 6 minutes) and run the tests as soon as slots become available.
Mark tests as passed/failed
To see if a test passed or not in our member area, or to send additional meta-data to TestingBot, you can use our API.
Please see the example below on how to notify TestingBot about the test success state:
[TearDown]
public void CleanUp()
{
bool passed = TestContext.CurrentContext.Result.Status == 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();
}
}
Other C# Framework examples
-
NUnit
An unit testing framework that is open source written in C#.
-
PNunit
With PNUnit you can run several tests in parallel.
-
SpecFlow
SpecFlow allows you to run Automated .NET tests using Cucumber-compatible Gherkin syntax.
-
MSTest
MSTest framework is a test framework which is included, by default, with Microsoft Visual Studio.
-
MbUnit
MbUnit is a generative test unit framework, built for C sharp testing.