# Automate and test WebView2 with Microsoft Edge Driver

Because WebView2 uses the Microsoft Edge (Chromium) web platform, WebView2 developers (you) may take advantage of standard web tooling for debugging and automation. Selenium is one such tool. It implements the W3C WebDriver API. You may use Selenium to create automated tests to simulate user interactions.

Get started with the following steps.

If you do not have an existing WebView2 project, download the WebView2API Sample app, a comprehensive sample of the latest WebView2 SDK. Ensure you have satisfied the prerequisites for the WebView2API Sample app.

Once you have cloned the repo, build the project in Visual Studio. It should look like the following figure.

## Step 2: Install Microsoft Edge Driver

Follow the instructions to install Microsoft Edge Driver the browser-specific driver required by Selenium to automate and test WebView2.

Ensure that the version of Microsoft Edge Driver matches the version of WebView2 Runtime that you app uses. For the WebView2API Sample to work, make sure that your version of WebView2 Runtime is greater than or equal than the supported version of the latest WebView2 SDK release. To locate the latest WebView2 SDK release, navigate to WebView2 release notes. To find out what version of WebView2 Runtime you currently have, navigate to edge://settings/help.

## Step 3: Add Selenium to the WebView2API Sample

At this point you should have WebView2 Runtime installed, built a WebView2 project, and installed Microsoft Edge Driver. Now, get started using Selenium.

Note

Selenium supports C#, Java, Python, Javascript, and Ruby. However, the following guide is written using C#.

1. Start by creating a new C# .NET Framework project in Visual Studio. Choose Next on the bottom right-hand corner to continue.

2. Give your project a name, save it to your preferred location, and choose Create.

3. A new project is created. In this guide, all code is written to the Program.cs file.

4. Now add Selenium to the project. Install Selenium using the Selenium.WebDriver NuGet package.

To download the Selenium.WebDriver NuGet package, in Visual Studio, hover on Project, and choose Manage NuGet Package. The following screen should appear.

5. Type Selenium.WebDriver in the search bar, choose Selenium.WebDriver from the results, and make sure to checkmark the box next to include pre-release. On the right-hand side window, ensure the Version is set to install 4.0.0-alpha04 or later and choose Install. NuGet downloads Selenium to your machine.

To learn more about the Selenium.WebDriver NuGet package, navigate to Selenium.WebDriver 4.0.0-alpha04.

6. Use OpenQA.Selenium.Edge by adding the following statement: using OpenQA.Selenium.Edge; at the beginning of Program.cs file.

using OpenQA.Selenium.Edge;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


## Step 4: Drive WebView2 with Selenium and Microsoft Edge Driver

1. First, create the EdgeOptions object, by copying the following code snippet.

static void Main(string[] args)
{
// EdgeOptions() requires using OpenQA.Selenium.Edge
// Construct EdgeOptions with is_legacy = false and the string "webview2"
EdgeOptions edgeOptions = new EdgeOptions(false, "webview2");


The EdgeOptions object takes in the following two parameters.

Parameter Details
is_legacy Set to false, which tells Selenium that you are driving the new Chromium-based Microsoft Edge browser.
"webview2" A string that tells Selenium you are driving WebView2.
2. Next, set edgeOptions.BinaryLocation to the file path of your WebView2 project runtime, create a string named msedgedriverDir that provides the file path to where you installed Microsoft Edge Driver, and create a string named msedgedriverExe to store the name of the Microsoft Edge Driver runtime. By default, the runtime is named msedgedriver.exe. Use these two strings to construct the EdgeDriverService object as shown below. Finally, create the EdgeDriver object using EdgeDriverService and EdgeOptions.

You may copy and paste the following code underneath edgeOptions. Ensure you specify the correct file paths to your project runtime and the Microsoft Edge Driver runtime on your machine.

//Set the BinaryLocation to the filepath of the WebView2API Sample runtime
edgeOptions.BinaryLocation = @"C:\path\to\your\webview2\project.exe";

//Set msedgedriverDir to the filepath of the directory housing msedgedriver.exe
string msedgedriverDir = @"C:\path\to\your\msededriver.exe's\directory";

//Set msedgedriverExe to the name of the Edge Driver. By default it is:
string msedgedriverExe = @"msedgedriver.exe";

// Construct EdgeDriverService with is_legacy = false
EdgeDriverService service = EdgeDriverService.CreateDefaultService(msedgedriverDir, msedgedriverExe, false);

EdgeDriver e = new EdgeDriver(service, edgeOptions);

3. Now, EdgeDriver is configured to drive the WebView2 in your project. For example, if you are using the WebView2API Sample, you may navigate to https://microsoft.com by running the e.Url = @"https://www.microsoft.com"; command. Verify the Selenium drive WebView2 by setting a breakpoint on the line and running the project.

    //The following navigates the WebView2API Sample from bing.com to microsoft.com
e.Url = @"https://www.microsoft.com";

//This exits the edge driver
e.Quit();
}


Congratulations. You have successfully automated a WebView2 project and driven WebView2 using Selenium and Microsoft Edge Driver.