Selenium - Automation Testing

Automation Testing

In VS, how to write automate test cases through Selenium:

Download file "selenium-dotnet-2.20.0.zip" from https://code.google.com/p/selenium/downloads/detail?name=selenium-dotnet-2.20.0.zip&can=2&q= Unzip files and place it in new folder say 'Selenium'

Download NUnit (www.nunit.org) and TestDriven (http://testdriven.net/download.aspx) and install it.

Create new C# project in VS2010

Add References of dll files unzipped in step 2, say Webdriver etc.

Add Reference of Nunit.framework

Include namespaces accordingly in you CS file
  1. using System;   
  2. using System.Threading; //include namespace (NS)   
  3. using NUnit.Framework; //add reference and include NS   
  4. using OpenQA.Selenium; //add reference and include NS   
  5. using OpenQA.Selenium.Firefox; //add reference and include NS   
  6. namespace Project1   
  7. {     
  8.    [TestFixture]   
  9.    public class CTD   
  10.    {   
  11.       private IWebDriver _driver;// = new FirefoxDriver();   
  12.       [SetUp]   
  13.       public void Init()   
  14.       {   
  15.          _driver = new FirefoxDriver(); //you can use InternetDriver for IE   
  16.          _driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(100));   
  17.          //seconds you can define maximum time of any event to be executed so test shouldn't fail   
  18.       }   
  19.       [Test]   
  20.       public void TestCase1()   
  21.       {   
  22.          _driver.Navigate().GoToUrl("http://localhost:xxxxx/abc.aspx");   
  23.          //replace your localhost portno and mention startpage for test   
  24.          IWebElement query = _driver.FindElement(By.Id("txtFName));   
  25.          //Find element on webpage   
  26.          query.SendKeys("Sumit Jolly"); //fills data in textbox   
  27.          Assert.AreEqual ("Sumit Jolly", _driver.FindElement(By.Id("txtFName)));   
  28.          //Test whether data entered by you is right   
  29.          _driver.FindElement(By.Id("btnSubmit")).Click();   
  30.       }   
  31.       [Test]   
  32.       public void TestCase2()   
  33.       {   
  34.       }   
  35.       [TearDownAttribute]   
  36.       public void Clean()   
  37.       {   
  38.          Thread.Sleep(1000); //milliseconds   
  39.          // TODO add wait   
  40.          _driver.Quit();   
  41.       }   
  42.    }   
  43. }   
Follow me @sumitjolly