This article will demonstrate how to get WebDriver working with C# using Visual Studio.
Posted Feb 13, 2019
This article will demonstrate how to get WebDriver working with C# using Visual Studio.
Getting started with WebDriver’s C# bindings in Visual Studio
This article is one in a series showing how to get WebDriver working in various editors and language platforms.
For an overview of how WebDriver works, please see the section “WebDriver Overview” in the
To create and run WebDriver tests in C# using Visual Studio you’ll need the following components:
We expect the reader is using Visual Studio for work. If you don’t have Visual Studio yet, take heart. The non-commercial version
For this particular
Once you’ve got Visual Studio installed and started, the next step is to create the project to work on. As with all editors and toolsets, there are multiple ways to accomplish the same basic goal of getting a test project built. If you’re working in a team environment, please do your part to maintain sanity and a good team environment: follow the same approach the team uses!
Because this is a simple HOWTO guide, we’ll use the simplest approach: A Class Library project. Use File => New => Project.
If this is the first time you’ve used this installation of Visual Studio you’ll see a short delay while project templates are constructed. At this
Choose the C# class library (.NET Framework) option. (Discussing the .NET Standard and Core options are beyond the scope of this article.) Give your project a good name and click OK. Visual Studio will create the project and add one class file to it.
At this
Right-click the project, then select Manage NuGet Packages.
In the package management screen,
Once you have installed all three packages, click on “Installed.” Your results should look like this:
Your C# code will not talk to the browser directly. Instead, Webdriver will call a .exe file that will drive the browser automation. Download
Note
WebDriver doesn’t know how to do anything other than
Below is a complete test case that starts a browser locally, executes a very simple test, then closes out the browser instance. The example is extremely simple and doesn’t follow normal practices like using Page Object Patterns. This is example code, not production code!
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
namespace WebDriver_CSharp_Example
{
[TestFixture]
public class Chrome_Sample_test
{
private IWebDriver driver;
public string homeURL;
[Test(Description="Check SauceLabs Homepage for Login Link")]
public void Login_is_on_home_page() {
homeURL = "https://www.SauceLabs.com";
driver.Navigate().GoToUrl(homeURL);
WebDriverWait wait = new WebDriverWait(driver,
System.TimeSpan.FromSeconds(15));
wait.Until(driver =>
driver.FindElement(By.XPath("//a[@href='/beta/login']")));
IWebElement element =
driver.FindElement(By.XPath("//a[@href='/beta/login']")) ;
Assert.AreEqual("Sign In", element.GetAttribute("text"));
}
[TearDown]
public void TearDownTest()
{
driver.Close();
}
[SetUp]
public void SetupTest()
{
homeURL = "http://SauceLabs.com";
driver = new ChromeDriver();
}
}
}
When you first create or open a project, Visual Studio doesn’t know what tests are in it. You need to first populate the list of tests. Do this by opening Visual Studio’s Test Explorer via Tests => Windows => Test Explorer. Select Run All Tests to build the project then automatically discover and run all tests—in this case, one.
You’ll see (hopefully!) a green test in the Explorer window which means your test passed.
In this
Good luck with your explorations of WebDriver!