ScreenshotNeo

BlogHow-to

MSTest Tutorial: Set Up a Selenium Testing Environment

Set up a C# Selenium WebDriver test with MSTest, choose a test platform, run a browser smoke test, and troubleshoot common setup issues.

By the ScreenshotNeo team4 October 20267 min read

This guide sets up a C# test project that uses MSTest attributes and Selenium WebDriver to open a real browser, submit Selenium’s example web form, and check the result. For a new MSTest project, the recommended setup is MSTest.Sdk. The runnable example below uses its default MSTest runner and Microsoft.Testing.Platform (MTP). If your repository already uses VSTest, use the package-based alternative described below and keep the project and tooling on a consistent platform.

1. Choose the project and test platform

MSTest is the test framework: it supplies attributes such as [TestClass] and [TestMethod] and assertion methods. Selenium WebDriver is the browser automation library. The test platform discovers and runs tests. These are related but separate choices.

Microsoft recommends MSTest.Sdk for new MSTest projects. It uses MSTest runner with MTP by default, and can be run with dotnet test or Visual Studio. Microsoft also documents a package-based MSTest setup. Selenium’s example project uses that package pattern, with VSTest components. Avoid combining project instructions that assume different runners; choose a platform that fits your repository, IDE, and CI tooling. See Microsoft’s MSTest getting-started guide and its test platform comparison.

Setup Use it when Runner notes
MSTest.Sdk You are creating a new MSTest project and want Microsoft’s recommended project SDK. MTP and MSTest runner are the defaults.
MSTest NuGet packages You need the package-based project pattern, such as a VSTest-oriented repository. Include the framework, adapter, and test SDK packages and align IDE and CI configuration.

2. Create a new MSTest project

The example targets .NET 10 and uses MSTest.Sdk 4.1.0 as shown in Microsoft’s getting-started documentation. Package and SDK versions change; check the official instructions and your repository’s supported .NET versions when choosing versions.

dotnet new classlib -n SeleniumMSTest
cd SeleniumMSTest
dotnet add package Selenium.WebDriver

Replace the generated project file with this SDK-style project file. It enables the MSTest SDK and adds Selenium WebDriver:

<Project Sdk="MSTest.Sdk/4.1.0">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Selenium.WebDriver" Version="4.49.0" />
  </ItemGroup>
</Project>

The Selenium version matches the version displayed in the official example project dossier; treat it as an example, not a guarantee that it is the newest version or compatible with every SDK and browser on your machine. The official Selenium .NET instructions install the library with dotnet add package Selenium.WebDriver. See Selenium’s .NET library installation guide.

The Selenium .NET examples list .NET SDK 8.0 or later for running the test suite. This sample uses .NET 10 because that is the target in Microsoft’s documented MSTest.Sdk example; .NET 10 is not a Selenium test-suite minimum. Selenium’s separate file-based script instructions requiring .NET 10 or later do not set the minimum for a test project. Keep the target framework aligned with your installed SDK and your organization’s support policy.

3. Add a browser smoke test

Create WebFormTests.cs. This follows Selenium’s official C# web-form example: open the form, verify its title, enter text, submit, and assert the confirmation. The finally block quits the browser even if navigation or an assertion fails.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumMSTest;

[TestClass]
public class WebFormTests
{
    [TestMethod]
    public void SubmitForm_displaysConfirmation()
    {
        IWebDriver driver = new ChromeDriver();

        try
        {
            driver.Navigate().GoToUrl(
                "https://www.selenium.dev/selenium/web/web-form.html");

            Assert.AreEqual("Web form", driver.Title);

            driver.FindElement(By.Name("my-text")).SendKeys("Selenium");
            driver.FindElement(By.TagName("button")).Click();

            string message = driver.FindElement(By.Id("message")).Text;
            Assert.AreEqual("Received!", message);
        }
        finally
        {
            driver.Quit();
        }
    }
}

ChromeDriver starts Chrome through Selenium’s driver management. The local environment still needs a compatible browser setup; the example documentation does not guarantee that every machine has Chrome or a usable driver available. If your team uses another browser, use its corresponding Selenium driver and make the browser choice explicit in CI.

4. Restore, build, and run the test

From the project directory, restore dependencies and run the test suite:

dotnet restore
dotnet test

To run just this test, use a fully qualified name filter supported by the Selenium .NET examples:

dotnet test --filter "FullyQualifiedName~SeleniumMSTest.WebFormTests.SubmitForm_displaysConfirmation"

Run these commands from the directory containing the project file, or pass the project path to dotnet test. The example is a browser smoke test, so a successful build alone does not prove that browser startup, page loading, and form submission work in the runtime environment.

