Skip to main content
Selenium

Introduction to Selenium with C Sharp

Selenium WebDriver is a free and open-source library for automated testing web applications. Selenium tests can be written in multiple programming languages such as Java, C#, Python and Ruby and multiple browsers we execute scripts.

1

Prerequisite:

  • Download and install Visual Studio Frame Work.
  • Your target browser is installed, such as Chrome or Firefox.

Set up Visual Studio Solution:

Create a new project in Visual Studio Select template ‘Templates’ → ‘Visual C#’ → ‘Test’ → ‘Unit Test Project’.

2

Add Selenium WebDriver package to the project.

Run the following command in ‘Package Manager Console’ (selecting menu ‘Tools’ → ‘NuGet Package Manager’ → ‘Package Manager Console’).

3

After Selenium WebDriver is installed,  Solution References will appear like the following:

4

How to Create a test and run it:

  1. Add a new C# (a test) Class. Right mouse click the project name (in Solution Explorer) select ‘Add’ → ‘Unit Test’.
  2. Define the following class code to test and run it.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Safari;

namespace TestAppDemo
{
    [TestClass]
    public class UnitTest2
    {
        static IWebDriver driverGC;
       // static IWebDriver driverFF;
        [AssemblyInitialize]
        public static void setup(TestContext contest)
        {          
            driverGC = new ChromeDriver(@"D:\Documents Study\Selenium details\chromedriver_win32");
        }        
        [TestMethod]
        public void TestChromeDriver()
        {
            driverGC.Navigate().GoToUrl("http://automationpractice.com/index.php");
            driverGC.FindElement(By.Id("search_query_top")).SendKeys("CASUAL DRESSES");
            driverGC.FindElement(By.Id("search_query_top")).SendKeys(Keys.Enter);

         
        }     
  [TestMethod]
        public void TestClose()
        {
            driverGC.Quite();          
        }     

    }
}

Right Click on `class’ and select ‘Run Tests’.

5

After running successful, test case, it will show as below with pass mark:

7

We have to understand the below elements and their usages:

  • By Id: Finds the Webpage element by its unique control id.
  • By Name: Finds the Webpage element by the control name.
  • By Class: Finds the Webpage element by the Class name.
  • By LinkText: Finds the link element by its exact text displayed on web page
  • By partialLinkText: Finds webpage element that contains the given text
  • By TagName: Finds the Webpage element by the Tag name.
  • By XPath: This is most popular and majorly used for locating webpage element in a easiest way.
  • By CSS selector: Finds Webpage element by the CSS selectorEx.

What are the Advantages of Selenium Test using C#:

  • It is Open source.
  • It can be extended with different technologies.
  • In multiple browsers we execute scripts.
  • It supports multiple OS platforms and compatible with mobile devices.
  • Test executes within the browsers, so focus is not needed while executing the script.
  • Using Selenium Grids, you can execute tests in parallel.
  • Web Driver is faster than Selenium RC because it has simpler architecture.
  • Web Driver directly talks to the web browser whereas Selenium RC needs the help of the RC Server in order to do the same.

Disadvantages of Selenium Test:

  • It supports only web based applications.
  • It does not provide any facility for Object Repository.
  • It does not provide any facility for Recovery Scenario.
  • There is no report generation for test methods by default.
  • Users need to depend on minimum one programming language.

For more details, you can go through this click here.

Mallesh Vibhuti

Mallesh Vibhuti

Mallesh Y Vibhuti is a Senior Software Engineer with Trigent Software. With over six years experience he is skilled in languages such as C#, WinForms,SQL Server,MySQL,SSRS,ADO.Net and WCF. Mallesh enjoys adapting to new technologies really quickly.

2 thoughts on “Introduction to Selenium with C Sharp

  1. Hi, Neat post. There’s a problem with your website in internet explorer, would test this… IE still is the market leader and a huge portion of people will miss your wonderful writing because of this problem.

Comments are closed.