C# Selenium Automation

1.Project Overview

This guide will cover creating a C# Selenium automation test suite.

The code generated in this guide is hosted at https://github.com/kirbycope/c-sharp-selenium

2.Installing Visual Studio 2015 Community

  1. In your web browser, navigate to https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx
  2. Select the “Download Community 2015” button
    Installing Visual Studio 2015 Community
  3. In the prompt, select “Save”
    002
  4. After the download has finished, select “Run” from the prompt
    003
  5. When the installer finishes loading, select “Install”
    004
  6. If the User Account Control prompt pops up, select “Yes”
    005
  7. After the setup has completed, select “Launch”
    006
  8. In the sign in prompt, select “Not now, maybe later”
    007
  9. Select “Start Visual Studio” (I also recommend using the “Dark” theme)
    008
  10. Visual Studio is now installed and ready to use
    009

3.Creating the Framework

The framework will contain code that is common to all of our test projects. In this way we limit the amount of copy+paste and redundancy.

  1. In Visual Studio, select “File” → “New” → “Project”
    001
  2. Select “Visual C#” → “Class Library”
    002
  3. Name the project “AutomationFramework” and select “OK”
    003
  4. Delete the auto-generated .cs file
    004
  5. Select “Tools” → “NuGet Package Manager” → “Manage NuGet Packages for Solution…”
    005
  6. In the NuGet tab, select “Browse”
    006
  7. In the search field, type NUnit
    007
  8. Select “NUnit”
    008
  9. In the side-pane, select the checkbox for “AutomationFramework”
    009
  10. Select “Install”
    010
  11. In the Preview popup, select “OK”
    011

    • Repeat Steps 7-11 for:
      • NUnit3TestAdapter
      • Selenium.WebDriver
      • Selenium.WebDriver.ChromeDriver
      • Selenium.WebDriver.IEDriver
      • Selenium.Support
  12. Close the NuGet tab
    012

3.1.Creating a Base Class for Tests

In this framework, we will create a class that our tests will inherit from. Inheritance will allow all of out test classes to share some base functionality or properties without a lot of copy paste. Its purpose in this framework is to define the [Setup] and [TearDown] for every test in a [TestFixture].

  1. In the Solution Explorer, right-click on “AutomationFramework” and select “Add” → “New Item…”
    013
  2. Select “Visual C# Items” → “Class”
    014
  3. Name it “TestBase.cs” and select “Add”
    015
  4. Select the “💡” → “Remove Unnecessary Usings” or, with the cursor in one of the unnecessary using directives, press [Ctrl]+[.] and then press [Enter]
    016

  5. Change the TestBase class to be public
    017

  6. In the class, declare a variable to hold a test’s WebDriver (i.e. browser)
    018

    • Select the text “IWebDriver”, press [Ctrl]+[.], and then press [Enter] to have Visual Studio fix the missing using directive
  7. Below that, type the following statement: protected string baseUrl = ConfigurationManager.AppSettings["url"];
    new-new
  8. In Solution Explorer, right-click “References”, and select “Add References…”
    023
  9. In the Reference Manager window, search for “System.Configuration”
    024
  10. Select “System.Configuration” and then select “OK”
    025

    • Now you can fix the missing assembly reference
  11. In the class, paste in the following method:

    • This method will take a given browser name and set the appropriate WebDriver
    • The window is set to full-screen to avoid the browser opening at whatever size you opened it last (change this to handle responsive design)
    • All cookies are deleted to ensure a clean browser session
    • The implicit wait time is extended from 0 (the default) to 30 seconds
  12. Below that method, paste in the following method:

  13. Below that method, paste in the following method:

    • This method will be called as the first step in any test
    • Fix all missing using directives
  14. Save your work and close the TestBase.cs tab
    027

3.2.Creating A Common Utilities Class

Methods that can be considered global (not only needed for one project) should be part of the framework. One simple example is taking a screenshot of the WebDriver (i.e.browser) when a test fails.

  1. In the Solution Explorer, right-click on “AutomationFramework” and select “Add” → “New Item…”
    001
  2. Select “Visual C# Items” → “Class”
    002
  3. Name it “SeleniumUtils.cs” and select “Add”
    003
  4. Select the “💡” → “Remove Unnecessary Usings” or, with the cursor in one of the unnecessary using directives, press [Ctrl]+[.] and then press [Enter]
    004
  5. Change the SeleniumUtils class to be public
    005
  6. In the class, paste in the following method:
  7. Fix all missing using directives
    007
  8. Save your work and close the SeleniumUtils.cs tab
    008

4.Creating a Test Suite

A Test Suite is a collection of tests cases intended to test an application. In our paradigm, we will create a new Visual Studio solution that references the AutomationFramework. This allows us to test multiple web applications using the same framework. The Test Suite will contain the tests and necessary page object models (POMs).

  1. Close the current solution
    001
  2. Select “File” → “New” → “Project…”
    002
  3. Select “Visual C#” → “Class Library”
    003
  4. Name the project “TimothyCopeCom” (we will use TimothyCope.com for this guide) and then select “OK”
    004
  5. Delete the auto-generated .cs file
    005

