Testing web apps in C# using Selenium WebDriver and MSTest | Applitools Documentation
Quickstart Guide for Visual Testing with Applitools in C# using Selenium WebDriver
This quickstart will show you how to visually test web apps in C# using Selenium WebDriver and MSTest. Visual testing can help you catch problems that traditional automation struggles to find.
After preparing your environment, this three-step quickstart should take about 15 minutes to complete.
Step 1: Preparing your environment
You'll need a few things to run this quickstart:
- An Applitools account, which you can register for free.
- The .NET 7 SDK for building and running the project.
- A good C# editor:
- Use Microsoft Visual Studio if you want to drive development through a full IDE. Visual Studio comes bundled with the .NET SDK and all the tools you need out of the box. However, you will need to add a
.runsettingsfile or overwrite some variables in the code to run the tests through Visual Studio. - Use Visual Studio Code if you want a faster experience driven more by the command line. You will need to install the C# extension and use the
dotnetCLI tool from the terminal. You will also need to install the .NET SDK separately. Setting environment variables to run tests will be easier.
- Use Microsoft Visual Studio if you want to drive development through a full IDE. Visual Studio comes bundled with the .NET SDK and all the tools you need out of the box. However, you will need to add a
- An up-to-date version of Google Chrome.
- A corresponding version of ChromeDriver.
Installing ChromeDriver
The major version numbers of Chrome and ChromeDriver must be the same. Otherwise, Selenium WebDriver may raise an exception when it is initialized. For example, Chrome v101 requires ChromeDriver v101. ChromeDriver must be installed into a directory covered by the system PATH variable.
Step 2: Getting your example project
Downloading the example project
The example project is located at https://github.com/applitools/example-selenium-csharp-mstest. Clone this repository to your local machine:
git clone https://github.com/applitools/example-selenium-csharp-mstest.git
cd example-selenium-csharp-mstest
If you don't have Git, instead of running git clone, you can download the project as a ZIP file and extract it.
Installing the dependencies
The example project uses NuGet for package management. All dependencies are listed in the project's .csproj file. They should be automatically downloaded whenever the project is built. You can explicitly restore NuGet packages from either Visual Studio or the command line by following this guide: Restore packages with NuGet Package Restore.
Adding Applitools to Another .NET Project? This example project already has the Applitools Eyes SDK as a dependency. There are two NuGet packages for the Applitools Eyes SDK for different Selenium versions:
Eyes.Selenium4uses Selenium 4Eyes.Seleniumuses Selenium 3
Deciding how to run tests
The test uses Selenium WebDriver to automate the browser. There are two places to run your Selenium WebDriver session:
- On your local machine (which is the default option)
- On Applitools Execution Cloud
If you run WebDriver locally, then you need to set it up and manage it yourself. If you use Execution Cloud, then Applitools will manage WebDriver for you. It will also automatically heal broken locators and wait for elements to be ready.
Walking through the code
The project contains one visual test case, which is located at Applitools.Example.Tests/AcmeBankTest.cs.
using Applitools.Selenium;
using Applitools.VisualGrid;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System.Drawing;
using ScreenOrientation = Applitools.VisualGrid.ScreenOrientation;
namespace Applitools.Example.Tests
{
/// <summary>
/// Tests for the ACME Bank demo app.
/// </summary>
[TestClass]
public class AcmeBankTest
{
#pragma warning disable CS0162
#pragma warning disable CS8618
// Test constants
public const bool UseUltrafastGrid = true;
public const bool UseExecutionCloud = false;
// Context for the current test (set by MSTest during runtime)
public TestContext TestContext { get; set; }
// Test control inputs to read once and share for all tests
private static string? ApplitoolsApiKey;
private static bool Headless;
// Applitools objects to share for all tests
private static BatchInfo Batch;
private static Configuration Config;
private static EyesRunner Runner;
// Test-specific objects
private WebDriver Driver;
private Eyes Eyes;
#pragma warning restore CS8618
/// <summary>
/// Sets up the configuration for running visual tests.
/// <summary>
[AssemblyInitialize]
public static void SetUpConfigAndRunner(TestContext context)
{
ApplitoolsApiKey = Environment.GetEnvironmentVariable("APPLITOOLS_API_KEY");
Headless = Environment.GetEnvironmentVariable("HEADLESS")?.ToLower() == "true";
Runner = UseUltrafastGrid ? new VisualGridRunner(new RunnerOptions().TestConcurrency(5)) : new ClassicRunner();
Batch = new BatchInfo($"Example: Selenium C# MSTest with the {(UseUltrafastGrid ? "Ultrafast Grid" : "Classic Runner")}");
Config = new Configuration();
Config.SetApiKey(ApplitoolsApiKey);
Config.SetBatch(Batch);
if (UseUltrafastGrid)
{
Config.AddBrowser(800, 600, BrowserType.CHROME);
Config.AddBrowser(1600, 1200, BrowserType.FIREFOX);
Config.AddBrowser(1024, 768, BrowserType.SAFARI);
Config.AddDeviceEmulation(DeviceName.Pixel_2, ScreenOrientation.Portrait);
Config.AddDeviceEmulation(DeviceName.Nexus_10, ScreenOrientation.Landscape);
}
}
/// <summary>
/// Sets up each test with its own ChromeDriver and Applitools Eyes objects.
/// <summary>
[TestInitialize]
public void OpenBrowserAndEyes()
{
ChromeOptions options = new ChromeOptions();
if (Headless) options.AddArgument("headless");
Driver = UseExecutionCloud ? new RemoteWebDriver(new Uri(Eyes.GetExecutionCloudUrl()), options) : new ChromeDriver(options);
Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
Eyes = new Eyes(Runner);
Eyes.SetConfiguration(Config);
Eyes.SaveNewTests = true;
Eyes.Open(
Driver,
"ACME Bank Web App",
TestContext.TestName,
new Size(1200, 600)
);
}
/// <summary>
/// This test covers login for the Applitools demo site, which is a dummy banking app.
/// <summary>
[TestMethod]
public void LogIntoBankAccount()
{
Driver.Navigate().GoToUrl("https://demo.applitools.com");
Eyes.Check(Target.Window().Fully().WithName("Login page"));
Driver.FindElement(By.Id("username")).SendKeys("applibot");
Driver.FindElement(By.Id("password")).SendKeys("I<3VisualTests");
Driver.FindElement(By.Id("log-in")).Click();
Eyes.Check(Target.Window().Fully().WithName("Main page").Layout());
}
/// <summary>
/// Concludes the test by quitting the browser and closing Eyes.
/// <summary>
[TestCleanup]
public void CleanUpTest()
{
Eyes.Close();
Driver.Quit();
}
/// <summary>
/// Prints the final summary report for the test suite.
/// <summary>
[AssemblyCleanup]
public static void PrintResults()
{
TestResultsSummary allTestResults = Runner.GetAllTestResults();
Console.WriteLine(allTestResults);
}
#pragma warning restore CS0162
}
}
Step 3: Running your tests
Setting Applitools variables
Before running the visual test, you must find your Applitools API key and set it as an environment variable named APPLITOOLS_API_KEY.
If you intend to run your tests from the command line (or Visual Studio Code), then set the environment variable like this:
- macOS and Linux
export APPLITOOLS_API_KEY=<your-api-key>
- Windows
set APPLITOOLS_API_KEY=<your-api-key>
Launching visual tests
To run tests from the command line (or Visual Studio Code's terminal), run:
dotnet build
dotnet test
After your tests run, you should see results in the Eyes Test Manager.
Resources for next steps:
Congratulations on completing this quickstart! There's still so much to learn about visual testing with Applitools, but you're off to a great start.