Saturday, 3 August 2013

How to Configure Selenium Webdriver with C#




How To Configure  SeleniumWebdriver  with C# (Visual Studio )

Getting Started With Selenium C#


Step 1: Download Selenium Webdriver from http://docs.seleniumhq.org/download/



Step 2  : Start Visual Studio and  create a New Project
              Select New Project->Visual  C#->Windows->Console Application

Step 3: Right Click on Console Application under Solution Explorer and Click on 'Add Reference '


Step 4: Click on ‘Browse’ on ‘add Reference’ Window and Navigate to Selenium Webdriver Location and Select folder net40


Step 5: Select all files and Click on ‘Add’


 Step 6: Click ‘OK’ on Reference Manager Window




Step 7: Start Coding in C# J
-Add Selenium namespaces with ‘using’ directive.
-Paste Below code in  program.cs
(Script to search word 'Test Automation' on bing.com)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             //Initiate Driver
            IWebDriver driver = new FirefoxDriver();
            //Get Element by Id
            driver.Navigate().GoToUrl("http://bing.com");
            driver.FindElement(By.Id("sb_form_q")).SendKeys("Test Automation");
            driver.FindElement(By.Id("sb_form_go")).Click();
            Console.WriteLine("Test Case Completed");
       
        }
    }
}
Step 8: Run Selenium Script
-Press F5 or ‘Start ‘



After Running Script




 Run  Selenium script in IE or Chrome Following changes may Require
       1. Download Driver from http://docs.seleniumhq.org/download/ according to
 System Type (32 –bit or 64-bit)




2.       Add namespaces  using OpenQA.Selenium.IE; OR using OpenQA.Selenium.Chrome;
3.       Give Path of respective driver (IE or Chrome) as follows.
-          For  IE
   IWebDriver driver = new InternetExplorerDriver(@"C:\Users\IE Driver\IEDriverServer_Win32_2.33.0");

-          For  Chrome
IWebDriver driver = new ChromeDriver(@"C:\Users\Chrome Driver\chromedriver_win32_2.1");

No comments:

Post a Comment