Skip to main content

The Browser Object

Extends: EventEmitter

The browser object is the session instance you use to control the browser or mobile device with. If you use the WDIO test runner, you can access the WebDriver instance through the global browser or driver object or import it using @wdio/globals. If you use WebdriverIO in standalone mode the browser object is returned by the remote method.

The session is initialized by the test runner. The same goes for ending the session. This is also done by the test runner process.

Besides all commands from the API, the browser object provides some more information you might be interested in during your test run:

Properties​

A browser object has the following properties:

NameTypeDetails
capabilitiesObjectAssigned capabilitie from the remote server.
Example:
{
acceptInsecureCerts: false,
browserName: 'chrome',
browserVersion: '105.0.5195.125',
chrome: {
chromedriverVersion: '105.0.5195.52 (412c95e518836d8a7d97250d62b29c2ae6a26a85-refs/branch-heads/5195@{#853})',
userDataDir: '/var/folders/3_/pzc_f56j15vbd9z3r0j050sh0000gn/T/.com.google.Chrome.76HD3S'
},
'goog:chromeOptions': { debuggerAddress: 'localhost:64679' },
networkConnectionEnabled: false,
pageLoadStrategy: 'normal',
platformName: 'mac os x',
proxy: {},
setWindowRect: true,
strictFileInteractability: false,
timeouts: { implicit: 0, pageLoad: 300000, script: 30000 },
unhandledPromptBehavior: 'dismiss and notify',
'webauthn:extension:credBlob': true,
'webauthn:extension:largeBlob': true,
'webauthn:virtualAuthenticators': true
}
requestedCapabilitiesObjectCapabilities requested from the remote server.
Example:
{ browserName: 'chrome' }
sessionIdStringSession id assigned from the remote server.
optionsObjectWebdriverIO options depending on how the browser object was created. See more setup types.
commandListString[]A list of commands registered to the browser instance
isMobileBooleanIndicates a mobile session. See more under Mobile Flags.
isIOSBooleanIndicates an iOS session. See more under Mobile Flags.
isAndroidBooleanIndicates an Android session. See more under Mobile Flags.

Methods​

A browser object provides all methods from the protocol section, e.g. WebDriver protocol as well as commands listed within the browser section. Available protocol commands depend on the type of session. If you run an automated browser session, none of the Appium commands will be available vice versa.

In addition to that the following commands are available:

NameParametersDetails
addCommand- commandName (Type: String)
- fn (Type: Function)
- attachToElement (Type: boolean)
Allows to define custom commands that can be called from the browser object for compisition purposes. Read more in the Custom Command guide.
overwriteCommand- commandName (Type: String)
- fn (Type: Function)
- attachToElement (Type: boolean)
Allows to overwite any browser command with custom functionality. Use carefully as it can confuse framework users. Read more in the Custom Command guide.

Remarks​

Mobile Flags​

If you need to modify your test based on whether or not your session runs on a mobile device, you can access the mobile flags to check.

For example, given this config:

// wdio.conf.js
export const config = {
// ...
capabilities: {
platformName: 'iOS',
app: 'net.company.SafariLauncher',
udid: '123123123123abc',
deviceName: 'iPhone',
// ...
}
// ...
}

You can access these flags in your test like so:

// Note: `driver` is the equivalent to the `browser` object but semantically more correct
// you can choose which global variable you want to use
console.log(driver.isMobile) // outputs: true
console.log(driver.isIOS) // outputs: true
console.log(driver.isAndroid) // outputs: false

This can be useful if, for example, you want to define selectors in your page objects based on the device type, like this:

// mypageobject.page.js
import Page from './page'

class LoginPage extends Page {
// ...
get username() {
const selectorAndroid = 'new UiSelector().text("Cancel").className("android.widget.Button")'
const selectorIOS = 'UIATarget.localTarget().frontMostApp().mainWindow().buttons()[0]'
const selectorType = driver.isAndroid ? 'android' : 'ios'
const selector = driver.isAndroid ? selectorAndroid : selectorIOS
return $(`${selectorType}=${selector}`)
}
// ...
}

You can also use these flags to run only certain tests for certain device types:

// mytest.e2e.js
describe('my test', () => {
// ...
// only run test with Android devices
if (driver.isAndroid) {
it('tests something only for Android', () => {
// ...
})
}
// ...
})

Events​

The browser object is an EventEmitter and a couple of events are emitted for your use cases.

Here is a list of events. Keep in mind that this is not the full list of available events yet. Feel free to contribute to update the document by adding descriptions of more events here.

request.performance​

This is an event to measure WebDriver level operations. Whenever WebDriverIO requests to your WebDriver endoints, this event will be emitted with some useful information:

  • durationMillisecond: Time duration of the request in millisecond.
  • error: Error object if the request failed.
  • request: Request object. You can find url, method, headers, etc.
  • retryCount: If it's 0, the request was the first attempt. It will increase when WebDriverIO retries under the hood.
  • success: Boolean to represent the request was succeeded or not. If it's false, error property will be provided as well.

An example event:

Object {
"durationMillisecond": 0.01770925521850586,
"error": [Error: Timeout],
"request": Object { ... },
"retryCount": 0,
"success": false,
},

Custom Commands​

You can set custom commands on the browser scope to abstract away workflows that are commonly used. Check out our guide on Custom Commands for more information.