自定义命令
如果你想用自己的一组命令扩展browser
实例,浏览器方法addCommand
就是为你准备的。你可以以异步方式编写命令,就像在你的测试规范中一样。
参数
命令名称
定义命令并将其附加到浏览器或元素作用域的名称。
类型:String
自定义函数
当调用命令时执行的函数。this
作用域是WebdriverIO.Browser
或WebdriverIO.Element
,取决于命令是附加到浏览器还是元素作用域。
类型:Function
目标作用域
决定是将命令附加到浏览器还是元素作用域的标志。如果设置为true
,该命令将是一个元素命令。
类型:Boolean
默认值:false
示例
这个例子展示了如何添加一个新命令,该命令将当前URL和标题作为一个结果返回。作用域(this
)是一个WebdriverIO.Browser
对象。
browser.addCommand('getUrlAndTitle', async function (customVar) {
// `this` refers to the `browser` scope
return {
url: await this.getUrl(),
title: await this.getTitle(),
customVar: customVar
}
})
此外,你可以通过将true
作为最后一个参数传递,来用你自己的一组命令扩展元素实例。在这种情况下,作用域(this
)是一个WebdriverIO.Element
对象。
browser.addCommand("waitAndClick", async function () {
// `this` is return value of $(selector)
await this.waitForDisplayed()
await this.click()
}, true)
自定义命令给你机会将你经常使用的特定命令序列捆绑为单个调用。你可以在测试套件中的任何点定义自定义命令;只需确保在首次使用之前定义命令。(wdio.conf.js
中的before
钩子是创建它们的一个好地方。)
定义后,你可以按如下方式使用它们:
it('should use my custom command', async () => {
await browser.url('http://www.github.com')
const result = await browser.getUrlAndTitle('foobar')
assert.strictEqual(result.url, 'https://github.com/')
assert.strictEqual(result.title, 'GitHub · Where software is built')
assert.strictEqual(result.customVar, 'foobar')
})
注意: 如果你将自定义命令注册到browser
作用域,该命令将无法被元素访问。同样,如果你将命令注册到元素作用域,它将无法在browser
作用域中访问:
browser.addCommand("myCustomBrowserCommand", () => { return 1 })
const elem = await $('body')
console.log(typeof browser.myCustomBrowserCommand) // outputs "function"
console.log(typeof elem.myCustomBrowserCommand()) // outputs "undefined"
browser.addCommand("myCustomElementCommand", () => { return 1 }, true)
const elem2 = await $('body')
console.log(typeof browser.myCustomElementCommand) // outputs "undefined"
console.log(await elem2.myCustomElementCommand('foobar')) // outputs "1"
const elem3 = await $('body')
elem3.addCommand("myCustomElementCommand2", () => { return 2 })
console.log(typeof browser.myCustomElementCommand2) // outputs "undefined"
console.log(await elem3.myCustomElementCommand2('foobar')) // outputs "2"
注意: 如果你需要链接自定义命令,该命令应该以$
结尾,
browser.addCommand("user$", (locator) => { return ele })
browser.addCommand("user$", (locator) => { return ele }, true)
await browser.user$('foo').user$('bar').click()
小心不要用太多自定义命令使browser
作用域过载。
我们建议在页面对象中定义自定义逻辑,这样它们就会绑定到特定页面。
多重远程
addCommand
对多重远程的工作方式类似,不同的是新命令将向下传播到子实例。使用this
对象时必须注意,因为多重远程browser
及其子实例有不同的this
。
这个例子展示了如何为多重远程添加新命令。
import { multiremotebrowser } from '@wdio/globals'
multiremotebrowser.addCommand('getUrlAndTitle', async function (this: WebdriverIO.MultiRemoteBrowser, customVar: any) {
// `this` refers to:
// - MultiRemoteBrowser scope for browser
// - Browser scope for instances
return {
url: await this.getUrl(),
title: await this.getTitle(),
customVar: customVar
}
})
multiremotebrowser.getUrlAndTitle()
/*
{
url: [ 'https://webdriver.io/', 'https://webdriver.io/' ],
title: [
'WebdriverIO · Next-gen browser and mobile automation test framework for Node.js | WebdriverIO',
'WebdriverIO · Next-gen browser and mobile automation test framework for Node.js | WebdriverIO'
],
customVar: undefined
}
*/
multiremotebrowser.getInstance('browserA').getUrlAndTitle()
/*
{
url: 'https://webdriver.io/',
title: 'WebdriverIO · Next-gen browser and mobile automation test framework for Node.js | WebdriverIO',
customVar: undefined
}
*/