Getting started with Selenium and C# is easy if you have general knowledge about connecting the right blocks for test code development. In one of the earlier blogs, we covered the Selenium WebDriver architecture in great detail. This is chapter 2 of the series where we are going to help you set up Selenium in Visual Studio for automated browser testing of your web-application. In chapter 1 we talked about the overview of Selenium C# tutorial series. We also covered why Selenium C# is a good call for test automation. Selenium C# tutorial In this chapter, we are going to get down to business and run your first Selenium C# test automation script. Getting Started with Visual Studio 2019 Visual Studio is the best IDE (Integrated Development Environment) to use for Selenium C# test automation. In this Selenium C# tutorial, we will use the latest version of Visual Studio i.e. (VS 2019). There are a number of download options available with VS 2019. The non-commercial version is , whereas the commercial versions are & . For the demonstration in this Selenium C# tutorial series, we would make use of the Community Edition of Visual Studio 2019 for Selenium test automation. Visual Studio Community Visual Studio Professional Visual Studio Enterprise Install the necessary packages for Selenium C# test automation in your operating system. The license of the Community Edition expires after 30 days of usage. To extend the license, you should sign-in to the IDE. Signing-in also lets you use the other powerful features of Visual Studio such as pushing source code to private Git, syncing Visual Studio settings, and more. Note: Downloading & Installing Selenium WebDriver Selenium WebDriver is one of the most popular open-source test automation framework used for automating web application testing. Selenium WebDriver helps to greatly reduce the efforts involved in cross-browser testing by automating the test scripts. Now, let’s install the Selenium WebDriver for the browser under test on your operating system. In this Selenium C# tutorial, we will make use of the Google Chrome for automation testing with Selenium in Visual Studio. It is recommended to install the Selenium WebDriver executable in the location where the Google Chrome browser is present. That way, you need not mention the location of the Selenium WebDriver when invoking the same in your test implementation. Note: Creating our First NUnit Project for Selenium C# in Visual Studio Now, you have installed Visual Studio 2019 along with the Selenium WebDriver for Google Chrome. You are all set to execute your first Selenium C# testing script through the NUnit framework. NUnit is a test automation framework that helps to perform Selenium C# testing with much ease. In this chapter of the Selenium C# tutorial series, we will touch upon basic aspects of NUnit such as the installation of the framework along with installing the Selenium WebDriver for the test project. We will talk more about NUnit as we go forward. For now, let get your first Selenium C# example script running through NUnit. You will need to create an NUnit project for running an automation script using Selenium in Visual Studio. Here are the steps to create an NUnit project for Selenium C# development: Create a new project of the type in Visual Studio. Step 1: ‘NUnit Test Project (.Net Core)’ Give an appropriate name to the project and click Step 2: Create Since the project is of the type , the newly created .cs file will contain the basic functionalities of the NUnit framework. Step 3: NUnit (.Net Core) Install the Selenium WebDriver (for Google Chrome) and NUnit framework. Execute the appropriate Package Manager(PM) commands to install the necessary packages. Step 4: To execute PM commands from the PM console, navigate to . Tools -> NuGet Package Manager -> Package Manager Console Execute these commands on the PM console to install Selenium in Visual Studio: Install-Package Selenium Install-Package Selenium .WebDriver .Chrome .WebDriver Similarly, execute the following commands on the PM console to install NUnit: - NUnit - NUnit3TestAdapter Install Package Install Package Below are some of the screenshots of the execution: To verify the status of the package installation, execute the command on the PM console. Step 5: Get-Package PM> Get-Package Id Versions -- -------- Selenium.WebDriver { } Selenium.Chrome.WebDriver { } ...... ...... ...... ...... 3.141 .0 79.0 .0 Writing Your First Selenium C# Test Automation In this section of Selenium C# tutorial, we look at the following test scenario: Navigate to the URL .Locate the search-box on the page. https://www.google.com Enter the search item as LambdaTest and click Google Search. We are using a basic test scenario that uses the bare minimum annotations of the NUnit framework. It is just to get you started with Selenium test automation using a test framework like NUnit. NUnit.Framework; OpenQA.Selenium; OpenQA.Selenium.Chrome; System; { { String test_url = ; IWebDriver driver; [ ] { driver = ChromeDriver(); driver.Manage().Window.Maximize(); } [ ] { driver.Url = test_url; System.Threading.Thread.Sleep( ); IWebElement searchText = driver.FindElement(By.CssSelector( )); searchText.SendKeys( ); IWebElement searchButton = driver.FindElement(By.XPath( )); searchButton.Click(); System.Threading.Thread.Sleep( ); Console.WriteLine( ); } [ ] { driver.Quit(); } } } using using using using namespace Selenium_Demo class Selenium_Demo "https://www.google.com" SetUp ( ) public void start_Browser // Local Selenium WebDriver new Test ( ) public void test_search 2000 "[name = 'q']" "LambdaTest" "//div[@class='FPdoLc tfB0Bf']//input[@name='btnK']" 6000 "Test Passed" TearDown ( ) public void close_Browser As seen in the above implementation, the Chrome WebDriver is initiated in the code implemented under the [SetUp] attribute. [ ] { driver = ChromeDriver(); driver.Manage().Window.Maximize(); } SetUp ( ) public void start_Browser // Local Selenium WebDriver new The test implementation is under the [Test] attribute. The necessary web elements are located using the Inspect Tool in Google Chrome. [ ] { driver.Url = test_url; System.Threading.Thread.Sleep( ); IWebElement searchText = driver.FindElement(By.CssSelector( )); searchText.SendKeys( ); ...................................... ...................................... } Test ( ) public void test_search 2000 "[name = 'q']" "LambdaTest" Now, you can execute the test using the IDE. The status of the tests can be seen using the Test Explorer in Visual Studio. For accessing Test Explorer, go to . View? Test Explorer To build the project and execute all the tests, go to . On successful execution, you would see a Green test in the Test Explorer window. Test ? Run All Tests Stay Tuned For Upcoming Chapters In this article of the continuing Selenium C# tutorial series, we looked at the basic aspects like installing Selenium in Visual Studio along with running the first Selenium C# testing script. The test case using the NUnit framework gave a complete end-to-end demonstration. I will see you in the next tutorial of this Selenium C# tutorial series where we’ll run our first Selenium C# script with NUnit. Let’s Automate!!! Previously published at https://www.lambdatest.com/blog/setting-up-selenium-in-visual-studio/