C# Automated Testing with NUnit

NUnit is a unit-test framework designed for all the .NET languages. It is written in C# and built to use many of the .NET language features. For example: it allows you to use custom attributes and other reflection-related capabilities. Please visit the official NUnit website for more documentation about the framework.

Prerequisites

To get started, you need to download and install these components:

Installing Selenium WebDriver

Follow the steps below, or download the Selenium driver files for C# from the official Selenium HQ website.
You'll also find additional documentation about working with Selenium and C# on the Selenium HQ website.

  • Create a new project in Visual Studio.
  • In the Visual Studio Tools menu, go to Library Package Manager > Manage Nuget Package for Solution.
  • This will open the Manage NuGet Packages dialog. Click Online, then Next.
  • In the Search Packages field, enter Selenium and click Search.
  • Select Selenium WebDriver from the search results and click Install.

Installing DLL files for Selenium and .NET

Follow the steps below, or download the Selenium driver files for C# from the official Selenium HQ website.
You'll also find additional documentation about working with Selenium and C# on the Selenium HQ website.


Once the Visual Studio project is created, you need to make sure it references the required Selenium DLLs for .NET.

  • Download the Selenium DLL files for .NET from http://selenium-release.storage.googleapis.com/index.html?path=2.53/
  • In the Solutions Explorer, select the project and right-click for References.
  • Click Add Reference. Click Browse and navigate to the net40 folder of the directory where you downloaded the Selenium .NET DLLs.
  • Add the WebDriver.Support.dll reference to your Visual Studio Project.

Installing NUnit

To install NUnit, please follow these steps:

  • Download NUnit from http://www.nunit.org/index.php?p=download.
  • Click Add Reference. Click Browse and navigate to the directory where you downloaded NUnit.
  • Add the nunit.framework.dll and pnunit.framework.dll references to your project.

Code Example


using NUnit.Framework;
using System;
using Selenium;
using System.Web;
using System.Text;
using System.Net;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
 
namespace TBExample {
    [TestFixture("chrome", "53", "Windows 7", "", "")]
    public class TbNUnit_Test
    {
        private IWebDriver driver;
        private String browser;
        private String version;
        private String os;
        private String deviceName;
        private String deviceOrientation;
 
        public TbNUnit_Test(String browser, String version, String os, String deviceName, String deviceOrientation)
        {
            this.browser = browser;
            this.version = version;
            this.os = os;
            this.deviceName = deviceName;
            this.deviceOrientation = deviceOrientation;
        }
 
        [SetUp]
        public void Init()
        {
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.SetCapability(CapabilityType.BrowserName, browser);
            caps.SetCapability(CapabilityType.Version, version);
            caps.SetCapability(CapabilityType.Platform, os);
            caps.SetCapability("deviceName", deviceName);
            caps.SetCapability("deviceOrientation", deviceOrientation);
            caps.SetCapability("username", "api_key");
            caps.SetCapability("accessKey", "api_secret");
            caps.SetCapability("name", TestContext.CurrentContext.Test.Name);
 
            driver = new RemoteWebDriver(new Uri("https://hub.testingbot.com:80/wd/hub"), caps, 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.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();
            }
        }
    }
}

Installing the NUnit Test Adapter and Running the Test

  • In VisualStudio, select Tools > Extension Manager.
  • Find the NUnit Test Adapter in the center panel and select it.
  • Click Download and follow the wizard.
  • Now go to Test > Windows > Test Explorer to find the NUnit Test Adapter.
  • Make sure you see the Test Explorer tab on the left side of your project.

Configuring capabilities

To let TestingBot know on which browser/platform you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.


IWebDriver driver;
DesiredCapabilities desiredCap = DesiredCapabilities.Firefox();
desiredCap.SetCapability("key", "key");
desiredCap.SetCapability("secret", "secret"

driver = new RemoteWebDriver(
  new Uri("https://hub.testingbot.com/wd/hub/"), desiredCap
);

To see how to do this please select a combination of browser version and platform in the drop-down menus below


1. Select a Platform
2. Select a Browser


Testing Internal/Staged 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 Java WebDriver 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 securily through the tunnel to TestingBot and back:


using NUnit.Framework;
using System;
using Selenium;
using System.Web;
using System.Text;
using System.Net;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
 
namespace TBExample {
    [TestFixture("chrome", "53", "Windows 7", "", "")]
    public class TbNUnit_Test
    {
        [SetUp]
        public void Init()
        {
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.SetCapability(CapabilityType.BrowserName, browser);
            caps.SetCapability(CapabilityType.Version, version);
            caps.SetCapability(CapabilityType.Platform, os);
            caps.SetCapability("name", TestContext.CurrentContext.Test.Name);
 
            driver = new RemoteWebDriver(new Uri("http://localhost:4445/wd/hub"), caps, TimeSpan.FromSeconds(600))
        }
    }
}

Other Options

We offer many other test options, for example: disable video recording, specifying a custom Firefox Profile, loading Chrome/Firefox/Safari extensions, running an executable before your test starts, uploading files, ...
See our list of test options for a full list of options to customize your tests.

Send test details back to TestingBot

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();
    }
}

Parallel Testing

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.


Queueing

Every plan we provide comes with a limit of concurrent VMs (how many tests you can run in parallel).
For example: if you have a plan with 5 concurrent VMs, it is possible to start more tests. TestingBot will queue the additional tests and run the tests as soon as slots become available.

Pick a C# Test Framework

  • 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.