使用 WebDriver (Chromium) 实现测试自动化Use WebDriver (Chromium) for test automation

WebDriver 允许开发人员创建模拟用户交互的自动测试。WebDriver allows developers to create automated tests that simulate user interaction. WebDriver 测试和模拟与 JavaScript 单元测试在以下方面有所不同。WebDriver tests and simulations differ from JavaScript unit tests in the following ways.

  • 访问在浏览器中运行的 JavaScript 不可用的功能和信息。Accesses functionality and information not available to JavaScript running in browsers.
  • 更精确地模拟用户事件或操作系统级事件。Simulates user events or OS-level events more accurately.
  • 在单个测试会话中管理多个窗口、选项卡和网页。Manages multiple windows, tabs, and webpages in a single test session.
  • 在特定的计算机上运行 Microsoft Edge 的多个会话。Runs multiple sessions of Microsoft Edge on a specific machine.

以下部分介绍如何开始使用 WebDriver for Microsoft Edge \ (Chromium) 。The following section describes how to get started with WebDriver for Microsoft Edge (Chromium).

安装 Microsoft Edge (Chromium) Install Microsoft Edge (Chromium)

确保安装Microsoft Edge (Chromium) 。 Ensure you install Microsoft Edge (Chromium). 若要确认已安装 Microsoft Edge \ (Chromium) ,请导航到 edge://settings/help ,并验证版本号是版本 75 或更高版本。To confirm that you have Microsoft Edge (Chromium) installed, navigate to edge://settings/help, and verify the version number is version 75 or later.

下载 Microsoft Edge 驱动程序Download Microsoft Edge Driver

若要开始自动执行测试,请使用以下步骤来确保安装的 WebDriver 版本与浏览器版本相匹配。To begin automating tests, use the following steps to ensure that the WebDriver version you install matches your browser version.

  1. 查找你的 Microsoft Edge 版本。Find your version of Microsoft Edge.

    1. 导航到 edge://settings/helpNavigate to edge://settings/help.

      Microsoft Edge 2021 年 4 月 15 日内部版本号

  2. 导航到 Microsoft Edge 驱动程序Navigate to Microsoft Edge Driver.

  3. 导航到 获取最新版本Navigate to Get the latest version.

  4. 选择与 Microsoft Edge 的版本号匹配的频道版本。Choose the build of channel that matches your version number of Microsoft Edge.

    Microsoft Edge 驱动程序网页上的获取最新版本部分

    Microsoft Edge 驱动程序网页上的 获取 最新版本 部分The Get the latest version section on the Microsoft Edge Driver webpage

选择 WebDriver 语言绑定Choose a WebDriver language binding

