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 broswers 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.
  • Mark Anderson

    i trad in VS2015 and get Cannot find a file named ‘c:Usersmark.andersonDocumentsVisual Studio 2015ProjectsSelenium TestSelenium TestbinDebugwebdriver.json’ or an embedded resource with the id ‘WebDriver.FirefoxPreferences’

    • Mark you may need to download the addtional WebDriver for firefox. Apologies I don’t have the link handy at this stage.

      • Mark Anderson

        thanks

        • Mark Apollogies, I am finally getting a chance to review your error message and taking a look into to today.

  • Lalit Kumar

    Hi Gary, I am confused to start a project type with C# using Selenium whether it should be a Console project/Library/Unit Testing. I have tried with Unit testing most of the things work however, I cannot open a console window in Unit Testing project using Console.Write(True); however the same opens in Console Project in that case I am unable to add [TestMethod] in console project. It could be because of Libraries as Unit test project has using Microsoft.VisualStudio.TestTools.UnitTesting; while console does not have this. Please suggest which project type should always be used in Selenium with C# along with difference if any. Thanks for help!

    • Lalit,
      You can use either approach it just entirely depends on how you would like to execute the project. The unit test project is ideal for executing unit test etc. The console project is more aligned with automating workflows etc, which is ideal for integration testing scenarios etc.

π