Friday, 24 January 2014

How to set up selenium web driver in C#?

Welcome Friends - In this article I am going to tell you how we can set up the selenium web driver in C sharp ( C#.Net)

What you need to set up Selenium Web driver in .Net?


  1. Microsoft Visual Studio.
  2. Selenium Web Driver API.
  3. Web-driver for Chrome 

You can download these things and then read below steps.
Please note that for testing applications on Internet Explorer, you will have to download the internet explorer driver separately.

Setting up Selenium Web driver in Visual Studio.Net 

We can set up selenium web driver in visual studio .Net easily with below steps.
  1. Create a new console based C# project.
  2. Go to the Project menu  and click on "add references".
  3. Choose all Web Driver API dll files.
  4. Finish.
Now you can access the selenium classes defined in the dll files to automate the web application.

Here is the sample code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//use of Selenium,Selenium.Chrome and Selenium.Support.UI namespaces is required
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

//Create a new Chrome webdriver instance 
//Please note that we have to give the path of the chromedriver.exe driver in below statement

        IWebDriver x = new ChromeDriver(@"C:\Users\sagar\Downloads");
//Set the URL to navigate to
        x.Url = "http://register.rediff.com/commonreg/index.php";
        x.Navigate();
// Find the element having id "fullname"
        x.FindElement(By.Id("fullname")).SendKeys ("sagar");    
//Quit the driver.
        x.Quit();
        }
    }
}

After you run above code chrome browser will be launched and it will navigate to the rediff registration page and enter the sagar in first name edit box.



What do you think on above selenium topic. Please provide your inputs and comments. Thanks

No comments:

Post a comment

Buy Best Selenium Books