Introduction: What Is Selenium WebDriver?
Selenium WebDriver is a tool used for automating Web application testing. WebDriver is the successor to Selenium RC, which accepts commands and sends them to a browser. WebDriver includes a collection of open source APIs that support browsers like Firefox, Chrome, IE, and Safari. Selenium WebDriver APIs are platform independent and support different programming languages such as C#, Java, Perl, PHP, and Ruby.
In this tutorial, I will provide instructions on how to set up Selenium WebDriver using Microsoft Visual Studio and give you an example to explain how to write a quick test using WebDriver.
How to Integrate Selenium WebDriver with Visual Studio
The Selenium WebDriver setup process is pretty much the same across different versions of Visual Studio: Visual Studio 2013, Visual Studio 2015, and Visual Studio 2017.
Follow these steps to integrate Selenium with Visual Studio.
Step 1
Open Visual Studio and Create a new project, as shown in Figure 1.
Figure 1: Visual Studio New Project
Step 2
Click the test project and name it Selenium WebDev Testing (see Figure 2).
Figure 2: Visual Studio Unit Test Project
Step 3
Next, after creating a new test project, we have to add the Selenium APIs to the Visual Studio project. A developer can either download Selenium WebDriver by clicking this link, or they can add the references from the NuGet package. To add the WebDriver reference from the NuGet package, open Solution Explorer -> expand the project -> Right-click references and select the ‘Manage NuGet Packages’ option that’s highlighted in Figure 3.
Figure 3: Visual Studio Manage NuGet Package
You will see the screen shown in Figure 4. Next, Search Selenium WebDriver -> Select Selenium.webdriver and click Install.
After successfully installing you will see the screen pictured in Figure 4.
Figure 4: Visual Studio Selenium.WebDriver Installed
You also can install the Selenium.Support if required. Refer to Figure 5.
Figure 5: Visual Studio Selenium.Support Installed
Now, verify the in WebDriver references added in your project from Solution Explorer.
Figure 6: Visual Studio Solution Explorer WebDriver
Testing Automation with Selenium WebDriver
Once you complete the WebDriver initial setup, go to the Solution Explorer window and right-click Program.cs and select Rename (see Figure 7). The new name will start reflecting in the project and code window.
Figure 7: Visual Studio Solution Rename File
Open the TestSelenium.cs file that was renamed in Step 3 and add the following code to test from the Chrome and Firefox browsers.
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Support.UI; namespace SeleniumWebDevTesting { [TestClass] public class TestSelenium { static IWebDriver driverFF; static IWebDriver driverGC; [AssemblyInitialize] public static void SetUp (TestContext context) { driverFF = new FirefoxDriver(); driverGC = new ChromeDriver(); } [TestMethod] public void TestChromeDriver() { driverGC.Navigate().GoToUrl("http://www.google.com/"); driverGC.FindElement(By.Id("lst-ib")) .SendKeys("Tapas Pal Codeguru"); driverGC.FindElement(By.Id("lst-ib")) .SendKeys(Keys.Enter); } [TestMethod] public void TestFireFoxDriver() { driverGC.Navigate().GoToUrl("http://www.google.com/"); driverGC.FindElement(By.Id("lst-ib")) .SendKeys("Tapas Pal Codeguru"); driverGC.FindElement(By.Id("lst-ib")) .SendKeys(Keys.Enter); } } }
The following references are added to provide all classes and properties required to integrate the Selenium WebDrivers.
using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Support.UI;
WebDriver works on the specific Web browser. Remember to download the chromedriver on your local machine. For the Firefox browser, you also have to download the correct WebDrivers version.
Now, run the test cases to see the output in a browser, as shown in Figure 8.
Figure 8: Visual Studio Solution Run Test Method
You will see the Chrome and Firefox browsers opened with searching criteria provided in the code (see Figure 9).
Figure 9: Browser Execution Result
Conclusion
Selenium WebDriver is a powerful tool to automate your mundane browser testing tasks. Selenium WebDriver makes direct calls to the browser by using each browser’s native support for automation. These direct calls are made and the features they support depends on the browser. I hope this article has provided a basic understanding about Selenium WebDriver automation. That’s all for now; happy reading!