I. How to set up a Visual Studio unit test project for automation testing?
- 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.
- 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.
- 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.
- Click "OK" button after you finished naming your solution and project.
- 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.
II. How to import appropriate assemblies into Visual Studio to run automated checks?
- 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...
- 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:
- 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.
- Copy this code and paste it into your method
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?
- 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.
- 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?)".
- 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.
- 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 .".
- Select the very first option "Using OpenQA.Selenium". You can also just hit enter to select that option. The first error has been resolved.
- The final piece of code should look like the image below.
IV. Running the automated check and viewing results in BrowserStack
- 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.
- 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.
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!
Latest posts by Nikolay Advolodkin (see all)
- Best Online Resources to Learn Automation Testing - December 22, 2016
- Parallel Test Execution Using Selenium Webdriver - April 26, 2016
- Proper Black Box Test Case Design-part 2-Boundary Value Analysis - April 5, 2016