Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

How to Run Selenium Tests with C# and NUnit

Selenium automation tests can be run as a Console Application, Selenium Service, and NUnit Framework. Since NUnit is used by developers for unit testing, QA engineers can also develop and run Selenium tests with NUnit. Here are the steps for using Nunit with Selenium tests. This example uses Selenium Web Driver and NUnit for Windows.

1. Launch Visual Studio 2010 and create a new class library project and name it as descriptive as possible. In most cases, include test application and component name in the project name.

2. Add the nunit.framework.dll to the reference.

3. Add Selenium .Net Web Driver dlls to the reference.

4. Add the following code to your class library project and compile it.

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

namespaceSeleniumUnitTest
{
    [TestFixture]
    public class SeleniumTest
    {
        private IWebDriver driver;


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

        [Test]
        public void TestSeleniumMasterLoadingPage()
        {
            driver.Navigate().GoToUrl("http://www.seleniummaster.com");
        }

        [TearDown]
        public void TearDown()
        {
            driver.Quit();
            driver.Dispose();
        } 
    }
}

Explanation of each annotations

[Setup] - This is where you need to setup the type of web driver you are going to use. In the above example, Firefox Web Driver is used. Basically, we are creating a new instance of Firefox Web Driver.

[Test] - This is where the test steps are defined. In this example, we are just navigating to the Selenium Master site.

[TearDown] - This is where the browser will be shut down after the test step is completed.

After you compile the project as a dll file, you can run it with NUnit runner or you can run it within Visual Studio as Unit tests.