5. Package-based alternative for VSTest projects

If your repository uses VSTest and package-based MSTest projects, use the NuGet packages as a coherent set. Do not add these packages on top of the MSTest.Sdk setup above without a specific reason. Selenium’s example project includes the following packages:

dotnet new classlib -n SeleniumMSTest
cd SeleniumMSTest
dotnet add package Microsoft.NET.Test.Sdk
dotnet add package MSTest.TestAdapter
dotnet add package MSTest.TestFramework
dotnet add package Selenium.WebDriver
dotnet add package Selenium.Support

The official example project displays Microsoft.NET.Test.Sdk 17.11.1, MSTest adapter and framework 3.6.0, and Selenium packages 4.49.0. These observed values are examples, not a promise of latest versions or cross-version compatibility. Use versions supported by your project and restore them together. The Selenium Support package is included in its example project; this minimal smoke test does not require a helper from it. The test class above can be used with the package-based setup.

6. Common setup errors and fixes

Symptom Likely cause Fix
dotnet test reports no tests found. The selected test platform and project packages or SDK do not match, or the test class/method is missing MSTest attributes. Confirm the project uses one setup path: MSTest.Sdk with its runner defaults, or the package-based setup with the adapter and test SDK. Check for [TestClass] and [TestMethod], then restore and build.
The compiler cannot find TestClass or TestMethod. The MSTest framework is absent, restore failed, or the namespace is missing. Use the SDK or add MSTest.TestFramework for the package setup, restore packages, and include using Microsoft.VisualStudio.TestTools.UnitTesting;.
The compiler cannot find ChromeDriver. Selenium WebDriver is missing or the Selenium namespace is not imported. Add Selenium.WebDriver, restore, and include using OpenQA.Selenium.Chrome;.
Chrome fails to start or Selenium reports a driver/browser error. Chrome may not be installed, may be unavailable in the execution environment, or the local browser and driver setup may be incompatible. Install or provision the intended browser in the machine or CI image, check the browser setup and Selenium diagnostics, and use the driver for that browser. Do not assume a developer workstation’s browser is present on a CI agent.
The page title or confirmation assertion fails. The page did not load as expected, the remote page changed, or a transient network condition interrupted the smoke test. Inspect the actual title and page state, confirm the URL is reachable from the runner, and distinguish a site/network failure from a test assertion failure.
A later test run finds Chrome still open. Driver cleanup did not execute after an exception. Keep driver.Quit() in finally or use an MSTest cleanup lifecycle method. Avoid relying on cleanup placed only after assertions.
IDE, local CLI, and CI behave differently. They may use different SDKs, test platforms, browser installations, or project settings. Align the SDK and platform across the repository’s .NET test projects and tooling. Reproduce CI’s browser provisioning and run the same dotnet test command locally.

7. Reliability, performance, and cost

A real-browser smoke test covers browser startup and an end-to-end interaction, but it depends on the browser installation, machine resources, network, and remote page. Keep the test small, always quit the driver, and avoid adding unrelated remote dependencies to the same smoke test. For repeatable CI runs, provision the browser deliberately and keep local, IDE, and CI platform choices consistent.

Browser startup and remote page loading add work compared with tests that do not launch a browser. This example makes one browser session and one form submission; it does not establish a benchmark or runtime guarantee. When a suite grows, consider which tests need a real browser and keep simple logic checks out of browser tests. Costs depend on your own CI and browser infrastructure; this tutorial does not assume a hosted testing provider or quote a service price.

Or skip the browser setup

If the goal is to capture a website image or PDF rather than validate browser interactions, ScreenshotNeo provides a website screenshot API and MCP server. This GET request saves a WebP screenshot. See the ScreenshotNeo API documentation for parameters and response details.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Cookie banners, popups, and chat widgets are removed before the shot. Bot checks, blank pages, and failed loads are never billed. An MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Create a free ScreenshotNeo account.

FAQ

Can I use Visual Studio or Visual Studio Code?

Yes. The Selenium .NET examples list Visual Studio and Visual Studio Code as IDE examples. Keep the project’s test platform compatible with the IDE and the rest of the repository.

Does this test need .NET 10?

This sample targets .NET 10 to match Microsoft’s documented MSTest.Sdk example. Selenium’s .NET test-suite instructions list SDK 8.0 or later; choose a target framework supported by your project and installed SDK.

Is Selenium.Support required for this test?

No helper from Selenium.Support is used in the sample. Selenium’s example project includes it, so add it when your project uses its APIs.

Does this test prove my application works across browsers?

No. It exercises one browser configuration. To cover other browsers, configure their Selenium drivers and run the test in environments where those browsers are available.