First step is to download the necessary software NUnit and WebDriver:
Download Nunit from http://www.nunit.org/ and install it.

NUnit download

And download the newest Webdriver library from page http://seleniumhq.org/download/

Selenium WebDriver

Open up a new Class Library Project in Visual Studio and add the downloaded Webdriver and NUnit library files to the project.

Add Reference

Add Reference

Add Reference

When all of this is done we can start to write some simple test code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using NUnit.Framework;
using OpenQA.Selenium.Firefox;

namespace ClassLibrary2
{
    [TestFixture]
    public class UnitTest1
    {

        IWebDriver driver;

        [TestFixtureSetUp]
        public void TestSetUp()
        {
            // the same way we can setup webDriver to use other browsers
            driver = new FirefoxDriver();

            // set the timeout after page load to 30seconds
            driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));

        }

        [Test]
        public void TestMethod1()
        {

            driver.Navigate().GoToUrl("http://www.wedoqa.com");

            Assert.AreEqual("WeDoQA", driver.Title);
        }

        [TestFixtureTearDown]
        public void FixtureTearDown()
        {
            // closes every window associated with this driver
            driver.Quit();
        }
    }
}

This code will initialize Webdriver using a Firefox Driver, which means that Firefox will be used to run the test. And it will set the default timeout after a page load request. The timeout should be between 30-60 seconds, depending on your internet connection and on how much js/flash is used on the page.

The testMethod will only check if we are on the correct page by testing the page title. It is a good practice to check this before any other tests take place, this way you can spare some time during test debugging.

To test the code open NUnit. Click “Open Project” in the File menu, and choose the projects dll file. By default it can be found C:\Users\Username\Documents\Visual Studio 2010\Projects\ProjectName\Projectname\bin\Debug\ProjectName.dll
(Replace ProjectName with your project’s name :) )
Click Run, and if everything went fine you will get the following screen:

testbase

And voila the testbase is ready for creating some more advanced tests.

Posted By Tihomir Turzai

    7 Responses to “How to setup Selenium WebDriver with Visual Studio and NUnit”

  1. RON says:

    Thank you for the above article, It really helped me ..!

  2. poo says:

    Can we run this test on iphone real device.

  3. Naveen says:

    This is about setting up webDriver & running a small test: http://iamautomationgeek.blogspot.in/2014/07/selenium-webdrivers.html

  4. Ashwin says:

    Hi Tihomir,
    I want to use visual studio with Web Driver for functional testing.
    My concern is do i need to have any framework? As i can see you have used NUnit and OpenQA, are those freeware? And can use TeamCity for the same purpose? Or any other freeware frameworks that i can use?

    Thanks !

    • Hi,

      This post is a bit old, but probably you can still use it. Be sure to use the newest version of everything. There are lots of new special features in the current version of webdriver which can make your life easier.
      NUnit and Webdriver(OpenQA) are free to use. It is not entirely clear for what do you want to use TeamCity, but i think you can start the whole test run from TeamCity if that is what do you want to do.
      There is full freeware tool you can use called Jenkins.

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>