Get started with Selenium testing in a CD pipeline

VS 2017 | VS 2015 | VSTS | TFS 2018 | TFS 2017 | TFS 2015

Performing user interface (UI) testing as part of the release process is a great way of detecting unexpected changes, and need not be difficult. This topic describes using Selenium to test your website during a continuous deployment release.

Typically you will run unit tests in your build workflow, and functional (UI) tests in your release workflow after your app is deployed (usually to a QA environment).

For more information about Selenium browser automation, see:

Create your test project

As there is no template for Selenium testing, the easiest way to get started is to use the Unit Test template. This automatically adds the test framework references and enables you run and view the results from Visual Studio Test Explorer.

  1. In Visual Studio, open the File menu and choose New Project, then choose Test and select Unit Test Project. Alternatively, open the shortcut menu for the solution and choose Add then New Project and then Unit Test Project.

  2. After the project is created, add the Selenium and browser driver references used by the browser to execute the tests. Open the shortcut menu for the Unit Test project and choose Manage NuGet Packages. Add the following packages to your project:

    • Selenium.WebDriver
    • Selenium.Firefox.WebDriver
    • Selenium.WebDriver.ChromeDriver
    • Selenium.WebDriver.IEDriver

    Adding the browser driver packages to your solution

  3. Create your tests. For example, the following code creates a default class named MySeleniumTests that performs a simple test on the website. Replace the contents of the TheBingSearchTest function with the Selenium code required to test your web app or website. Change the browser assignment in the SetupTest function to the browser you want to use for the test.

    using System;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using OpenQA.Selenium;           
    using OpenQA.Selenium.Firefox;   
    using OpenQA.Selenium.Chrome;    
    using OpenQA.Selenium.IE;
    namespace SeleniumBingTests
      /// <summary>
      /// Summary description for MySeleniumTests
      /// </summary>
      public class MySeleniumTests
        private TestContext testContextInstance;
        private IWebDriver driver;
        private string appURL;
        public MySeleniumTests()
        public void TheBingSearchTest()
          driver.Navigate().GoToUrl(appURL + "/");
          Assert.IsTrue(driver.Title.Contains("VSTS"), "Verified title of the page");
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        public TestContext TestContext
            return testContextInstance;
            testContextInstance = value;
        public void SetupTest()
          appURL = "";
          string browser = "Chrome";
            case "Chrome":
              driver = new ChromeDriver();
            case "Firefox":
              driver = new FirefoxDriver();
            case "IE":
              driver = new InternetExplorerDriver();
              driver = new ChromeDriver();
        public void MyTestCleanup()
  4. Run the Selenium test locally using Test Explorer and check that it works.

Define your CI build process

You'll need a continuous integration (CI) build process that builds your Selenium tests. For more details, see Build your .NET desktop app for Windows.

Create your web app

You'll need a web app to test. You can use an existing app, or deploy one in your CD release process. The example code above runs tests against For details of how to set up your own release process to deploy a web app, see Build and deploy to an Azure Web App.

Decide how you will deploy and test your app

You can deploy and test your app using either the Hosted agent in Azure, or a private agent that you install on the target servers.

  • When using the Hosted agent, you should use the Selenium web drivers that are pre-installed on the hosted agents because they are compatible with the browser versions installed on the hosted agent images. The file paths to these drivers can be obtained from the environment variables named IEWebDriver (Internet Explorer), ChromeWebDriver (Google Chrome), and GeckoWebDriver (Firefox). For example,

    driver = new ChromeDriver(Environment.GetEnvironmentVariable("ChromeWebDriver")); 

  • When using a private agent that you deploy on your target servers, agents must be configured to run interactively with auto-logon enabled. See Build and Release Agents.

Include the test in a CD release

NOTE: This example uses the Visual Studio Test Platform Installer task and the latest version of the Visual Studio Test task. These tasks are not available in TFS 2015 or TFS 2017. To run Selenium tests in these versions of TFS, you must use the Visual Studio Test Agent Deployment and Run Functional Tests tasks instead.

  1. If you don't have an existing release definition that deploys your web app:

    • Open the Releases page in the Build & Release hub and choose the + icon, then choose Create release definition.

      Creating a new release definition

    • Select the Azure App Service Deployment template and choose Apply.

    • In the Artifacts section of the Pipeline tab, choose + Add. Select your build artifacts and choose Add.

      Selecting the artifacts

    • Choose the Continuous deployment trigger icon in the Artifacts section of the Pipeline tab. In the Continuous deployment trigger pane, enable the trigger so that a new release is created from every build.

      Configuring continuous deployment

    • Open the Tasks tab, select the Environment 1 section, and enter your account information and the name of the web app where you want to deploy the app and tests. These settings are applied to the Deploy Azure App Service task.

  2. If you are deploying your app and tests to environments where the target machines that host the agents do not have Visual Studio installed:

    • In the Tasks tab of the release definition, choose the + icon in the Run on agent section. Select the Visual Studio Test Platform Installer task and choose Add. Leave all the settings at the default values.

      Adding a Visual Studio Test Platform Installer task

      You can find a task more easily by using the search textbox.

  3. In the Tasks tab of the release definition, choose the + icon in the Run on agent section. Select the Visual Studio Test task and choose Add.

  4. If you added the Visual Studio Test Platform Installer task to your definition, change the Test platform version setting in the Execution options section of the Visual Studio Test task to Installed by Tools Installer.

    Setting the teat platform version

    How do I pass parameters to my test code from a release pipeline?

  5. Save the release definition and start a new release. You can do this by queuing a new CI build, or by choosing Create release from the Release drop-down list in the release definition.

    Creating a new release

View the test results

  1. To view the test results, open the release summary from the Releases tab.

    Selecting a release summary

  2. In the release summary page choose the Tests link to show a page that highlights the changes including errors, stack traces, and the ability to easily create a bug that contains this information.

Next steps