Skip to main content

Modules

WebdriverIO publishes various modules to NPM and other registries that you can use to build your own automation framework. See more documentation on WebdriverIO setup types here.

webdriver and devtoolsโ€‹

The protocol packages (webdriver and devtools) expose a class with the following static functions attached that allow you to initiate sessions:

newSession(options, modifier, userPrototype, customCommandWrapper)โ€‹

Starts a new session with specific capabilities. Based on the session response commands from different protocols will be provided.

Paramatersโ€‹
Returnsโ€‹
Exampleโ€‹
const client = await WebDriver.newSession({
capabilities: { browserName: 'chrome' }
})

attachToSession(attachInstance, modifier, userPrototype, customCommandWrapper)โ€‹

Attaches to a running WebDriver or DevTools session.

Paramatersโ€‹
Returnsโ€‹
Exampleโ€‹
const client = await WebDriver.newSession({...})
const clonedClient = await WebDriver.attachToSession(client)

reloadSession(instance)โ€‹

Reloads a session given provided instance.

Paramatersโ€‹
Exampleโ€‹
const client = await WebDriver.newSession({...})
await WebDriver.reloadSession(client)

webdriverioโ€‹

Similar as to the protocol packages (webdriver and devtools) you can also use the WebdriverIO package APIs to manage sessions. The APIs can be imported using import { remote, attach, multiRemote } from 'webdriverio and contain the following functionality:

remote(options, modifier)โ€‹

Starts a WebdriverIO session. The instance contains all commands as the protocol package but with additional higher order functions, see API docs.

Paramatersโ€‹
Returnsโ€‹
Exampleโ€‹
import { remote } from 'webdriverio'

const browser = await remote({
capabilities: { browserName: 'chrome' }
})

attach(attachOptions)โ€‹

Attaches to a running WebdriverIO session.

Paramatersโ€‹
Returnsโ€‹
Exampleโ€‹
import { remote, attach } from 'webdriverio'

const browser = await remote({...})
const newBrowser = await attach(browser)

multiRemote(multiRemoteOptions)โ€‹

Initiates a multi-remote instance which allows you to control multiple session within a single instance. Checkout our multi-remote examples for concrete use cases.

Paramatersโ€‹
Returnsโ€‹
Exampleโ€‹
import { multiRemote } from 'webdriverio'

const matrix = await multiRemote({
myChromeBrowser: {
capabilities: { browserName: 'chrome' }
},
myFirefoxBrowser: {
capabilities: { browserName: 'firefox' }
}
})
await matrix.url('http://json.org')
await matrix.getInstance('browserA').url('https://google.com')

console.log(await matrix.getTitle())
// returns ['Google', 'JSON']

Keyโ€‹

An object containing special character constants for use with the browser.keys command. These constants represent special keys that can be sent to the browser, such as Enter, Tab, Escape, arrow keys, function keys, and more.

Exampleโ€‹
import { Key } from 'webdriverio'

// Press Enter key
await browser.keys(Key.Enter)

// Use Ctrl+A to select all (works cross-platform)
await browser.keys([Key.Ctrl, 'a'])

// Navigate with arrow keys
await browser.keys([Key.ArrowDown, Key.ArrowDown, Key.Enter])
Available Keysโ€‹

The following special keys are available via the Key object:

Modifier Keys:

ConstantDescriptionKey.CtrlCross-platform control key (Command on Mac, Control on Windows/Linux)Key.ControlControl keyKey.ShiftShift keyKey.AltAlt keyKey.CommandCommand key (Mac)Key.NULLNull/release key โ€” releases all currently held modifier keys

Navigation Keys:

ConstantDescriptionKey.CancelCancel keyKey.HelpHelp keyKey.BackspaceBackspace keyKey.TabTab keyKey.ClearClear keyKey.ReturnReturn keyKey.EnterEnter keyKey.PausePause keyKey.EscapeEscape keyKey.SpaceSpace keyKey.PageUpPage Up keyKey.PageDownPage Down keyKey.EndEnd keyKey.HomeHome keyKey.ArrowLeftLeft Arrow keyKey.ArrowUpUp Arrow keyKey.ArrowRightRight Arrow keyKey.ArrowDownDown Arrow keyKey.InsertInsert keyKey.DeleteDelete key

Character Keys:

ConstantDescriptionKey.SemicolonSemicolon keyKey.EqualsEquals key

Numpad Keys:

ConstantDescriptionKey.Numpad0 - Key.Numpad9Numpad 0-9Key.MultiplyNumpad MultiplyKey.AddNumpad AddKey.SeparatorNumpad SeparatorKey.SubtractNumpad SubtractKey.DecimalNumpad DecimalKey.DivideNumpad Divide

Function Keys:

ConstantDescriptionKey.F1 - Key.F12Function keys F1 through F12

Other Keys:

ConstantDescriptionKey.ZenkakuHankakuZenkaku/Hankaku key (Japanese)
Cross-Platform Modifier Keys

The Key.Ctrl constant provides a convenient way to use the "control" modifier across different operating systems. On macOS, it maps to the Command key, while on Windows and Linux it maps to the Control key. This is useful when writing tests that need to work across multiple platforms, e.g., for select-all (Ctrl+A), copy (Ctrl+C), or paste (Ctrl+V) operations.

@wdio/cliโ€‹

Instead of calling the wdio command, you can also include the test runner as module and run it in an arbitrary environment. For that, you'll need to require the @wdio/cli package as module, like this:

import Launcher from '@wdio/cli'

After that, create an instance of the launcher, and run the test.

Launcher(configPath, opts)โ€‹

The Launcher class constructor expects the URL to the config file, and an opts object with settings that will overwrite those in the config.

Paramatersโ€‹
Exampleโ€‹
const wdio = new Launcher(
'/path/to/my/wdio.conf.js',
{ spec: '/path/to/a/single/spec.e2e.js' }
)

wdio.run().then((exitCode) => {
process.exit(exitCode)
}, (error) => {
console.error('Launcher failed to start the test', error.stacktrace)
process.exit(1)
})

The run command returns a Promise. It is resolved if tests ran successfully or failed, and it is rejected if the launcher was unable to start run the tests.

@wdio/browser-runnerโ€‹

When running unit or component tests using WebdriverIO's browser runner you can import mocking utilities for your tests, e.g.:

import { fn, spyOn, mock, unmock } from '@wdio/browser-runner'

The following named exports are available:

fnโ€‹

Mock function, see more in the official Vitest docs.

spyOnโ€‹

Spy function, see more in the official Vitest docs.

mockโ€‹

Method to mock file or dependency module.

Paramatersโ€‹
Exampleโ€‹
mock('../src/constants.ts', () => ({
SOME_DEFAULT: 'mocked out'
}))

mock('lodash', (origModuleFactory) => {
const origModule = await origModuleFactory()
return {
...origModule,
pick: fn()
}
})

unmockโ€‹

Unmock dependency that is defined within the manual mock (__mocks__) directory.

Paramatersโ€‹
Exampleโ€‹
unmock('lodash')

Welcome! How can I help?

WebdriverIO AI Copilot