4.1.Referencing the Framework

Note: In this section we will add a reference to AutomationFramework and point TimothyCopeCom’s reference to that. You could use NuGet to install NUnit and Selenium on your project (TimothyCopeCom in this guide), but when you add another project things can get funky. Visual Studio will install the packages on TimothyCopeCom and point AutomationFramework reference’s to that location. We manually add it so that the NuGet package stays with the AutomationFramework. This will allow us to add more projects later without issue.

  1. In Solution Explorer, right-click the solution and select “Add” → “Existing Project…”
    06
  2. Browse to and select the AutomationFramework’s .csproj file and then select “OK”
    007
  3. Expand the AutomationFramework
  4. Expand the “References” of AutomationFramework
  5. Right-click “nunit.framework” and then select “View in Object Browser”
  6. Copy the file path of the DLL

    • Repeat for WebDriver and WebDriver.Support
  7. Expand the “References” of TimothyCopeCom
  8. Right-click “References” and then select “Add Reference…”
  9. Select the “Browse” option in the left hand navigation menu
  10. Select the “Broswe…” button
  11. Paste in the .DLL file path and then select “Add”

    • Repeat for WebDriver and WebDriver.Support
  12. Ensure the checkboxes next to each reference is checked and then select “OK”

4.2.Creating a Page Object Model

A Page Object Model (POM) is used in the Page Object Design Pattern. The POM is composed of the following components:

  1. In Solution Explorer, right-click the TimothyCopeCom project and select “Add” → “New Folder”
    001
  2. Type POMs and then press [Enter]
    002
  3. Right-click the POMs folder and select “Add” → “New Item…”
    003
  4. Select “Visual C# Items” → “Class”
    004
  5. Name the file “HomePageObject.cs” and select “Add”
    005
  6. Select the “💡” → “Remove Unnecessary Usings” or, with the cursor in one of the unnecessary using directives, press [Ctrl]+[.] and then press [Enter]
    006
  7. Change the class to be “public”
    007

4.2.1.Create the Constructor

  1. In the class, type ctor and the press [Tab] two times
    008

    • This is a Visual Studio shortcut for making a constructor
  2. Change the constructor to accept an IWebDriver parameter called “driver”
    009

    • Fix the missing assembly reference
  3. In the constructor add PageFactory.InitElements(driver, this); and fix the missing assembly reference
    010

    • This will initialize the elements/properties of the page object so that you don’t get a NullPointerException
    • The this keyword is used because we are constructing an object in its own class

4.2.2.Create the Find Element Logic

  1. Add a region and note it as “FIND ELEMENT LOCATORS”
    011
  2. In the region, type prop and then press [Tab] twice
    012
  3. Change “int” to “IWebElement” and “MyProperty” to “linkHome”
    013
    • Is is best practice to preface your elements with its given type. Here are a few examples:
      • <a> : linkElement
      • <image> : imgElement
      • <button> : btnElement
  4. Above the property, type [FindsBy(How = How.CssSelector, Using ="")]
    014
    • This tells the initialize method how to find the element
  5. In Chrome, open http://timothycope.com/
    015
  6. Right-click on the first item in the navigation menu and select “Inspect”
    016
  7. In the Elements panel, right-click on the element and then select “Copy” → “Copy selector”
    017
  8. In the quotes of the FindsBy statement, paste in the copied selector
    018

4.2.3.Create the Page Specific Methods

  1. Add another region, indicating “PAGE SPECIFIC METHODS”
    019
  2. In the region, paste in the following code:
  3. Close the HomePageObject.cs tab
    021

4.3.Creating a Test

  1. In Solution Explorer, right-click the TimothyCopeCom project and then select “Add” → “New Folder”
    001
  2. Name the folder “Tests”
    002
  3. Right-click the Tests folder and then select “Add” → “New Item…”
    003
  4. Select “Visual C#” → “Class”
    004
  5. Name the file “HomePageTests” and then select “Add”
    005
  6. Select the “💡” → “Remove Unnecessary Usings” or, with the cursor in one of the unnecessary using directives, press [Ctrl]+[.] and then press [Enter]
    006

4.3.1.Create the TestFixture

  1. Make the class “public”
    007
  2. Above the class add the following annotations, [TestFixture] and [Parallelizable]
    008

    • Fix the missing assembly reference
  3. Change the class to inherit from the TestBase class by adding : TestBase;
    009
  4. To fix the missing assembly reference, select “using AutomationFramework” from the “💡” or [Ctrl]+[.] menu
    010

4.3.2.Create a Test Method

  1. In the class, paste in the following code:
  2. Close the HomePageTests.cs tab
    002

5.Setting Up the Config

