I. How to set up a Visual Studio unit test project for automation testing?

  1. Open Visual Studio and click “New Project” to start a new project. For those of you that are new to Visual Studio, a project can represent a collection of things such as classes and tests.1-1- automation testing new project
  2. On the left pane, open Templates > Other Languages > Visual C# > Test > Unit Test Project. A unit test project is useful to create a collection of unit tests. Something that an Automation Engineer desires.
    1. The highest level is the Solution which can encompass multiple projects. The projects can contain multiple classes and other assets. So the name of the solution usually represents your organization. In this case, I just named it QTP Tutorial Tests for our purposes, assuming that this solution would just have a bunch of tests related to the site. Inside of the solution, you can have projects that correlate to each of your applications.naming the project
  3. Click “OK” button after you finished naming your solution and project.
  4. Automatically your Visual Studio will take you to a template of a unit test. It creates everything for you. You can rename the classes and the method names, but that’s not relevant for these purposes.4-template code

II. How to import appropriate assemblies into Visual Studio to run automated checks?

  1. In the “Solution Explorer” tab on the right side, right click on “References” and then select “Manage NuGet Packages”. You can think of NuGet packages as plugins or extensions that give you code to be able to perform certain actions. These have been developed by different individuals to solve a common problem. For example, use WebDriver APIs, use RESTful calls, work with a testing framework…2-1-opening nuget package manager
  2. Search for “selenium” in the upper right search bar of the NuGet online store. This will bring up a bunch of Selenium related NuGet packages for different purposes. We want 2 specific ones:
    1. Selenium WebDriver
    2. Selenium WebDriver Support Classes5-2-searching for selenium in nuget store
  3. Time Saving Tip: You all know how I love to save time and be more efficient so that I have more time to actually code and think. So, rather than downloading each of the packages 1 at a time, there is a trick! Just install “Selenium WebDriver Support Classes” package and that will automatically install “Selenium WebDriver” as well.2-3 installing the nuget packages5-3-2- installed packages
  4. Copy this code and paste it into your method

IWebDriver driver;
DesiredCapabilities capability = DesiredCapabilities.Firefox();
capability.SetCapability(“browserstack.user”, “YOUR USER NAME”);
capability.SetCapability(“browserstack.key”, “YOUR PASSWORD”);

driver = new RemoteWebDriver(
new Uri(“http://hub.browserstack.com/wd/hub/”), capability


III. How to refactor the code and get it to compile?

  1. Your C# file should look something like the image below. Please notice all of the red lines throughout the code. These are errors that will not allow your code to compile.3-1- automation testing code with errors
  2. The reason that these errors are occurring is because we don’t have the appropriate assemblies being used in this file. The code does not understand what is a “IWebDriver” class or what a “DesiredCapabilities” class is. In fact, if you hover over the errors, you can see a message like, “The type or namespace name ‘RemoteWebDriver’ could not be found (are you missing a using directive or an assembly reference?)”.
  3. To fix these errors in your test method is very easy. Place your cursor at the begging of the first error for the “IWebDriver” class. This class has a red highlight. On your keyboard, press “ctrl” + “.”  This will open the help from the IDE. The IDE provides you helpful suggestions to help and fix the errors.3-3- opening the IDE help
    1. Tip: Many individuals tend to just copy the “using” clauses from other code. However, I do not think that this is the most efficient manner. Sometimes you may copy extra statements that you later have to remove. And sometimes, your code will get into a weird state and you will spend time trying to understand the errors. So I prefer to let the IDE and the compiler solve my problems for me by using the “Ctrl .”.
  4. Select the very first option “Using OpenQA.Selenium”. You can also just hit enter to select that option. The first error has been resolved.3-4-automation testing fixed first error
  5. The final piece of code should look like the image below. automation testing - 3-5 final code with errors fixed

IV. Running the automated check and viewing results in BrowserStack

  1. Remember that we created a Unit Test project? What this means is that there is no .exe file to start execution. Therefore, you cannot run a test using the “Run” button. However, there are other ways to execute your automated check.
    1. Inside of the test, press “Ctrl R,T”. This means that you need to press “Ctrl R” first, keep holding the Ctrl key and then press the “T”. This will begin test execution.
    2. Alternatively, you can right click on the test and just press “Run Test”4-0-automation testing - running a test in VS
  2. Now that the test is running, log in to BrowserStack and view your sessions. A browser stack session looks like the image below. On the left you can see a unique run instance where all of your tests are stored. On the right, you can see the awesome test that we ran. The session information is on top. The steps of the test execution are below.4-1-automation testing results of browser stack

Tear Down ( aka Conclusion)

This basically wraps up our awesome tutorial where you learned how to set up an automated test using Selenium WebDriver, C#, and BrowserStack! You learned how to create a brand new solution in Visual Studio. Then, you learned how to resolve dependency issues using NuGet package manager. Afterward, you learned how to run a unit test from a C# Class Library and see the results in BrowserStack.

If you have any questions, comments or insults, just leave them below. We love to hear from our amazing community!

Author: Nikolay Advolodkin – a highly passionate Automation Engineer!

Pin It on Pinterest

Share This