Selenium WebDriver & Visual Studio





Selenium is a suite of browser automation tools to help with automating web browsers across a number of platforms. It’s a really handy tool to help with automating web applications for testing purposes.

Most of the well known browsers support it and have taken steps to make Selenium a native component of their browsers. The core Selenium technology is used across other browser automation tools , Api’s and frameworks.

In this guide I’ll provide instructions on how to set up Selenium Web Driver using Microsoft Visual Studio and an example of how to write a quick test.

The setup process is pretty much the same, across most versions of Visual Studio i.e. Visual Studio 2013, Visual Studio 2015 and Visual Studio 2017.

For Visual Studio 2013, I recommend ensuring you have Update 5 isntalled.

I prefer to use nUnit for my Selenium Testing, you may want to install the nUnit VS Templates .

Selenium WebDriver browser automation testing

The WebDriver is a tool  for automating the testing of web applications. It assists in the running of web browsers natively as a user would either locally or on a remote machine using the Selenium Server. It offers support for dynamic web pages and supports AJAX.

The Selenium WebDriver uses each browsers native support for automation and is programming language dependent.  Microsoft C# bindings are distributed as part of a set of signed dlls and dependency dlls.

The first step would be to start Microsoft Visual Studio and create a new Visual C# Unit Test Project. newprojOnce the solution opens right click on the Project file in the Solution Explorer Window and Select Manage Nuget Packages…

nugetConduct a search using Online and the search term Selenium the options for both the Selenium WebDriver and Selenium WebDriver Support Classes should appear. Ensure you install both these packages to your project.

nuget addBoth the Internet Explorer and Firefox driver are included in the Selenium, however we will need to download an additional web driver for Chrome  . Once the web driver for chrome has been downloaded extract the executable file to a folder in your solution .

I created a folder called dependencies and added the executable there

dependenciesRight click on the chromedriver.exe and select Properties

Ensure the Build Action Content is selected  Copy to Output Directory Copy Always has been selected.  This will ensure that chromedriver.exe is always in the folder of the running assembly so it can be used.

propertiesOnce all the set up is complete we can now begin writing a simple test check everything is working as expected.  In my example I am going to test that a user is redirected to a login page if they select the Login link on the homepage of the threenine.co.uk.


Example of how to use Selenium for automated browser testing

We’ll create a simple ThreeNineLoginTest.cs unit test class and import the Selenium Namespaces required

We’ll now create a simple unit test

If we run the test,  we’ll see the chrome browser launch and the website should be loaded, the new chrome window should be maximized and then the Login link should be click and the user should be redirected to the login page.  We’ll carry out a simple assert to determine if the Browser URL is inded the URL of the login page.  The test should pass.

When in the Visual Studio IDE, I prefer to run all my tests using Resharper Productivity Tool for Visual Studio, but we can now also equally run these tests using the Selenium Server too.



Creating and Editing Test Cases Using Selenium IDE

You can develop Test Cases using the Selenium IDE, a Firefox plugin with a COntext Menu and Selenium command options that help with learning the Selenium Script Syntax.

Selenium IDE is an integrated development environment for Selenium scripts. It is implemented as a Firefox extension, and allows you to record, edit, and debug tests. Selenium IDE includes the entire Selenium Core, allowing you to easily and quickly record and play back tests in the actual environment that they will run in.

Once you’ve installed Selenium IDE in Firefox, it’s available in the Firefox Tools menu. Selecting it will open a new, empty script-editing window as well as a menu for loading or creating test cases.

After installation of the Selenium IDE , it’s available in the Firefox Tools menu.

Selecting it will open a new, empty script-editing window as well as a menu for loading or creating test cases. From there, you can use one of three methods to build test cases:

  • Record test cases: Record a test case based on website interaction. During recording, Selenium automatically inserts commands based on your actions, such as clicking a link , entering values , selecting options from a drop-down menu , or clicking check boxes or radio buttons.
  • Adding verifications and asserts: check the properties of a web page using the assert and verify.
  • Editing: Edit test cases and manually insert or edit commands and comments.

Executing Test Cases in Selenium IDE

  • Running a test case: Execute the currently displayed test case.
    Running a test suite: Execute all test cases in the test suite currently loaded.
  • Stop and start: Use the “Pause” button to stop a test case currently running.
  • Stop in the middle: This option enables you to set a specific breakpoint that stops a running test at a particular command. Setting a breakpoint is easy; just select a command, right-click, and then choose “Toggle Breakpoint” from the context menu.
  • Start from the middle: Similar to stopping a test at a certain command, you can also set a test to begin running from a specific command. The process for setting this point is the same, but instead of choosing “Toggle Breakpoint,” you’ll select “Set/Clear Start Point” from the context menu.
  • Running a single command: Double-click on any command to run it by itself, a useful tool for testing a command while you’re constructing it.

Summary

Selenium is a powerful tool to automate your mundane browser testing tasks. Mastering the basics and you’ll seen find more ways to use the tool to improve your development and testing processes.

There are many more advanced features and functionality, but the ease of use and simplicity of the tooling makes it very easy and appealing for new users to to pick up and master very quickly.

A useful resource for learning about the advanced capabilities of Selenium is teh Selenium Wiki on GitHub .

Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

Affiliate Disclaimer

Disclosure: Please note that some of the links included in the blog posts are affiliate links, which means I will earn a commission if you decide to make a purchase.

I only include affiliate links to products, services and companies that I have personal experience and have actually used. I only recommend them because they are helpful and useful, not because of the small commissions I make if you decide to buy something.

Please do not spend any money on these products unless you feel they are relevant, suitable and will help you achieve your goals.

Buying anyone of these products and the commisions earned will not make me an overnite multi millionaire, but they will help to cover the hosting costs and compensate for the countless hours and effort I put in to create meaningful and valuable free content I provide to help others succeed.

You've also undoubtedly noticed that I also use various advertising networks - unless of of course you're using an Ad blocker, this is also an attempt to reduce my monthly deficit I experience in providing free relevant, valuable and on occassion insightful content for the benefit of others.

I only really make some money if people click on the ads, unless of course you consider 1c per 1000 impressions real money!

Bear in mind just how many impressions I need to make before I can cover my £20 hosting costs a month!

If you are using an adblocker and guilt stricken you can of course donate using any of the buttons below, but I won't be holding my breath.


Buy me a coffeeBuy me a coffee
π