必须下载的最后一个组件是特定语言的客户端驱动程序,用于将代码 \ (Python、Java、C#、Ruby、JavaScript) 转换为 Microsoft Edge 驱动程序在 Microsoft Edge \ (Chromium) 中运行的命令。The last component you must download is a language-specific client driver to translate your code (Python, Java, C#, Ruby, JavaScript) into commands the Microsoft Edge Driver runs in Microsoft Edge (Chromium).

下载你选择的 WebDriver 语言绑定Download the WebDriver language binding of your choice. Microsoft Edge 团队建议使用 Selenium 4.00-alpha07 或更高版本,因为它支持 Microsoft Edge \ (Chromium) 。The Microsoft Edge team recommends Selenium 4.00-alpha07 or later, because it supports Microsoft Edge (Chromium). 但是,你可以控制所有旧版本 Selenium 中的 Microsoft Edge \ (Chromium) ,包括当前稳定的 Selenium 3 版本。However, you may control Microsoft Edge (Chromium) in all older versions of Selenium, including the current stable Selenium 3 release.

重要

如果之前使用 和 类自动或测试 Microsoft Edge \ (Chromium) ,则 WebDriver 代码不会在 ChromeDriver ChromeOptions Microsoft Edge 版本 80 或更高版本中运行。If you previously automated or tested Microsoft Edge (Chromium) using ChromeDriver and ChromeOptions classes, your WebDriver code does not run on Microsoft Edge Version 80 or later. 若要解决此问题,请更新测试以使用 EdgeOptions 类并下载 Microsoft Edge 驱动程序To solve the problem, update your tests to use the EdgeOptions class and download Microsoft Edge Driver.

使用 Selenium 3Use Selenium 3

如果你已使用 [Selenium 3,][|::ref1::|]你可能已有浏览器测试,并且希望增加 Microsoft Edge \ (Chromium) 的覆盖范围,而无需更改 Selenium 版本。If you already use [Selenium 3][|::ref1::|], you may have existing browser tests and want to add coverage for Microsoft Edge (Chromium) without changing your version of Selenium. 若要使用 [Selenium 3][|::ref2::|] 为 Microsoft Edge \ (EdgeHTML) 和 Microsoft Edge \ (Chromium) 编写自动测试,请安装适用于 Microsoft Edge 的 Selenium 工具 程序包以使用更新的驱动程序。To use [Selenium 3][|::ref2::|] to write automated tests for both Microsoft Edge (EdgeHTML) and Microsoft Edge (Chromium), install the Selenium Tools for Microsoft Edge package to use the updated driver. 工具 EdgeDriver EdgeDriverService 中包含的 和 类与 Selenium 4 中的内置等效项完全兼容。The EdgeDriver and EdgeDriverService classes included in the tools are fully compatible with the built-in equivalents in Selenium 4.

使用以下步骤将适用于 Microsoft Edge 和[Selenium 3][|::ref3::|]的Selenium 工具添加到项目中。Use the following steps to add the Selenium Tools for Microsoft Edge and [Selenium 3][|::ref3::|] to your project.

  • C#C#
  • PythonPython
  • Java Java
  • JavaScriptJavaScript

使用NuGet CLI或 Visual Studio 将Microsoft.Edge.SeleniumToolsSelenium.WebDriver 程序包添加到 .NET 项目Add the Microsoft.Edge.SeleniumTools and Selenium.WebDriver packages to your .NET project using the NuGet CLI or Visual Studio.

使用 WebDriver (Microsoft Edge) ChromiumAutomate Microsoft Edge (Chromium) with WebDriver

若要使用 WebDriver 自动化浏览器,必须先使用首选的 WebDriver 语言绑定启动 WebDriver 会话。To automate a browser using WebDriver, you must first start a WebDriver session using your preferred WebDriver language binding. 会话是使用 WebDriver 命令控制的浏览器的单个运行实例。A session is a single running instance of a browser controlled using WebDriver commands. 启动 WebDriver 会话以启动新的浏览器实例。Start a WebDriver session to launch a new browser instance. 在关闭 WebDriver 会话之前,启动的浏览器实例保持打开状态。The launched browser instance remains open until you close the WebDriver session.

以下内容将引导你使用 Selenium 启动与 Microsoft Edge \ (Chromium) 的 WebDriver 会话。The following content walks you through using Selenium to start a WebDriver session with Microsoft Edge (Chromium). 可以使用 Selenium 3 或 4 运行这些示例。You may run the examples using either Selenium 3 or 4. 若要与 Selenium 3 一同使用,必须安装适用于 Microsoft Edge 的 Selenium 工具程序包。To use with Selenium 3, the Selenium Tools for Microsoft Edge package must be installed.

自动执行 Microsoft Edge (Chromium) Automate Microsoft Edge (Chromium)

Selenium 使用 EdgeDriver 类管理 Microsoft Edge \ (Chromium) 会话。Selenium uses the EdgeDriver class to manage a Microsoft Edge (Chromium) session. 若要启动会话并自动执行 Microsoft Edge \ (Chromium) ,请创建一个新对象,并传递一个属性设置为 EdgeDriver EdgeOptions UseChromium 的对象 trueTo start a session and automate Microsoft Edge (Chromium), create a new EdgeDriver object and pass it an EdgeOptions object with the UseChromium property set to true.

  • C#C#
  • PythonPython
  • Java Java
  • JavaScriptJavaScript

var options = new EdgeOptions();
options.UseChromium = true;

var driver = new EdgeDriver(options);

备注

如果你的 IT 管理员将DeveloperToolsAvailability策略设置为 ,Microsoft Edge 驱动程序将被阻止驱动 2 Microsoft Edge \ (Chromium) ,因为该驱动程序使用Microsoft Edge DevToolsIf your IT admin has set the DeveloperToolsAvailability policy to 2, Microsoft Edge Driver is blocked from driving Microsoft Edge (Chromium), because the driver uses the Microsoft Edge DevTools. 确保 将 DeveloperToolsAvailability 策略设置为或自动执行 Microsoft Edge (0 1 Chromium) 。Ensure the DeveloperToolsAvailability policy is set to 0 or 1 to automate Microsoft Edge (Chromium).

选择"特定浏览器二 (仅 Chromium") Choose Specific Browser Binaries (Chromium-Only)

可以使用特定的 Microsoft Edge \ (Chromium) 启动 WebDriver 会话。You may start a WebDriver session with specific Microsoft Edge (Chromium) binaries. 例如,可以使用 Microsoft Edge 预览渠道(如 Microsoft Edge Beta) 运行测试。For example, you may run tests using the Microsoft Edge preview channels such as Microsoft Edge Beta.

  • C#C#
  • PythonPython
  • Java Java
  • JavaScriptJavaScript

var options = new EdgeOptions();
options.UseChromium = true;
options.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge Beta\Application\msedge.exe";

var driver = new EdgeDriver(options);

自定义 Microsoft Edge 驱动程序服务Customize the Microsoft Edge Driver Service

  • C#C#
  • PythonPython
  • Java Java
  • JavaScriptJavaScript

