C# Automated Testing
With TestingBot you can easily run your automated tests with any C# test framework.
TestingBot has created a sample Visual Studio project to help you get started with C# and .NET Selenium testing.
Download ZIPYou can import this project into Visual Studio and run the tests directly.
To run your tests with C# and .NET, you need to have the .NET SDK installed on your machine.
-
On Windows:
- Download the .NET SDK installer and run it.
-
Or use the
winget
command:winget install Microsoft.DotNet.SDK.9
-
On MacOS:
brew install dotnet
-
On Linux:
sudo apt-get install dotnet-sdk-9.0
After installing the .NET SDK, you can create a new C# project using the command line:
dotnet new console -n SeleniumTest
Next, add the necessary Selenium WebDriver packages to your project:
cd SeleniumTest
dotnet add package Selenium.WebDriver
dotnet add package Selenium.Support
Selenium C# Example
The example below shows how to run a simple Selenium test using C# on TestingBot. It will instruct a remote browser to navigate to Google, mark the test as passed and then close the remote browser.
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("https://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 |
Make sure to always stop your test (driver.Quit()
), otherwise it will continue running, leading to a timeout.
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:
java -jar testingbot-tunnel.jar key secret
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 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://localhost:4445/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();
}
}
}
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.