Start with C# WebDriver - Procedure to create first test script for the beginners

Prerequisite:
  1. Visual studio should be installed in machine..
  2. Download “Nunit: from link http://nunit.org/index.php?p=download and install in your machine.
  3. Download selenium-dotnet-2.31.0.zip file from link http://docs.seleniumhq.org/download/ and unzip in your machine.
Step To create new project.

    1. Open Visual studio in your machine.
    2. Go to and click on  File > New > Project.
    3. Select visual C# from left panel and select class library


     4. Enter project name  chose location and click on OK button.
     5. A default class created with named “Class1.cs” under your project         
     6. Rename this class or create new class for your test script.
Step To add references:
  1. Right click on project and click on “Add References” option.
  2. Browser from  “selenium-dotnet-2.31.0.zip” unzipped folder and select “WebDriver.dll” “WebDriver.Support.dll” and “Selenium.WebDriverBackedSelenium.dll” files.
  3. As above step add reference “nunit.framework.dll”, “nunit.mocks.dll”, “pnunit.framework.dll” from nunit installed “bin\frameworks“ folder.
  4. After added references dll files in your project, solution explorer look like below screen.
Step to create first test script:

     1. Record script using selenium IDE and convert it into “C# / Nunit /Webdriver”
     2. Add to converted test class in visual studio. Your test script look like as below code.
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;


namespace SmapleProject
{
    [TestFixture]
    public class Google
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
     
       
        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "https://www.google.co.in/";
            verificationErrors = new StringBuilder();
        }
       
        [TearDown]
        public void TeardownTest()
        {          
            driver.Quit();          
        }
       
        [Test]
        public void GoogleTest()
        {
            driver.Navigate().GoToUrl(baseURL + "/");
            driver.FindElement(By.Id("gbqfq")).Clear();
            driver.FindElement(By.Id("gbqfq")).SendKeys("Testing");
            driver.FindElement(By.Id("gbqfb")).Click();
        }     
    }
}
      3. Test class in divided into three methods SetupTest ()( this method will execute before any test methods)   TeardownTest()(this method will execute after test method)  GoogleTest() (this is actual test methods).

Execution:
:
Go to start and open Nunit console. Your console look like as below screen.
Go to file menu, click on open project option . Navigate “bin\debug” folder of your created project and chose project dll file.Your project tests should be displayed in left panel of Nunit console.
Chose your test and click on run button. Your test should be executed and execution report will display on Nunit  console
Hope above post will help to start working on webdriver with C#

No comments:

Post a Comment

Leave your comments, queries, suggestion I will try to provide solution