Tauri 测试

2023-10-17 14:18 更新

与 WebdriverIO 测试套件不同,Selenium不是开箱即用的测试套件,需要开发人员来构建这些内容。 我们选择 Mocha, 它是相当中性的,与 WebDrivers 没有关系。 因此我们的脚本需要做一些工作才能以正确的顺序为我们设置所有内容。 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://localhost: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)
})
})

如果你熟悉JavaScript的测试框架,那么"it"和"expect"应该看起来很熟悉。我们还有相对复杂的"before()"和"after()"回调来设置和拆卸Mocha。不属于测试本身的代码行都有注释,解释了设置和拆卸代码。如果你熟悉WebdriverIO示例中的规范文件,你会注意到有更多的不是测试的代码,因为我们需要设置一些与WebDriver相关的其他项。


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号