跳到内容
Tauri

WebdriverIO

此 WebDriver 测试示例将使用 WebdriverIO 及其测试套件。预计已安装 Node.js,以及 npmyarn,尽管完成的示例项目使用 yarn

为测试创建目录

让我们在项目中创建一个空间来编写这些测试。我们将为此示例项目使用嵌套目录,因为稍后我们还将介绍其他框架,但通常您只需要使用一个。使用 mkdir -p webdriver/webdriverio 创建我们将使用的目录。本指南的其余部分假定您位于 webdriver/webdriverio 目录中。

初始化 WebdriverIO 项目

我们将使用预先存在的 package.json 来引导此测试套件,因为我们已经选择了特定的 WebdriverIO 配置选项,并希望展示一个简单的工作解决方案。本节底部有一个折叠的指南,介绍如何从头开始设置它。

package.json:

{
"name": "webdriverio",
"version": "1.0.0",
"private": true,
"scripts": {
"test": "wdio run wdio.conf.js"
},
"dependencies": {
"@wdio/cli": "^7.9.1"
},
"devDependencies": {
"@wdio/local-runner": "^7.9.1",
"@wdio/mocha-framework": "^7.9.1",
"@wdio/spec-reporter": "^7.9.0"
}
}

我们有一个脚本,它运行 WebdriverIO 配置作为测试套件,并作为 test 命令公开。我们还有在首次设置时由 @wdio/cli 命令添加的各种依赖项。简而言之,这些依赖项用于使用本地 WebDriver 运行器、Mocha 作为测试框架和简单 Spec Reporter 的最简单设置。

如果您想了解如何从头开始设置项目,请点击我

CLI 是交互式的,您可以选择自己使用的工具。请注意,您可能会与本指南的其余部分有所不同,并且您需要自行设置差异。

让我们将 WebdriverIO CLI 添加到此 npm 项目中。

npm install @wdio/cli

然后运行交互式配置命令来设置 WebdriverIO 测试套件,您可以运行

npx wdio config

配置

您可能已经注意到,我们的 package.json 中的 test 脚本提到了一个文件 wdio.conf.js。那是 WebdriverIO 配置文件,它控制着我们测试套件的大部分方面。

wdio.conf.js:

const os = require('os');
const path = require('path');
const { spawn, spawnSync } = require('child_process');
// keep track of the `tauri-driver` child process
let tauriDriver;
exports.config = {
specs: ['./develop/tests/specs/**/*.js'],
maxInstances: 1,
capabilities: [
{
maxInstances: 1,
'tauri:options': {
application: '../../target/release/hello-tauri-webdriver',
},
},
],
reporters: ['spec'],
framework: 'mocha',
mochaOpts: {
ui: 'bdd',
timeout: 60000,
},
// ensure the rust project is built since we expect this binary to exist for the webdriver sessions
onPrepare: () => spawnSync('cargo', ['build', '--release']),
// ensure we are running `tauri-driver` before the session starts so that we can proxy the webdriver requests
beforeSession: () =>
(tauriDriver = spawn(
path.resolve(os.homedir(), '.cargo', 'bin', 'tauri-driver'),
[],
{ stdio: [null, process.stdout, process.stderr] }
)),
// clean up the `tauri-driver` process we spawned at the start of the session
afterSession: () => tauriDriver.kill(),
};

如果您对 exports.config 对象上的属性感兴趣,我建议阅读文档。对于非 WDIO 特定项,有注释解释了为什么我们在 onPreparebeforeSessionafterSession 中运行命令。我们还将 spec 设置为 "./develop/tests/specs/**/*.js",所以现在让我们创建一个 spec。

Spec

Spec 包含测试您的实际应用程序的代码。测试运行器将加载这些 spec,并根据需要自动运行它们。现在让我们在指定的目录中创建我们的 spec。

test/specs/example.e2e.js:

// calculates the luma from a hex color `#abcdef`
function luma(hex) {
if (hex.startsWith('#')) {
hex = hex.substring(1);
}
const rgb = parseInt(hex, 16);
const r = (rgb >> 16) & 0xff;
const g = (rgb >> 8) & 0xff;
const b = (rgb >> 0) & 0xff;
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
describe('Hello Tauri', () => {
it('should be cordial', async () => {
const header = await $('body > h1');
const text = await header.getText();
expect(text).toMatch(/^[hH]ello/);
});
it('should be excited', async () => {
const header = await $('body > h1');
const text = await header.getText();
expect(text).toMatch(/!$/);
});
it('should be easy on the eyes', async () => {
const body = await $('body');
const backgroundColor = await body.getCSSProperty('background-color');
expect(luma(backgroundColor.parsed.hex)).toBeLessThan(100);
});
});

顶部的 luma 函数只是我们其中一个测试的辅助函数,与应用程序的实际测试无关。如果您熟悉其他测试框架,您可能会注意到暴露了类似的函数,例如 describeitexpect。其他 API,例如 $ 及其公开的方法等项目,都包含在 WebdriverIO API 文档中。

运行测试套件

既然我们已经完成了配置和 spec 的设置,让我们运行它!

npm test

我们应该看到以下输出

➜ webdriverio git:(main) ✗ yarn test
yarn run v1.22.11
$ wdio run wdio.conf.js
Execution of 1 workers started at 2021-08-17T08:06:10.279Z
[0-0] RUNNING in undefined - /develop/tests/specs/example.e2e.js
[0-0] PASSED in undefined - /develop/tests/specs/example.e2e.js
"spec" Reporter:
------------------------------------------------------------------
[wry 0.12.1 linux #0-0] Running: wry (v0.12.1) on linux
[wry 0.12.1 linux #0-0] Session ID: 81e0107b-4d38-4eed-9b10-ee80ca47bb83
[wry 0.12.1 linux #0-0]
[wry 0.12.1 linux #0-0] » /develop/tests/specs/example.e2e.js
[wry 0.12.1 linux #0-0] Hello Tauri
[wry 0.12.1 linux #0-0] ✓ should be cordial
[wry 0.12.1 linux #0-0] ✓ should be excited
[wry 0.12.1 linux #0-0] ✓ should be easy on the eyes
[wry 0.12.1 linux #0-0]
[wry 0.12.1 linux #0-0] 3 passing (244ms)
Spec Files: 1 passed, 1 total (100% completed) in 00:00:01
Done in 1.98s.

我们看到 Spec Reporter 告诉我们,来自 test/specs/example.e2e.js 文件的所有 3 个测试都通过了,以及最终报告 Spec Files: 1 passed, 1 total (100% completed) in 00:00:01

使用 WebdriverIO 测试套件,我们只需通过几行配置和一个运行它的命令,即可轻松为我们的 Tauri 应用程序启用 e2e 测试!更棒的是,我们根本不必修改应用程序。


© 2025 Tauri Contributors. CC-BY / MIT