Tauri 測(cè)試

2023-10-17 14:18 更新

與 WebdriverIO 測(cè)試套件不同,Selenium不是開箱即用的測(cè)試套件,需要開發(fā)人員來(lái)構(gòu)建這些內(nèi)容。 我們選擇 Mocha, 它是相當(dāng)中性的,與 WebDrivers 沒(méi)有關(guān)系。 因此我們的腳本需要做一些工作才能以正確的順序?yàn)槲覀冊(cè)O(shè)置所有內(nèi)容。 Mocha默認(rèn)期望在test/test.js路徑下存在一個(gè)測(cè)試文件,因此讓我們現(xiàn)在創(chuàng)建這個(gè)文件。

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的測(cè)試框架,那么"it"和"expect"應(yīng)該看起來(lái)很熟悉。我們還有相對(duì)復(fù)雜的"before()"和"after()"回調(diào)來(lái)設(shè)置和拆卸Mocha。不屬于測(cè)試本身的代碼行都有注釋,解釋了設(shè)置和拆卸代碼。如果你熟悉WebdriverIO示例中的規(guī)范文件,你會(huì)注意到有更多的不是測(cè)試的代碼,因?yàn)槲覀冃枰O(shè)置一些與WebDriver相關(guān)的其他項(xiàng)。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)