UI Automation With C# and Selenium: Adding Packages

The Takeaway: Installing the right packages from the start makes everything else a lost easier.

So far, we’ve created a blank solution with a few projects and added that solution to a repository. Now we need to install the right packages so that when we start coding some tests, everything will be in place and we won’t need to go back and figure out what we’re missing. For our automation, we’ll start out with the following packages:

  • Selenium
  • NUnit
  1. In your automation solution (which you can find the previous post for here), Navigate to Tools > NuGet Package Manager > Manage NuGet Packages for Solution

    Visual Studio - Manage NuGet Packages
    Visual Studio – Manage NuGet Packages
  2. Select the Browse option and search for Selenium 

    Visual Studio - Selenium NuGet Packages
    Visual Studio – Selenium NuGet Packages
  3. For each package Selenium. WebDriver and Selenium.Support, select the package. Notice that the project where our tests live is the only option available installing this package. Remember, since the framework is a Shared Project, there’s no DLL file created for that project, but the files required for the tests will be automatically included when the test project is built 

    Visual Studio - Packages Install in the Test Project
    Visual Studio – Packages Install in the Test Project
  4. Select the Test project (UI Tests in the example above) and select the Install Button 

    Visual Studio - Installing Packages
    Visual Studio – Installing Packages
  5. You’ll probably need to confirm the install of each package as well 

    Visual Studio - Review Package Install
    Visual Studio – Review Package Install
  6. After both the Selenium.WebDriver and Selenium.Support packages are installed, search for and install the NUnit and NUnit .Console packages as well 
    Visual Studio - Installing Nunit Packages
    Visual Studio – Installing NUnit Packages

    Once the NUnit packages are installed, you can select the Installed link and see that several NUnitpackagaes were installed. These packages will be the basis for our framework and tests, but we’ll add more as we progress.

UI Automation With C# and Selenium: Adding Your Project to GitHub

The Takeaway: Using GitHub as your repository for version control makes development of your automation solution MUCH easier!

An important element of your automation project is to make sure you can keep it backed up and available. 

A great way to do that is with GitHub. As a version control system and repository, GutHub allows you to keep track of changes in your automation solution, make the code available to others working on the project, and fork your project in order to further develop the project in new directions.

This article will assume that you:

  • Already understand the basics of version control and repositories. If not, this video and associated YouTube channel can help out there
  • Already have a GitHub username and password. If not, do that here
  • Already have the GitHub Extension installed as part of Visual Studio, If not, here it is. 
  1. Open your Automation solution in Visual Studio. 
  2. Right-click on the solution for the project and select Add Solution to Source Control

    Visual Studio - Add Solution to Source Control
    Visual Studio – Add Solution to Source Control
  3. At this point, Visual Studio will create a git folder in the folder where your solution is saved (which is typically hidden). This folder is needed to keep track of changes to your solution

    Visual Studio - Git Folder Created
    Visual Studio – Git Folder Created
  4. You will also notice that your solution, folders, and files have a blue lock on them

    Visual Studio - Solution Locked
    Visual Studio – Solution Locked
  5. Navigate to the Team Explorer window

    Visual Studio - Team Explorer Home
    Visual Studio – Team Explorer Home
  6. Click the Sync link, and the Publish to GitHub option should display

    Visual Studio - Publish to GitHub
    Visual Studio – Publish to GitHub
  7. Click the Get Started link. Since the repository you just created doesn’t haver a remote version, we can use this tool to create a version on the GitHub website

    Visual Studio - Repository Details
    Visual Studio – Repository Details
  8. Click Publish. Now you should be able to go to your GitHub Profile, click your Repositories link, and view your repo
    GitHub - New Repository
    GitHub – New Repository

    Click here to find the above page, so you can either clone or download the files for this solution as well!

UI Automation With C# and Selenium: Visual Studio Project Types

The Takeaway: Using shared projects with class libraries is a good way to start your Visual Studio automation project.

Setting Up Your Visual Studio Project

One of the initial things you need to decide when you start an automation project is what the structure of your Visual Studio Solution will be. I have worked with several different scenarios, involving various types of projects:

Class Library:

Default project type

Results in the compilation of a DLL (Dynamic Linked Library) that can be ran by Nunit

NUnit Unit Test Project:

Available through the NUnit VS Templates Extension in Visual Studio

Provides code snippets that make getting up and running with NUnit a bit quicker

NUnit VS Templates
NUnit VS Templates

Unit Test Project:

Similar to the NUint VS Templates project template, the Unit Test project template creates a class file with a basic frame of a unit test already built in (including a using statement for the Microsoft.VisualStudio.TestTools.UnitTesting package and properties for a class and method to mark them as a test class and test method). 

I’m sure there are others that are and have been used as well. BUT, one of my favorite project types for testing has become the Shared Project.

Shared Projects:

What I love about Shared Projects is that any code in that project, referenced by other projects, is automatically added to and complies with the other project. So, you can have multiple types of test projects in your solution (one for UI testing, one for API testing, one for database testing, one for security testing), and one single repository for your framework. When you compile the code for that project, the shared code that applies to that project.

How Shared Projects Share Code
Shared Projects

Starting Your Automation Solution

  1. Open up Visual Studio. I will use Visual Studio 2015, but these examples should all work in Visual Studio 2017 just as easily
  2. Navigate to File > New > Project
  3. In the New Project window, select the Templates TreeView > Other Project Types
  4. There should be a Blank Solution template available to you. Select that in the Window, give your solution a name (I’m calling mine Automation), ensure that the Create directory for solution checkbox is checked, and click the OK Button:

    Visual Studio Automation Project - Blank Solution
    Visual Studio Automation Project – Blank Solution
  5. Now, you should have a blank solution with nothing in itNow, we need to add at least two projects to this solution: one Shared Project, and one 
  6. Navigate to File > New > Project
  7. Select the Templates TreeView > Visual C#
  8. Select Shared Project, name it Framework, select Add to solution, make sure you’re in the same location as your blank solution, make sure the Create Directory for solution checkbox is selected, and select the OK Button

    Visual Studio Automation Project - Shared Project

      Visual Studio Automation Project – Shared Project

    Now, you should have a solution with an empty shared project. Time to add the test library:

  9. Navigate to File > New > Project
  10. Select the Templates TreeView > Visual C#
  11. Select Class Library, name it Framework, select Add to solution, make sure you’re in the same location as your blank solution, make sure the Create Directory for solution checkbox is selected, and select the OK Button
Visual Studio Automation Project - Class Library
Visual Studio Automation Project – Class Library

So, you should wind up with a solution with two project templates: a Shared Project for your framework, and a Class Library for your tests. This is the base off of which we will build our automation and framework. Using a Shared Project, we can add whatever we need into the framework portion of our solution, then simply use the necessary components when we build and deploy our tests.

Visual Studio Automation Project - Complete
Visual Studio Automation Project – Complete

A Zip file of this project is available here