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

browser.addCommand(name, callback, elementScope);

Command Parameters

NameTypeDescription
nameStringCustom command name.
callbackFunctionCommand implementation function.
elementScopeBoolean

If the value is true, add the command to the element instead of the browser. Default: false.

Usage Examples

// add the getUrlAndTitle command
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://testplane.io");

const result = await browser.getUrlAndTitle("foobar");

assert.strictEqual(result.url, "https://testplane.io");
assert.strictEqual(result.title, "Testplane Docs | Testplane Docs");
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.