Quantcast
Viewing all articles
Browse latest Browse all 22

How to setup Selenium WebDriver with Visual Studio and NUnit

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

Image may be NSFW.
Clik here to view.
NUnit download

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

Image may be NSFW.
Clik here to view.
Selenium WebDriver

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

Image may be NSFW.
Clik here to view.
Add Reference

Image may be NSFW.
Clik here to view.
Add Reference

Image may be NSFW.
Clik here to view.
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 Image may be NSFW.
Clik here to view.
🙂
)
Click Run, and if everything went fine you will get the following screen:

Image may be NSFW.
Clik here to view.
testbase

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


Viewing all articles
Browse latest Browse all 22

Trending Articles