Features

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.

See our NUnit example repository for a simple example on how to run NUnit tests on TestingBot.

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

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
  • 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 the NUnit website.
  • 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 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")]
    public class TbNUnit_Test
    {
        private IWebDriver driver;
        private string browser;
        private string version;
        private string os;

        public TbNUnit_Test(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"] = "api_key",
                ["secret"] = "api_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");
            Assert.That(driver.Title, Is.EqualTo("Google"));
            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();
                driver.Dispose();
            }
        }
    }
}

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.

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.

var chromeOptions = new ChromeOptions()
{
    BrowserVersion = "latest",
    PlatformName = "Windows 10"
};

var tbOptions = new Dictionary<string, string>
{
    ["key"] = "api_key",
    ["secret"] = "api_secret",
    ["name"] = TestContext.CurrentContext.Test.Name,
    ["selenium-version"] = "3.14.0"
};

Depending on the browser you want to test on, you might need to specify different browser options in your code:

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 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", "latest", "Windows 7", "", "")]
    public class TbNUnit_Test
    {
        [SetUp]
        public void Init()
        {
            var chromeOptions = new ChromeOptions()
            {
                BrowserVersion = "latest",
                PlatformName = "Windows 10"
            };

            var tbOptions = new Dictionary<string, string>
            {
                ["key"] = "api_key",
                ["secret"] = "api_secret",
                ["name"] = TestContext.CurrentContext.Test.Name,
                ["selenium-version"] = "3.14.0"
            };

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

            driver = new RemoteWebDriver(new Uri("http://localhost:4445/wd/hub"), chromeOptions.ToCapabilities(), TimeSpan.FromSeconds(600));
        }
    }
}

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.

NUnit provides a [assembly: Parallelizable(ParallelScope.Fixtures)] option to specify tests should run in parallel. More information is available in the NUnit Parallelizable documentation.

Queuing

Every plan we provide comes with a limited amount 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.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();
    }
}

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.