View Source Improve this doc

addCommand

Add custom command to client/browser instance. Read more about addCommand here.

Usage

1
browser.addCommand(commandName,customMethod,overwrite);

Parameters

Param Type Details
commandName String name of your custom command
customMethod Function your custom method
overwrite Boolean if set to true you can overwrite existing commands

Example

addCommandAsync.js
1
2
3
4
5
6
7
8
9
10
// adding `async` as function name disables the synchronous behavior of WebdriverIO commands
// in case you need to interact with other 3rd party libraries that support promises
client.addCommand("getUrlAndTitle", function async (customVar) {
return this.url().then(function(urlResult) {
return this.getTitle().then(function(titleResult) {
console.log(customVar); // "a custom variable"
return { url: urlResult.value, title: titleResult }
})
})
})
addCommand.js
1
2
3
4
5
6
7
browser.addCommand("getUrlAndTitle", function (customVar) {
return {
url: this.getUrl(),
title: this.getTitle(),
customVar: customVar
}
})
example.js
1
2
3
4
5
6
7
8
it('should use my custom command', function () {
browser.url('http://www.github.com')
var result = browser.getUrlAndTitle('foobar')
assert.strictEqual(result.url, 'https://github.com/')
assert.strictEqual(result.title, 'GitHub · Where software is built')
assert.strictEqual(result.customVar, 'foobar')
})