使用类创建类实例时,它会为 EdgeOptions EdgeDriver Microsoft Edge EdgeDriverService \ (EdgeHTML) 或 Microsoft Edge \ (Chromium) 创建和启动相应的类。When you use the EdgeOptions class to create an EdgeDriver class instance, it creates and launches the appropriate EdgeDriverService class for either Microsoft Edge (EdgeHTML) or Microsoft Edge (Chromium).

如果要创建 ,请使用 方法创建一个配置为 EdgeDriverService CreateChromiumService() Microsoft Edge \ (Chromium) 。If you want to create an EdgeDriverService, use the CreateChromiumService() method to create one configured for Microsoft Edge (Chromium). CreateChromiumService()当您需要添加自定义项时,该方法非常有用。The CreateChromiumService() method is useful when you need to add customizations. 例如,以下代码开始详细日志输出。For example, the following code starts verbose log output.

using (var service = EdgeDriverService.CreateChromiumService())
{
    service.UseVerboseLogging = true;

    var driver = new EdgeDriver(service);
}

备注

在传递给实例时,不需要 EdgeOptions 提供 EdgeDriverService EdgeDriver 对象。You do not need to provide the EdgeOptions object when you pass EdgeDriverService to the EdgeDriver instance. 该类根据你提供的服务使用 EdgeDriver Microsoft Edge \ (EdgeHTML) 或 Microsoft Edge \ (Chromium) 的默认选项。The EdgeDriver class uses the default options for either Microsoft Edge (EdgeHTML) or Microsoft Edge (Chromium) based on the service you provide.
但是,如果要同时提供 和 类,请确保为相同版本的 Microsoft Edge 配置这两 EdgeDriverService EdgeOptions 个类。However, if you want to provide both EdgeDriverService and EdgeOptions classes, ensure that both are configured for the same version of Microsoft Edge. 例如,您可以在该类中使用默认的 Microsoft Edge \ (EdgeHTML) 类和 EdgeDriverService Chromium EdgeOptions 属性。For example, you may use a default Microsoft Edge (EdgeHTML) EdgeDriverService class and Chromium properties in the EdgeOptions class. EdgeDriver该类会引发错误,以阻止使用不同的版本。The EdgeDriver class throws an error to prevent using different versions.

使用Chromium-Specific选项Use Chromium-Specific Options

如果将 属性设置为 ,可以使用 类访问与自动执行其他 Chromium 浏览器时所使用的特定于 UseChromium true EdgeOptions Chromium 的属性和方法。If you set the UseChromium property to true, you may use the EdgeOptions class to access the same Chromium-specific properties and methods that are used when you automate other Chromium browsers.

  • C#C#
  • PythonPython
  • Java Java
  • JavaScriptJavaScript

var options = new EdgeOptions();
options.UseChromium = true;
options.AddArgument("headless");
options.AddArgument("disable-gpu");

备注

如果 UseChromium 属性设置为 true ,则不能对 Microsoft Edge \ (EdgeHTML) 。If the UseChromium property is set to true, you are not able to use properties and methods for Microsoft Edge (EdgeHTML).

其他 WebDriver 安装选项Other WebDriver installation options

百年Chocolatey

如果你使用 作为程序包 管理器的部署管理程序,请运行以下命令来安装 Microsoft Edge 驱动程序。If you use Chocolatey as your package manager, run the following command to install the Microsoft Edge Driver.

choco install selenium-chromium-edge-driver

有关详细信息,请导航到"位于 "在"完成"上的 Selenium Chromium边缘驱动程序。For more information, navigate to Selenium Chromium Edge Driver on Chocolatey.

DockerDocker

如果使用 Docker,请运行以下命令以下载预配置映像(预安装了 Microsoft Edge \ (Chromium) 和 Microsoft Edge 驱动程序)。If you use Docker, run the following command to download a pre-configured image with Microsoft Edge (Chromium) and Microsoft Edge Driver pre-installed.

docker run -d -p 9515:9515 mcr.microsoft.com/msedge/msedgedriver

有关详细信息,请导航到 Docker Hub 上的 msedgedriver 容器For more information, navigate to the msedgedriver container on Docker Hub.

后续步骤Next steps

有关 WebDriver 和如何使用 Selenium 编写自动 WebDriver 测试的信息,请导航到 Selenium 文档For more information about WebDriver and how to write automated WebDriver tests using Selenium, navigate to the Selenium documentation.

联系 Microsoft Edge DevTools 团队Getting in touch with the Microsoft Edge DevTools team

Microsoft Edge 团队希望倾听你有关使用 WebDriver、Selenium 和 Microsoft Edge 的反馈。The Microsoft Edge team is eager to hear your feedback about using WebDriver, Selenium, and Microsoft Edge. 若要向团队发送你的问题和意见,请选择 Microsoft **** Edge DevTools 中的"发送反馈"图标发送推文@EdgeDevTools。To send the team your questions and comments, choose the Send Feedback icon in the Microsoft Edge DevTools or send a tweet @EdgeDevTools.

Microsoft Edge DevTools 中的“发送反馈”图标