The browserName and baseUrl of our tests comes from an App.config file we will now add into the project. This will be a config of the Test Suite and not the AutomationFramework as these setting will differ at the project level.

  1. In Solution Explorer, right-click your the “TimothyCopeCom” solution and then select “Configuration Manager…”
    001
  2. In the “Active solution configuration” drop-down, select “New”
    002
  3. Name the configuration “PROD”, select “Debug” in the “Copy settings from” drop-down, and then select “OK”
    003
  4. Select “Close”
    004
  5. Right-click the “TimothyCopeCom” project and then select “Add” → “New Item…”
    005
  6. Select “Visual C#” → “Application Configuration File”
    006
  7. Select “Add”
    007
  8. Repeat Steps 5-7 to create “App.Prod.config”
    008
  9. Right-click the “TimothyCopeCom” project and then select “Unload Project”
    009
  10. Right-click the “TimothyCopeCom” project and then select “Edit TimothyCopeCom.csproj”
    010
  11. Before the closing tag of the first <PropertyGroup>, add <ProjectConfigFileName>App.Config</ProjectConfigFileName>
    011
  12. Find “None”
    012
  13. Modify the App.Prod.config to include <DependentUpon>App.Config</DependentUpon>
    013
  14. Before the closing </Project> tag add the following code:
  15. Close the TimothyCopeCom.csproj tab
    015
  16. Right-click the “TimothyCopeCom” project and then select “Reload Project”
    016

    • If you get an error, then try clicking “Build”.
  17. In Solution Explorer, open “App.Config”
    017
  18. Replace the content with the following code:
  19. Close the App.config tab
    019
  20. In Solution Explorer, open “App.Prod.Config”
    020
  21. Replace the content with the following code:
  22. Close the App.Prod.config tab
    022

6.Running the Tests

You can use Visual Studio’s Test Explorer or NUnit2’s GUI (v3 is still in development as of this guide’s publication date) to execute tests. In this guide, we will use VS Test Explorer.

  1. Select “Test” → “Windows” → “Test Explorer”
    001
  2. Select “Build” → “Build Solution” (Make sure the “PROD” config is used)
    002
  3. Select “Run All”
    003
  4. After the test executes, the results will be displayed
    004

    • If you use System.Console.WriteLine() or an exception is thrown, there will be an “Output” link.

7.Adding Grid

Selenium-Grid allows you run your tests on different machines against different browsers in parallel. That is, running multiple tests at the same time against different machines running different browsers and operating systems. Essentially, Selenium-Grid support distributed test execution. It allows for running your tests in a distributed test execution environment.
– http://www.seleniumhq.org/docs/07_selenium_grid.jsp#what-is-selenium-grid

7.1.Check for Java

Java is needed to run the Selenium Standalone Server (.jar).

  1. Select the Windows key and type cmd
    001
  2. Select “Command Prompt”
    002
  3. Type java -version and then press [Enter]
    003
    • If you do not see the same error message, then skip to the next section.
  4. In your web browser, navigate to http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html
    004
  5. Select “Accept License Agreement”
    005
  6. Select the appropriate Download for your system
    006
  7. Select “Save”
    007
  8. Select “Run”
    008
  9. Select “Install”
    009
  10. After the install completes, select “Close”
    010

7.2.Getting and Starting Grid

  1. In your web browser, navigate to http://www.seleniumhq.org/download/
    011
  2. Select the download link under Selenium Standalone Server
    012
  3. Select “Save”
    013
  4. Select the Windows key and type cmd
    014
  5. Select “Command Prompt”
    015
  6. Type cd Downloads and then press [Enter]
    016
  7. Type java -jar selenium-server-standalone-<version>.jar -role hub and then press enter
    017
    • Replace <version> with the version number of Selenium Server Standalone
  8. In your web browser, navigate to http://localhost:4444/grid/console
    018
  9. Select “view config”
    019
  10. Verify that the role is “hub”
    020
  11. (FYI Only – Do not do while following this guide) To stop the hub press [Ctrl]+[C] in the Command Prompt
    021

7.3.Starting the Node

The node for this example will run locally (same machine that the hub is on) but ideally will be setup on another machine.

  1. Select the Windows key and type cmd
    001
  2. Select “Command Prompt”
    002
  3. Type cd Downloads and then press [Enter]
    003
  4. Type java -Dwebdriver.chrome.driver=<path> -jar selenium-server-standalone-<version>.jar -role node -hub http://localhost:4444/grid/register and then press [Enter]
    04
    • Replace <path> with the path to the chromedriver.exe
      • This should be in ..\c-sharp-selenium\AutomationFramework\AutomationFramework\bin\PROD
    • Replace <version> with the version number of Selenium Server Standalone
  5. In your web browser, navigate to http://localhost:4444/grid/console
    005
    • You’ll see your registered node (on port 5555)
  6. (FYI Only – Do not do while following this guide) To stop the hub press [Ctrl]+[C] in the Command Prompt
    006

7.4.Using Grid to Run Tests

This guide uses a WebDriver setup. For Selenium 1 RC, follow the official quick-start guide.

  1. In Visual Studio, open the project
    011
  2. Open TestBase.cs
    012
  3. Comment out the line that says driver = new ChromeDriver();
    013
  4. Below that, paste in the following code:
    • Fix the missing assembly reference
  5. Save your work and close the TestBase.cs tab
    015
  6. In Test Explorer, select Run All
    016
  7. If everything was setup correctly, your test should pass
    017
Suggest Edit