Selenium
本 WebDriver 测试示例将使用Selenium和一个流行的 Node.js 测试套件。您应该已经安装了 Node.js,以及npm
或yarn
,尽管完成的示例项目使用了yarn
。
创建测试目录
让我们在项目中创建一个空间来编写这些测试。在本示例项目中,我们将使用嵌套目录,因为稍后我们还将介绍其他框架,但通常您只需要使用一个。使用mkdir -p webdriver/selenium
创建我们将使用的目录。本指南的其余部分将假设您位于webdriver/selenium
目录中。
初始化 Selenium 项目
我们将使用预先存在的package.json
来引导此测试套件,因为我们已经选择了要使用的特定依赖项,并希望展示一个简单的可行方案。本节底部有一个折叠的指南,说明如何从头开始设置。
package.json
:
{
"name": "selenium",
"version": "1.0.0",
"private": true,
"scripts": {
"test": "mocha"
},
"dependencies": {
"chai": "^4.3.4",
"mocha": "^9.0.3",
"selenium-webdriver": "^4.0.0-beta.4"
}
}
我们有一个脚本将Mocha作为测试框架运行,作为test
命令公开。我们还有各种依赖项,我们将使用它们来运行测试。Mocha作为测试框架,Chai作为断言库,以及selenium-webdriver
,它是 Node.js 的Selenium包。
如果您想了解如何从头开始设置项目,请点击这里
如果您想从头开始安装依赖项,只需运行以下命令。
- npm
- Yarn
- Bun
npm install mocha chai selenium-webdriver
yarn add mocha chai selenium-webdriver
bun add mocha chai selenium-webdriver
我还建议在package.json
的"scripts"
键中添加一个"test": "mocha"
项,以便可以使用以下命令简单地调用 Mocha:
- npm
- Yarn
- Bun
npm test
yarn test
bun run test
测试
与WebdriverIO 测试套件不同,Selenium 没有自带测试套件,而是由开发者构建。我们选择了Mocha,它非常中性且与 WebDriver 无关,因此我们的脚本需要做一些工作来按正确的顺序为我们设置好一切。Mocha默认情况下期望在test/test.js
处找到测试文件,所以让我们现在创建该文件。
test/test.js
:
const os = require('os')
const path = require('path')
const { expect } = require('chai')
const { spawn, spawnSync } = require('child_process')
const { Builder, By, Capabilities } = require('selenium-webdriver')
// create the path to the expected application binary
const application = path.resolve(
__dirname,
'..',
'..',
'..',
'target',
'release',
'hello-tauri-webdriver'
)
// keep track of the webdriver instance we create
let driver
// keep track of the tauri-driver process we start
let tauriDriver
before(async function () {
// set timeout to 2 minutes to allow the program to build if it needs to
this.timeout(120000)
// ensure the program has been built
spawnSync('cargo', ['build', '--release'])
// start tauri-driver
tauriDriver = spawn(
path.resolve(os.homedir(), '.cargo', 'bin', 'tauri-driver'),
[],
{ stdio: [null, process.stdout, process.stderr] }
)
const capabilities = new Capabilities()
capabilities.set('tauri:options', { application })
capabilities.setBrowserName('wry')
// start the webdriver client
driver = await new Builder()
.withCapabilities(capabilities)
.usingServer('http://127.0.0.1:4444/')
.build()
})
after(async function () {
// stop the webdriver session
await driver.quit()
// kill the tauri-driver process
tauriDriver.kill()
})
describe('Hello Tauri', () => {
it('should be cordial', async () => {
const text = await driver.findElement(By.css('body > h1')).getText()
expect(text).to.match(/^[hH]ello/)
})
it('should be excited', async () => {
const text = await driver.findElement(By.css('body > h1')).getText()
expect(text).to.match(/!$/)
})
it('should be easy on the eyes', async () => {
// selenium returns color css values as rgb(r, g, b)
const text = await driver
.findElement(By.css('body'))
.getCssValue('background-color')
const rgb = text.match(/^rgb\((?<r>\d+), (?<g>\d+), (?<b>\d+)\)$/).groups
expect(rgb).to.have.all.keys('r', 'g', 'b')
const luma = 0.2126 * rgb.r + 0.7152 * rgb.g + 0.0722 * rgb.b
expect(luma).to.be.lessThan(100)
})
})
如果您熟悉 JS 测试框架,describe
、it
和expect
应该看起来很熟悉。我们还有半复杂的before()
和after()
回调来设置和拆卸 mocha。非测试本身的行都有注释解释了设置和拆卸代码。如果您熟悉WebdriverIO 示例中的规范文件,您会注意到更多非测试代码,因为我们必须设置更多与 WebDriver 相关的项目。
运行测试套件
现在我们已经设置好依赖项和测试脚本,让我们运行它!
- npm
- Yarn
- Bun
npm test
yarn test
bun run test
我们应该看到以下输出
➜ selenium git:(main) ✗ yarn test
yarn run v1.22.11
$ Mocha
Hello Tauri
✔ should be cordial (120ms)
✔ should be excited
✔ should be easy on the eyes
3 passing (588ms)
Done in 0.93s.
我们可以看到,我们使用describe
创建的Hello Tauri
测试套件中的所有 3 个使用it
创建的项目都通过了测试!
使用Selenium和一些与测试套件的连接,我们只需启用 e2e 测试,而无需修改我们的 Tauri 应用程序!