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://webdriverio.dev.org.tw')
const result = await browser.getUrlAndTitle('foobar')
assert.strictEqual(result.url, 'https://webdriverio.dev.org.tw')
assert.strictEqual(result.title, 'WebdriverIO · Next-gen browser and mobile automation test framework for Node.js | WebdriverIO')
assert.strictEqual(result.customParam, 'foobar')
})