Overview

In this tutorial, we will create a NUnit Test Case in C#, using Selenium's WebDriver for browser automation.

Description

The test case opens a Google page, searches for a given keyword and checks the results returned by Google.

Note: The ChromeDriver must be on the path.


using System;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumTests
{
    [TestFixture]
    public class WdriverTest
    {
    private IWebDriver driver;

    [SetUp]
    public void SetupTest()
    {
        driver = new ChromeDriver();
    }
    [TearDown]
    public void TeardownTest()
    {
        try
        {
        driver.Quit();
        }
        catch (Exception)
        {
        // Ignore errors if unable to close the browser
        }
    }
    [Test, Property("Requirement", "CALC-1")]
    public void GoogleTest()
    {
        // Open Google search engine.
        driver.Navigate().GoToUrl("http://www.google.com/");
        // Assert Title of page.
        Assert.AreEqual("Google", driver.Title);
        // Provide search term as "Selenium OpenQA"
        IWebElement query = driver.FindElement(By.Name("q"));
        query.SendKeys("Selenium OpenQA");
        query.Submit();
        Assert.IsTrue(driver.PageSource.Contains("www.openqa.org"));
        //Assert.AreEqual("Selenium OpenQA - Google Search",driver.Title);
    }
    }
}
{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "testRunner": "nunit",
  "dependencies": {
        "NUnit": "3.5.0",
        "dotnet-test-nunit": "3.4.0-beta-2",
        "Selenium.WebDriver.ChromeDriver": "2.27.0",
        //"Selenium.Firefox.WebDriver": "0.13.0",
        //"Selenium.WebDriver": "3.0.1"
        "CoreCompat.Selenium.WebDriver": "2.54.0-beta002" 
  },
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.0"
        }
      },
      "imports": "dnxcore50"
    }
  }
}


After successfully running the Test Case and generating the NUnit XML report (e.g., TestResult.xml), it can be imported to Xray via the REST API or the Import Execution Results action within the Test Execution.

NUnit's Test Case is mapped to a Generic Test in Jira, and the Generic Test Definition field contains the name of the namespace, class, and the method name that implements the Test case.

The Execution Details of the Generic Test contains information about the context, which in this case corresponds to the Test case method itself. 


Tips

If you're using Visual Studio as your IDE, you need to have some dependencies/packages installed.

These can be installed from Tools>NuGet Package Manager (using the console or the manager's UI).


Then you can configure the Test Explorer to run the NUnit tests while at the same time producing a NUnit XML report.


<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
   <NUnit>
     <TestOutputXml>C:\TestResults</TestOutputXml>
 </NUnit>
</RunSettings>

 


References