Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk6 min

MSTest Tutorial: Set Up Selenium WebDriver in C#

Set up a C# MSTest project with Selenium WebDriver, run a browser-based web-form smoke test, and troubleshoot runner and Chrome setup issues.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To run a Selenium smoke test with MSTest, create a .NET test project, add Selenium WebDriver and an MSTest test runner, then write a test that opens a browser and checks a page. This guide uses Microsoft’s recommended MSTest.Sdk project setup and Selenium’s official web-form example. The two frameworks have different jobs: MSTest discovers and runs the test; Selenium drives the browser.

Choose the test project and runner first

MSTest is the test framework, while the test platform is the machinery that discovers and executes tests. Keep the project configuration consistent with the runner used by your repository, IDE, and CI. Microsoft recommends MSTest.Sdk for new MSTest projects; it uses MSTest Runner with Microsoft.Testing.Platform (MTP) by default. Microsoft also documents a package-based alternative that integrates with VSTest. These are different project patterns, so do not combine their setup instructions indiscriminately. See Microsoft’s MSTest getting-started guide and its test platform comparison.

Recommended here: MSTest.Sdk and MTP

The project below uses MSTest.Sdk, a concise MSBuild project SDK intended for MSTest projects. Microsoft’s guide illustrates version 4.1.0 targeting net10.0; treat those as documented example values, not a requirement to use those exact versions. For a new project, use a target framework and SDK supported by your installed .NET tooling, and check current package and SDK guidance if you need a specific target.

When a package-based VSTest project fits better

If your repository and tooling are already based on VSTest, use Microsoft’s package-based MSTest setup rather than grafting the SDK-style runner configuration onto it. That route uses a NuGet meta-package or its components, including the framework, adapter, analyzers, and VSTest integration. Selenium’s example project lists MSTest.TestAdapter, MSTest.TestFramework, and Microsoft.NET.Test.Sdk. The versions shown in that example are not a guarantee of the latest releases or compatibility with every combination; select compatible versions together.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Create the project and add Selenium

Selenium’s .NET examples list .NET SDK 8.0 or later to run the test suite. This is distinct from Selenium’s file-based script instructions, which require .NET 10 or later; this tutorial creates a normal test project, not a file-based script. Install a compatible .NET SDK before starting.

  1. Create a directory and a test project using the MSTest SDK template: dotnet new mstest -n SeleniumSmoke

  2. Move into the project directory: cd SeleniumSmoke

  3. Add Selenium WebDriver: dotnet add package Selenium.WebDriver. The official Selenium example project also includes Selenium.Support; add it if your tests use its helpers: dotnet add package Selenium.Support.

  4. Restore dependencies: dotnet restore.

For this SDK-based path, do not also add the VSTest adapter and test SDK simply because they appear in Selenium’s separate package-based sample. Follow the setup that matches the project runner you chose. Selenium’s installation instructions are at Install a Selenium library — .NET.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Write a browser smoke test

Replace the generated test file contents with the following. It uses the Selenium web form to check the page title, enter text, submit the form, and assert the response. The try/finally ensures the browser session is asked to close even if an assertion or interaction fails.

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

namespace SeleniumSmoke;

[TestClass]
public class WebFormTests
{
    [TestMethod]
    public void SubmittingTextShowsConfirmation()
    {
        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();
        }
    }
}

The test follows the official Selenium C# example’s page, selectors, title, input value, and expected confirmation. A local browser and compatible driver setup are still required; a successful package restore alone does not prove that Chrome can launch. See Selenium’s .NET usage examples for the example context.

Restore, run, and target one test

Run the project from its directory:

dotnet restore
dotnet test

To run only the method, use a fully qualified name filter. Replace the namespace or class name if you changed them:

dotnet test --filter "FullyQualifiedName~SeleniumSmoke.WebFormTests.SubmittingTextShowsConfirmation"

With a working runner, browser, and driver configuration, the test should open the form and pass when the title and submitted confirmation match the assertions. Visual Studio and Visual Studio Code are IDE options in Selenium’s .NET examples. If using an IDE or CI runner, confirm it supports the project’s selected platform, MTP, rather than assuming VSTest-specific instructions apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Browser setup and reliability notes

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting common setup failures

dotnet cannot find the template, SDK, or target framework

Check that the .NET SDK is installed and on PATH, then run dotnet --list-sdks. Use an SDK and target framework supported by the project configuration. The test-project guidance here uses the .NET SDK 8.0-or-later baseline listed by Selenium’s examples, not the separate file-based script requirement.

The test is not discovered or the runner complains

Verify that the project follows one complete runner setup: this tutorial’s MSTest.Sdk path, or a package-based VSTest path. Check that your IDE or CI supports the selected platform and that you did not combine runner dependencies from the other pattern without a reason. Re-run dotnet restore after changing project dependencies.

Chrome fails to start

A successful compile does not establish that a browser session can launch. Confirm Chrome is available in the execution environment and that the Selenium/driver setup is compatible with it. For CI or a clean machine, explicitly plan browser installation and driver management rather than relying on assumptions about a developer workstation.

The browser opens but an element cannot be found

Check that the expected page loaded and that the selectors still match its markup: my-text is the input’s name, the submit control is selected by the button tag, and the response is located by id message. If the page is still loading when Selenium searches, use an appropriate wait strategy rather than assuming every page is immediately ready.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The test fails after an earlier failure or leaves processes behind

Ensure the driver is created inside a scope whose finally block calls Quit(). Avoid placing setup that can fail before cleanup protection is established if it creates a browser session.

Or skip the browser setup

If your goal is a page image rather than browser-driven interaction and assertions, ScreenshotNeo is a screenshot API and MCP server for developers. A single request can return an image or PDF; this is not a replacement for Selenium tests that click controls or verify application behavior.

cURL example (see the ScreenshotNeo documentation for API details):

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://www.selenium.dev/selenium/web/web-form.html -o shot.webp

It removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are not billed. Its MCP server lets AI agents take screenshots, and the free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Sign up for ScreenshotNeo’s free plan.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.