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("https://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.

Similar Posts from the author:

15 thoughts to “How to setup Selenium WebDriver with Visual Studio and NUnit

  • RON

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

  • poo

    Can we run this test on iphone real device.

  • Naveen

    Here is one more usefull link that may help: http://iamautomationgeek.blogspot.in/2014_09_01_archive.html

  • Naveen

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

  • Ashwin

    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 !

    • Tihomir Turzai

      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.

  • Orale Barron

    you have got an incredible blog right here!

  • Katja

    Very good content i think.

  • krishna

    Hi

    Thank you for Nice article and useful for beginners. can u tell me some frameworks and integrating tools used in selenium with visual studios .

  • krishna

    hi i am new to selenium ,

    what frame work is used with selenium in visual studio.

    Thanks

    • Tihomir Turzai

      Hi Krishna,

      Nowadays, we are rarely using Visual Studio for webdriver testing. Back in the time we used NUnit to handle the test suites.

Comments are closed.