Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Run C# Selenium Tests With NUnit and Taurus

DZone's Guide to

How to Run C# Selenium Tests With NUnit and Taurus

We take a look at Taurus, an open source framework for performing continuous testing, especially as it pertains to running NUnit and Selenium tests done in C#!

· DevOps Zone ·
Free Resource

Download the blueprint that can take a company of any maturity level all the way up to enterprise-scale continuous delivery using a combination of Automic Release Automation, Automic’s 20+ years of business automation experience, and the proven tools and practices the company is already leveraging.

Are you a .NET developer who wants to utilize Open Source tools for continuous testing? No problem! Now, Taurus, an automation-friendly open source framework for Continuous Testing, can run NUnit C# scripts. Selenium C# users can now utilize the advantages of continuous testing and continuous integration.

This is possible now that Taurus 1.9.5 supports NUnit, through a dedicated executor. With NUnit (a unit-testing framework for all .Net languages) and Mono (an open source implementation of Microsoft's .NET Framework), orchestrated by Taurus, you can now easily build and execute C# Selenium scripts. Nuget and XBuild (Mono) compile the script into binaries, and then these binaries are run by Taurus through Mono. This whole process can be run through one YAML file! Then, you can integrate the test with Jenkins or another CI tool.

This blog post will show how to run a C# script through Taurus. The complete example is available at the Taurus GitHub Repository.

Here is the complete YAML file:

---
services:
- module: shellexec
  prepare:
  - nuget restore SeleniumSuite/packages.config -SolutionDirectory .
  - xbuild /p:Configuration=Release SeleniumSuite.sln

execution:
- executor: selenium
  iterations: 10
  scenario:
script: SeleniumSuite/bin/Release/SeleniumSuite.dll  # assembly containing test suites  

 The first part tells Taurus to run external tools. This is done through the ShellExec module, which prepares Nuget and XBuild to compile the C# script. You can use this script part as-is if you download the example we provided from GitHub. Please note that using this Taurus ability requires Mono 4 and Nuget to be installed, and of course Taurus.

The second part tells Taurus to execute the binaries created in the first step by Mono and Nuget, for 10 times, from the path SeleniumSuite/bin/Release/SeleniumSuite.dll.

When running C# scripts, set the executor as Selenium. When doing so, the Selenium executor automatically detects the test types and runs them by using one of the test runners JUnit, NUnit, TestNG, Mocha, etc. You can configure the number of iterations according to your needs.

Now let’s take a look at the source code of our script. The C# script has four simple steps:

  1. Initialize – Creates a new ChromeDriver object.
  2. IndexPage – Browse to blazedemo.com and Assert page title.
  3. ReservePage – Browse to reservation page and Assert page title.
  4. AfterTest – Tear down ChromeDriver.
using System;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;


namespace SeleniumSuite {
 [TestFixture()]
 public class Test {
  IWebDriver driver = null;

  [SetUp]
  public void Initialize() {
   driver = new ChromeDriver();
  }

  [Test()]
  public void IndexPage() {
   driver.Navigate().GoToUrl("http://blazedemo.com");
   Assert.AreEqual(driver.Title, "BlazeDemo");
  }

  [Test()]
  public void ReservePage() {
   driver.Navigate().GoToUrl("http://blazedemo.com/reserve.php");
   Assert.AreEqual(driver.Title, "BlazeDemo - reserve");
  }

  [TearDown]
  public void AfterTest() {
   if (this.driver != null)
    driver.Close();
  }
 }
}

 Change the C# script according to your needs. Make sure you save it in the Selenium Suite folder (in this example).

That’s it! You can now run your tests with Taurus. Once your tests are run through Taurus, they can be automated and integrated with Jenkins or any other CI tool, like Bamboo or TeamCity. Then they can be run with all of your other tests, including load tests, enabling you to shift left while developing.

Learn more Taurus from this free webinar “Using Taurus to Automate JMeter and Selenium Tests“.

Download the ‘Practical Blueprint to Continuous Delivery’ to learn how Automic Release Automation can help you begin or continue your company’s digital transformation.

Topics:
ci ,testing ,continuous testing ,selenium ,c# ,nunit ,devops

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}