Tools you will need:
- An IDE – I’m using Visual Studio 2010 but you can use any IDE. For example Visual Express, which is free
- Nunit – Download it from here
- 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.
- We will run our tests in firefox browser
- The test will navigate to www.yahoo.co.uk
- Type “selenium hq” into the search box
- Clink the “Search” button
- 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.