Skip to main content

addCommand

Overview

Use the addCommand command to add your own command to the browser or to an element. The command being added can be either synchronous or asynchronous.

info

Read more about how to add your custom commands in the recipe "How to add custom commands".

Usage

await browser.addCommand(name, callback, elementScope);

Command Parameters

NameTypeDescription
nameStringCustom command name.
callbackFunctionCommand implementation function.
elementScopeBooleanIf the value is true, add the command to the element instead of the browser. Default: false.

Usage Examples

// add the getUrlAndTitle command
await browser.addCommand("getUrlAndTitle", async function (customParam) {
return {
url: await this.getUrl(), // `this` here and below refers to the "browser" object
title: await this.getTitle(),
customParam: customParam,
};
});

// use the new getUrlAndTitle command
it("should use my add command", async ({ browser }) => {
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",
);
assert.strictEqual(result.customParam, "foobar");
});

References

We'd like to give credit to the original WebdriverIO docs article, from which we drew some information while writing our version.