Getting Started with Webdriver and CSharp (C#)

Tools you will need:

  1. An IDE – I’m using Visual Studio 2010 but you can use any IDE. For example Visual Express, which is free
  2. Nunit – Download it from here
  3. Webdriver C# Clent Drivers – Download them from here


Open Visual Studio and create a New Project (Class Library):


In your project references, add the references for Nunit.Framework.dll, Webdriver.dll & Webdriver.Support.dll



You are now ready to write some code. Here is what we will be doing.

  1. We will run our tests in firefox browser
  2. The test will navigate to
  3. Type “selenium hq” into the search box
  4. Clink the “Search” button
  5. On the results page we will Assert on the title of the page.


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

namespace GettingStartedWithWebdriver
public class WebDriverDemo
private IWebDriver _driver;
private string _baseUrl;

public void SetupTest()
_driver = new FirefoxDriver(); // we want our tests to run in Firefox
_baseUrl = “”;


public void TeardownTest()

_driver.Quit(); // quits webdriver and closes any associated windows

public void NavigateToTrainLineHomePageAndAssertOnTitle()
_driver.FindElement(By.Id(“p_13838465-p”)).Clear(); // clear the text field before entering text
_driver.FindElement(By.Id(“p_13838465-p”)).SendKeys(“selenium hq”); // type into the text field
_driver.FindElement(By.Id(“search-submit”)).Click(); // click on the search button
Assert.AreEqual(“selenium hq – Yahoo! Search Results”, _driver.Title); // Assert using nunit on the window title


Incase you are wondering where did I get the ID of the fields from, you can use a tool like firebug in firefox to inspect elements on a broweser. See below


 And there you have it, your first test in Webdriver and CSharp.

Leave a Reply

Your email address will not be published.