Integrate Selenium With Visual Studio

Here we are going to see how to integrate selenium with Visual Studio. This actually help developers to test their code on their own.

What is selenium and why we need them

Selenium, almost all techies will look for this now. Selenium is an open source software testing framework that helps user to run an automated test on their web based applications.

So, how this happens

Selenium WebDriver is the successor to Selenium RC. Selenium WebDriver accepts commands (sent in via a Client API) and sends them to a browser. Selenium consists of APIs that helps to integrate with any programming language such as Java, C#, Groovy, Perl, PHP, Python and Ruby.
Now, we will see how we add them to our visual studio. These are the simple steps that helps you to integrate selenium to Visual studio.

STEP 1: Open Visual Studio and create new project,

new

STEP 2: Click on test project (here I’m using Visual Studio 2010).

unittest

Now, after creating a New Test project, we need to add the Selenium APIs to the Visual studio. Here is the link to download Selenium Web Driver.

debug

STEP 3: Adding selenium to your c# project in Visual Studio.

selenium

STEP 4: Add all dll files from the folder where selenium drivers are extracted.

drivers

STEP 5:

  • Click on OK.
  • Also check all Dll’s are added properly.

STEP 6: Replace the following code and run them.

  1. using System;  
  2. using OpenQA.Selenium;  
  3. using OpenQA.Selenium.Chrome;  
  4. using OpenQA.Selenium.Support.UI;  
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;   
  6.   
  7.   
  8. namespace SeleniumExample   
  9. {  
  10.     [TestClass]  
  11.     public class UnitTest1   
  12.     {  
  13.         ChromeDriver Chrome;  
  14.   
  15.         // This is the test to be carried out.  
  16.         [TestMethod]  
  17.         public void TestMethod1()  
  18.         {  
  19.   
  20.             Chrome = new ChromeDriver();  
  21.             Chrome.Navigate().GoToUrl("http://www.google.com/");  
  22.             Chrome.FindElement(By.Id("lst-ib")).SendKeys("APJ ABDUL KALAM");  
  23.             Chrome.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);  
  24.         }  
  25.   
  26.         // This closes the driver down after the test has finished.  
  27.         [TestCleanup]  
  28.         public void TearDown()  
  29.         {  
  30.             //Chrome.Quit();  
  31.         }  
  32.     }  
  33. }  
  • You will be getting an error stating Chrome web driver is missing exception. Why this happen is the code cannot find the chromeWebdriver.exe
  • To debug this issue, we have to install a package to this project.
  • Right click on references and click on NUget packages .

    nuget

STEP 7:

  • Install Selenium Web Driver.
  • Click ok once you completed with your installation.
  • Now run the test again.
  • The following result will come if the test is successful else it will display the fail status.

    status

    output

Up Next
    Ebook Download
    View all
    Learn
    View all