Selenium C#

Selenium is a web automation tool used by many programmers. Most commonly it's used by testers to automate their testing processes, but it can be used to automate nearly any website for any purpose. Understanding how to use Selenium opens endless possibilities, especially when combined with a first grade programming language like C#.

Some background info on C# in case you have no experience in it:
C# has been around since the beginning of this century when Microsoft decided to create an all encompassing development framework for Windows. It is Object Oriented and this means that you can map your programming to match the real world. Instead of speaking of only digits and abstract variables, you can speak of actual things, such as a car, a factory, an apple, a person, an employee or in case of web automation, we could speak of a browser or web element.

When using C#, we will need an interface (not a visual one, but in the sense of coupling C# to Selenium). This interface is Webdriver. You will be able to find more details in the articles below. For now, an important fact is that Webdriver is recognized by the World Wide Web Consortium, also known as the W3C.

1. Installing Visual Studio (for absolute beginners)

2.  Set up Selenium Webdriver to control your Browser 

3. Finding and controlling xhtml elements with xpath

Whenever using Selenium with C# we'll need Selenium Webdriver. This is an open source and free library for automating web applications, mainly used for testing but can also serve other purposes such as web scraping.  Selenium supports mosts browsers and in this tutorial we'll show you how to  use Chrome but it will be easy to switch to Firefox or or Internet explorer for example.

Selenium can be used with many languages such as Python, Ruby, etc. Selenium and Java is a popular combination. Here we'll be focusing on Selenium with C#. Just as an fyi, the binding of selenium to a specific programming language is very similar. This means, that allthough we'll use selenium C#, you could relatively easily switch to Java.

//C#
static void Main()
{
    IWebDriver driver = new ChromeDriver(); //Start a new instance of chrome
    driver.Navigate().GoToUrl("http://www.mashitupide.be/TestPages/TestPage1.html"); //navigate to the page
    IWebElement element = driver.FindElement(By.id("frmSubmit")); //find a button on the page
    driver.FindElement(element).Click(); //Click the button
}

//Java
 public static void main(String[] args) 
{ 
    WebDriver driver = new ChromeDriver(); //Start a new instance of chrome
    driver.get("http://www.mashitupide.be/TestPages/TestPage1.html"); //navigate to the page
    WebElement element = driver.FindElement(By.id("frmSubmit")); //find a button on the page
    driver.FindElement(element).Click(); //Click the button
}
 

The first thing you'll need is to set up your development environment. We'll be using Visual Studio. Visual studio is the user interface most C# developers use and it's from Microsoft, the creator of C#. Alternatively you could use Jetbrains Rider as a development environment but it's not free. You can download Visual Studio Community Edition to use with Selenium freely. Also make sure you have chrome installed on your Windows computer.

Once this is installed we can get started with some coding. In the selenium visual studio steps you'll see how to create a console application. A console application is a general purpose application with minimal dependencies. When you run this application, you'll just see a black box. Depending on what you want to do, you can also select a Unit Test project. The Unit test project will help you create specific tests and assert a report. We'll continue with the console application. Should you want a Unit test, the picture below will show you how to start a unit test project:

Unit test selenium C#

The next steps involves adding selenium webdriver to the project. In the past we had to surf the internet and lookup third party libraries on a variety of websites. Nowadays this is no longer an issue. Thanks to nuget, you can just right click your project, click "Manage NuGet Packages", then search for the library that you need. Just type "Selenium" and you'll find "Selenium.Webdriver". Install the latest version. We will also need the specific selenium drivers for Chrome. Look for ChromeDriver and install it. I've described the details on the "Selenium Webdriver C#" page .

If you are interrested in cross browser testing with Selenium C#, this should be easy. Just look for the specific browser drivers in nuget. Search for Firefox, install the correct driver. In your code just make the following changes

//Instead of
IWebDriver driver = new ChromeDriver();
//Use 
IWebDriver driver = new FirefoxDriver();

Now we should have everything setup to start automating web pages.

How to Find and use WebElements

A WebElement represents a part of a webpage that can be manipulated, such as a textbox or a button. There are several ways to locate a WebElement in Selenium with C#. I've summed up the most common ways here below:

By Name: Find the element based on the name it has (this is not always the case), ex. FindElement(By.Name("frmSubmit"));
By ID: Find the element based on it's ID.  ex. FindElement(By.id("frmSubmit"));
By CSS: Search the element based on the CSS classes.
By XPath: Without a doubt the most powerfull way to find elements inside of your webpage. There have been books written on XPath because it's so flexible. As long as your webpage is xml compliant, searching for WebElements by XPath is my prefered way of finding an element.

A big advantage of XPath over the other ways of locating elements in Selenium for C# is that you can have relative paths to an element. This means that you can build your XPath in such a way that it's less influencial to changes on the webpage.

Here is the difference between a relative and absolute XPath:

Absolute Xpath: Traverses the document object from the top down to the specific WebElement, starting from the root node. 

/html/body/form/table/tbody/tr[3]/td[2]/input

Relative XPath: This is a way of finding an element starting anywhere on the webpage. No need to write a long XPath and can be very handy. You can modify it so that the chances of your code breaking is minimal when the webpage changes.

//*[@id="frmSubmit"]

Check out this more detailed explanation on how to perform Selenium FindElement with C# and using Chrome.

Recording, Manual Automation and Web Scraping in Selenium C#

There are ways to find elements easily by recording your actions. For this you can use Selenium IDE. Allthough I'm not the biggest fan of recording, this can be usefull in some cases.

Regular Expressions

When using selenium for web scraping then more often than not you'll need to parse the text inside of your elements to get the exact information that you want. For this we can use Regular Expressions.

 

Soon More on following topics:

- Using Chrome for building your automation

- Using Internet Explorer

- Using Firefox

- Elements

- Xpath and selenium

- IDs

....