addCommand 添加命令
浏览器方法 addCommand
可以帮助你编写自己的命令集。
信息
关于添加自定义命令的更多信息,可以在自定义命令指南中找到。
用法
browser.addCommand(name, callback, elementScope)
参数
名称 | 类型 | 详情 |
---|---|---|
name | string | 自 定义命令的名称 |
callback | Function | 要调用的函数 |
elementScope 可选 | Boolean | 扩展Element对象而不是Browser对象 |
示例
execute.js
await browser.addCommand('getUrlAndTitle', async function (customParam) {
// `this` refers to the `browser` scope
return {
url: await this.getUrl(),
title: await this.getTitle(),
customParam: customParam
}
})
//usage
it('should use my add command', async () => {
await browser.url('https://webdriver.io')
const result = await browser.getUrlAndTitle('foobar')
assert.strictEqual(result.url, 'https://webdriver.io')
assert.strictEqual(result.title, 'WebdriverIO · Next-gen browser and mobile automation test framework for Node.js | WebdriverIO')
assert.strictEqual(result.customParam, 'foobar')
})