跳到主要内容

WebdriverIO

示例应用

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 项目中。

yarn add @wdio/cli

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

yarn 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: ['./test/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 中运行命令。我们还将我们的规范设置为 "./test/specs/**/*.js",所以让我们现在创建一个规范。

规范

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

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 文档 涵盖。

运行测试套件

现在我们已经准备好配置和规范,让我们运行它!

yarn 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 - /test/specs/example.e2e.js
[0-0] PASSED in undefined - /test/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] » /test/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 测试!更好的是,我们根本不必修改应用程序。