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 www.yahoo.co.uk
  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
{
[TestFixture]
public class WebDriverDemo
{
private IWebDriver _driver;
private string _baseUrl;

[SetUp]
public void SetupTest()
{
_driver = new FirefoxDriver(); // we want our tests to run in Firefox
_baseUrl = “http://www.yahoo.co.uk”;

}

[TearDown]
public void TeardownTest()
{

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

[Test]
public void NavigateToTrainLineHomePageAndAssertOnTitle()
{
_driver.Navigate().GoToUrl(_baseUrl);
_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.