Skip to content

WebDriver

WebDriver 是一个用于与网页文档交互的标准化接口,主要用于自动化测试。推荐在 Tauri 中使用它的方式是 WebdriverIO@wdio/tauri-service,它适用于 Windows、Linux 和 macOS。在 WebdriverIO 项目下维护,它通过 browser.tauri.execute() 提供对 Tauri API 的访问、命令(IPC)模拟、前端和后端日志捕获,以及多重远程支持。

默认情况下,该服务在你的应用内部运行一个嵌入式 WebDriver 服务器,因此在任何平台上都不需要外部驱动程序——这就是 macOS 得以支持的方式。它还可以通过 Windows 和 Linux 上的tauri-driver驱动平台的原生 WebDriver,或者在所有平台上通过 CrabNebula 的跨平台 tauri-driver 分支驱动(macOS 需要付费 API 密钥)。无论你选择哪种方式,该服务都会检测你的应用二进制文件,并且在 tauri-driver 路线下,它会在 Windows 上为你保持 Edge WebDriver 的同步。

🌐 By default the service runs an embedded WebDriver server inside your app, so no external driver is needed on any platform — and this is how macOS is supported. It can also drive the platform’s native WebDriver through tauri-driver on Windows and Linux, or CrabNebula’s cross-platform fork of tauri-driver on all platforms (a paid API key is required for macOS). Whichever route you choose, the service detects your application binary, and on the tauri-driver route it keeps the Edge WebDriver in sync on Windows for you.

搭建项目的最快方式是使用 WebdriverIO 启动器:

🌐 The quickest way to scaffold a project is the WebdriverIO starter:

Terminal window
npm create wdio@latest ./

选择 桌面测试 并在框架提示中选择 Tauri。一个最小配置如下:

🌐 Pick Desktop Testing and choose Tauri at the framework prompt. A minimal configuration looks like this:

export const config: WebdriverIO.Config = {
services: [
[
'tauri',
{
appBinaryPath: './src-tauri/target/release/my-tauri-app',
driverProvider: 'embedded',
},
],
],
};

设置此项使用两个小型 Tauri 插件,根据你的需求,这两个插件都是可选的:

🌐 Setting this up uses two small Tauri plugins, both optional depending on your requirements:

  • tauri-plugin-wdio-webdriver 运行嵌入式 WebDriver 服务器。它是 embedded 提供程序(默认)的必需组件——该服务通过它驱动你的应用,无需外部驱动程序,同时这也是 macOS 得以支持的方式。如果你想改为使用 externalcrabnebula 提供程序,可以跳过它。
  • tauri-plugin-wdio 允许后端访问,包括:browser.tauri.execute()、命令(IPC)模拟和日志捕获。

请参阅 [插件设置] 获取完整步骤,如果你使用该提供商,请参阅 [CrabNebula 设置指南]。

🌐 See Plugin Setup for the full steps, and the CrabNebula setup guide if you use that provider.

对于快速、仅渲染器的测试,还有一种 浏览器模式,它在普通的 Chrome 中运行你的 Tauri 前端,并连接 Vite 开发服务器——不需要 Tauri 二进制文件、驱动程序或插件。它会拦截 invoke() 调用,因此你可以使用相同的 WDIO API 模拟命令并断言其参数。参见 [浏览器模式指南]。

🌐 For fast, renderer-only tests there is also a browser mode that runs your Tauri frontend in plain Chrome against a Vite dev server — no Tauri binary, driver, or plugin required. It intercepts invoke() calls so you can mock commands and assert on their arguments with the same WDIO API. See the browser mode guide.

🌐 Example Applications

使用该服务的完整、可运行示例位于 WebdriverIO 桌面-移动 仓库

🌐 Complete, runnable examples that use the service live in the WebdriverIO desktop-mobile repository.

🌐 Continuous Integration (CI)

WebDriver CI 指南解释了如何在 GitHub Actions 下运行这些测试以及背后的概念。

🌐 The WebDriver CI guide explains how to run these tests under GitHub Actions and the concepts behind it.

🌐 Driving tauri-driver directly

如果你不使用 Node.js,更倾向于 Selenium,或者正在将 WebDriver 集成到自定义测试环境中,你可以直接驱动 tauri-driver 而不是使用服务。直接驱动时,桌面端仅支持 Windows 和 Linux,因为 macOS 没有可用的 WKWebView 驱动工具(macOS 上请使用服务内嵌的 WebDriver 服务器)。

🌐 If you are not using Node.js, prefer Selenium, or are integrating WebDriver into a custom test harness, you can drive tauri-driver directly instead of using the service. Driven directly, only Windows and Linux are supported on desktop, as macOS has no WKWebView driver tool available (use the service’s embedded WebDriver server for macOS).


Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站