Get Started with Selenium on Visual Studio

Learn to Install & Download Selenium in Visual Studio and run Tests on Real devices for comprehensive testing

Get Started free
Home Guide How to set up Selenium on Visual Studio

How to set up Selenium on Visual Studio

By Ganesh Hegde, Community Contributor -

What is Visual Studio?

Visual Studio is an IDE developed by Microsoft. Its popularity comes from offering unique features such as cloud connectivity, supported extensions, easy debugging, collaboration, deployment, and version control integration, among others. Visual Studio is available for both Windows and Mac. It can be used for mobile, web, cloud, desktop applications, and gaming application development.

Visual Studio is available in both Free and Paid versions. The free version comes with basic features, whereas the paid version comes with enterprise features.

Note: Do not confuse Visual Studio with Visual Studio Code. Visual Studio is a complete IDE, and Visual Studio Code is an editor.

Benefits of Visual Studio

  • Easy to Install:  Visual studio is easy to install. It comes with a platform-specific installer. Simply follow the on-screen instructions during installation. 
  • Easy to Set up: Visual Studio comes with default templates. To create a new project, a user can choose from many available templates. The IDE will do the basic configuration to get it ready for usage.
  • Extendable: Visual Studio supports thousands of extensions. Just hook into the IDE and customize them.
  • IntelliSense: Visual Studio supports modern IntelliSense for many languages, which makes coding easier.
  • Version Control: Users can integrate version control tools like Git and easily manage them.
  • Live Share Support: Users can share their code in real-time which facilitates collaboration.
  • Testing: Visual Studio makes Test Driven Development easier with its built-in testing tools. Users can write unit and integration tests quickly.

Note: To integrate the BDD framework into Selenium tests, integrate with SpecFlow.

  • Cloud Support: Integrate this IDE with cloud providers such as Azure, making development and deployment more straightforward and faster.
  • Developer Friendly: Visual Studio offers various features for development such as easy refactoring, code clean up, etc.

Setting up Selenium and Visual Studio

Selenium is the most popular end-to-end automation testing tool in existence. It supports the most popular programming languages – C#, Java, Python, JavaScript, and many more.

To set up Selenium C# with Visual Studio, just follow the steps below:

  1. Install Visual Studio
  2. Create NUnit Project
  3. Add Selenium libraries

Install Visual Studio

Step 1: Navigate to Visual Studio Download Webpage and download the installer.

Note: The community edition is free. If someone has already paid for the license, they can install Visual Studio Professional or Enterprise Edition.

Step 2: Start the installer and choose the desired option. For Selenium users, C# Test, the Asp.Net, and web development should be good enough.

Install Visual Studio

Step 3: Wait for the installation to finish. The program may ask the user to restart the system. 

Step 4: After restart, choose the desired theme. After this, Visual Studio will begin its first time setup.

Create New Project

C#, Selenium, and NUnit comprise the most used combination for end-to-end automation tests.

Step 1: Click on Create new Project.
Create New Project in Visual Studio

Note: On the Start Screen of Visual Studio, create a new project by navigating menu File > New > Project.

Step 2: Click on Create NUnit Project, which emerges from the Create New Project pop-up.  Look for NUnit Project with .Net Core. Click on Next.

Create NUnit Project

Step 3: Configure your project. Specify project name, location, and solution name. Click Next to Continue.

Configure your project on Visual Studio

Step 4:  Choose the latest version of Target Framework. In this case, it’s .Net Core 5.0. Select that and click on Create.

Select Target Framework

The project setup is complete.

Add Selenium Libraries and Drivers

Having created the C# and NUnit Project, users need to add Selenium libraries and browser-specific drivers for End to End testing automation. 

Step 1: Add Selenium Webdriver NuGget package.

From the Visual Studio Tools menu, navigate to NuGet package manager and then click on Manage NuGet Packages for Solution.

The NuGet Solution Window opens up.

Step 2: Install Selenium Webdriver for the Project.

In the Nuget Solution window, search for and choose Selenium Webdriver package. Click on Install

Note: This NuGet Package may ask for license Acceptance. Read and Accept the License Agreement to proceed.

Manage Selenium package in visual studio

Step 3: Install Selenium Support Package.

Talk to an Expert

Like Step 2, search for Selenium Support and Install Selenium. Support Package for the project.

Step 4: Create a new folder from inside the project. Name folder as “drivers.”

Install Selenium Package in Visual Studio

 Step 5: Download the Selenium Browser-specific drivers. To execute Selenium end-to-end tests on the browser, download browser-specific drivers.

  • Chrome drivers can be downloaded from here. The Firefox driver (called Gecko driver) can be downloaded from here.

Step 6: Copy the downloaded driver .exe file into the newly created drivers folder.

Select Chromedriver and Geckodriver
After completion of all the above steps, the project configuration looks as below:
Project Configuration in Visual Studio

You are all set to write your first tests with Selenium and Visual Studio.

BrowserStack Automate Banner 1

Selenium C# Automation Best Practices

  • Use Page Object Model: The page object model (POM) is a popular design pattern for Selenium end-to-end tests. In this pattern, a separate class file will be created based on the web page or page component and utilized inside the test methods.
  • Avoid Assertions in Page Object: Do not use any assertions (Assert) inside the page object.
  • Create a Top-level class for Setup and Teardown: Create a Base class for commonly used operations across tests like creating driver instance, teardown, etc. Utilize that base class inside tests.
  • Create Utility Classes:  To use the same functions/methods for multiple tests, separate them and create them as utility classes. This helps avoid duplicate code.
  • Create Independent Tests: Do not write test cases dependent on one another. Independent tests help with parallelization, faster execution, and debugging.
  • Do not make test files complex: When creating test files, don’t create files with thousands of lines of code. Split them into smaller chunks which increases readability.
  • Avoid using Xpath Locator: There exist many locators available apart from XPath like Id, Name, LinkText, PartialLinkText, TagName, ClassName, CSSSelector. An intelligent locator strategy makes test maintenance easier.
  • Integrate with CI/CD Pipeline: It is always recommended to integrate an automation framework with CI/CD tools like Jenkins, Azure DevOps, etc., which makes your testing more manageable and on time.
  • Reporters: To share test reports with stakeholders, have reporting tools integrated into Selenium tests.

Bear in mind that  Selenium WebDriver tests must be executed on real devices and browsers. Remember that device fragmentation is a significant concern for every developer and tester. Every website has to work seamlessly on multiple device-browser-OS combinations. With 9000+ distinct devices being used to access the internet globally, all software has to be optimized for different configurations, viewports, and screen resolutions.

Run Selenium Tests for Free

In this state, no emulator or simulator can replicate real user conditions. Software needs to be tested on real devices to work in real-world circumstances such as a low battery, incoming calls, weak network strength, and so on. If an in-house lab is not accessible, opt for a cloud-based testing option that offers real devices.

BrowserStack’s cloud Selenium grid offers 3000+ real devices and browsers for automated testing. That means users can run tests on multiple real devices and browsers by simply signing up, logging in, and selecting the required combinations. Testers can also conduct Cypress testing on 30+ real browser versions across Windows and macOS. Detects bugs before users do by testing software in real user conditions with BrowserStack.

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

How to Run Visual Tests with Selenium: Tutorial

Selenium with C# : How to start running Automated Tests

Automation Tests on Real Devices & Browsers

Seamlessly Run Automation Tests on 3500+ real Devices & Browsers