* New translations docker.md (German) * New translations docker.md (Italian) * New translations docker.md (Japanese) * New translations docker.md (Polish) * New translations docker.md (Portuguese) * New translations docker.md (Russian) * New translations docker.md (Ukrainian) * New translations docker.md (Chinese Simplified) * New translations docker.md (Persian) * New translations docker.md (Tamil) * New translations docker.md (Hindi) * New translations boilerplateprojects.md (Belarusian) * New translations boilerplateprojects.md (Portuguese) * New translations customreporter.md (Belarusian) * New translations customreporter.md (Portuguese) * New translations proxy.md (French) * New translations configuration.md (Spanish) * New translations proxy.md (Spanish) * New translations proxy.md (Arabic) * New translations cloudservices.md (Belarusian) * New translations configuration.md (Belarusian) * New translations proxy.md (Belarusian) * New translations proxy.md (Bulgarian) * New translations configuration.md (German) * New translations proxy.md (German) * New translations proxy.md (Italian) * New translations proxy.md (Japanese) * New translations proxy.md (Polish) * New translations cloudservices.md (Portuguese) * New translations configuration.md (Portuguese) * New translations proxy.md (Portuguese) * New translations proxy.md (Russian) * New translations proxy.md (Ukrainian) * New translations proxy.md (Chinese Simplified) * New translations proxy.md (Persian) * New translations proxy.md (Tamil) * New translations configuration.md (Hindi) * New translations proxy.md (Hindi) * New translations setuptypes.md (French) * New translations setuptypes.md (Spanish) * New translations setuptypes.md (Arabic) * New translations setuptypes.md (Belarusian) * New translations setuptypes.md (Bulgarian) * New translations setuptypes.md (German) * New translations setuptypes.md (Italian) * New translations setuptypes.md (Japanese) * New translations setuptypes.md (Polish) * New translations setuptypes.md (Portuguese) * New translations setuptypes.md (Russian) * New translations setuptypes.md (Ukrainian) * New translations setuptypes.md (Chinese Simplified) * New translations setuptypes.md (Persian) * New translations setuptypes.md (Tamil) * New translations setuptypes.md (Hindi) * New translations appium.md (French) * New translations appium.md (Belarusian) * New translations appium.md (German) * New translations appium.md (Japanese) * New translations appium.md (Portuguese) * New translations appium.md (Ukrainian) * New translations appium.md (Tamil) * New translations browser.md (French) * New translations browser.md (Spanish) * New translations browser.md (Arabic) * New translations browser.md (Belarusian) * New translations browser.md (Bulgarian) * New translations browser.md (German) * New translations browser.md (Italian) * New translations browser.md (Japanese) * New translations browser.md (Polish) * New translations browser.md (Portuguese) * New translations browser.md (Russian) * New translations browser.md (Ukrainian) * New translations browser.md (Chinese Simplified) * New translations browser.md (Persian) * New translations browser.md (Tamil) * New translations browser.md (Hindi) * New translations protocols.md (French) * New translations vue.md (French) * New translations protocols.md (Spanish) * New translations vue.md (Spanish) * New translations protocols.md (Arabic) * New translations vue.md (Arabic) * New translations frameworks.md (Belarusian) * New translations protocols.md (Belarusian) * New translations vue.md (Belarusian) * New translations protocols.md (Bulgarian) * New translations vue.md (Bulgarian) * New translations protocols.md (German) * New translations vue.md (German) * New translations protocols.md (Italian) * New translations vue.md (Italian) * New translations protocols.md (Japanese) * New translations vue.md (Japanese) * New translations protocols.md (Polish) * New translations vue.md (Polish) * New translations frameworks.md (Portuguese) * New translations protocols.md (Portuguese) * New translations vue.md (Portuguese) * New translations protocols.md (Russian) * New translations vue.md (Russian) * New translations protocols.md (Ukrainian) * New translations vue.md (Ukrainian) * New translations protocols.md (Chinese Simplified) * New translations vue.md (Chinese Simplified) * New translations protocols.md (Persian) * New translations vue.md (Persian) * New translations protocols.md (Tamil) * New translations vue.md (Tamil) * New translations protocols.md (Hindi) * New translations vue.md (Hindi) * New translations customreporter.md (Belarusian) * New translations customreporter.md (Portuguese) * New translations configuration.md (Belarusian) * New translations configuration.md (Portuguese) * New translations sharding.md (French) * New translations sharding.md (Spanish) * New translations sharding.md (Arabic) * New translations vscodeextension.md (Belarusian) * New translations sharding.md (Belarusian) * New translations sharding.md (Bulgarian) * New translations sharding.md (German) * New translations sharding.md (Italian) * New translations sharding.md (Japanese) * New translations sharding.md (Polish) * New translations vscodeextension.md (Portuguese) * New translations sharding.md (Portuguese) * New translations sharding.md (Russian) * New translations sharding.md (Ukrainian) * New translations sharding.md (Chinese Simplified) * New translations sharding.md (Persian) * New translations sharding.md (Tamil) * New translations sharding.md (Hindi) * New translations code.json (Portuguese, Brazilian) * New translations options.json (Portuguese, Brazilian) * New translations current.json (Portuguese, Brazilian) * New translations current.json (Portuguese, Brazilian) * New translations footer.json (Portuguese, Brazilian) * New translations navbar.json (Portuguese, Brazilian) * New translations api.md (Portuguese, Brazilian) * New translations axecore.md (Portuguese, Brazilian) * New translations autowait.md (Portuguese, Brazilian) * New translations autocompletion.md (Portuguese, Brazilian) * New translations automationprotocols.md (Portuguese, Brazilian) * New translations watchfiles.md (Portuguese, Brazilian) * New translations whywebdriverio.md (Portuguese, Brazilian) * New translations browser.md (Portuguese, Brazilian) * New translations createlocalworkerprocess.md (Portuguese, Brazilian) * New translations donate.md (Portuguese, Brazilian) * New translations materials.md (Portuguese, Brazilian) * New translations openofficehours.md (Portuguese, Brazilian) * New translations support.md (Portuguese, Brazilian) * New translations team.md (Portuguese, Brazilian) * New translations appium.md (Portuguese, Brazilian) * New translations emulation.md (Portuguese, Brazilian) * New translations visualtesting.md (Portuguese, Brazilian) * New translations method-options.md (Portuguese, Brazilian) * New translations methods.md (Portuguese, Brazilian) * New translations writing-tests.md (Portuguese, Brazilian) * New translations snapshot.md (Portuguese, Brazilian) * New translations filedownloads.md (Portuguese, Brazilian) * New translations events.md (Portuguese, Brazilian) * New translations percy-overview.md (Portuguese, Brazilian) * New translations integrate-with-app-percy.md (Portuguese, Brazilian) * New translations integrate-with-percy.md (Portuguese, Brazilian) * New translations browserstack.md (Portuguese, Brazilian) * New translations clock.md (Portuguese, Brazilian) * New translations argos.md (Portuguese, Brazilian) * New translations seleniumgrid.md (Portuguese, Brazilian) * New translations browserlogs.md (Portuguese, Brazilian) * New translations axecore.md (Portuguese, Brazilian) * New translations donate.md (Portuguese, Brazilian) * New translations materials.md (Portuguese, Brazilian) * New translations openofficehours.md (Portuguese, Brazilian) * New translations support.md (Portuguese, Brazilian) * New translations team.md (Portuguese, Brazilian) * New translations events.md (Portuguese, Brazilian) * New translations browserstack.md (Portuguese, Brazilian) * New translations browser.md (Portuguese, Brazilian) * New translations element.md (Portuguese, Brazilian) * New translations environment.md (Portuguese, Brazilian) * New translations events.md (Portuguese, Brazilian) * New translations clock.md (Portuguese, Brazilian) * New translations dialog.md (Portuguese, Brazilian) * New translations globals.md (Portuguese, Brazilian) * New translations axecore.md (Portuguese, Brazilian) * New translations browser.md (Portuguese, Brazilian) * New translations element.md (Portuguese, Brazilian) * New translations globals.md (Portuguese, Brazilian) * New translations mock.md (Portuguese, Brazilian) * New translations modules.md (Portuguese, Brazilian) * New translations protocols.md (Portuguese, Brazilian) * New translations lit.md (Portuguese, Brazilian) * New translations preact.md (Portuguese, Brazilian) * New translations react.md (Portuguese, Brazilian) * New translations solid.md (Portuguese, Brazilian) * New translations svelte.md (Portuguese, Brazilian) * New translations vue.md (Portuguese, Brazilian) * New translations coverage.md (Portuguese, Brazilian) * New translations mocking.md (Portuguese, Brazilian) * New translations donate.md (Portuguese, Brazilian) * New translations materials.md (Portuguese, Brazilian) * New translations openofficehours.md (Portuguese, Brazilian) * New translations support.md (Portuguese, Brazilian) * New translations team.md (Portuguese, Brazilian) * New translations stencil.md (Portuguese, Brazilian) * New translations filedownloads.md (Portuguese, Brazilian) * New translations events.md (Portuguese, Brazilian) * New translations browserstack.md (Portuguese, Brazilian) * New translations clock.md (Portuguese, Brazilian) * New translations dialog.md (Portuguese, Brazilian) * New translations mobile.md (Portuguese, Brazilian) * New translations browserlogs.md (Portuguese, Brazilian) * New translations createlocalworkerprocess.md (Portuguese, Brazilian) * New translations highleveloverview.md (Portuguese, Brazilian) * New translations testexecution.md (Portuguese, Brazilian) * New translations wdiocommands.md (Portuguese, Brazilian) * New translations webextension.md (Portuguese, Brazilian) * New translations vscodeextension.md (Portuguese, Brazilian) * New translations electron.md (Portuguese, Brazilian) * New translations macos.md (Portuguese, Brazilian) * New translations windows.md (Portuguese, Brazilian) * New translations cli-wizard.md (Portuguese, Brazilian) * New translations getting-started.md (Portuguese, Brazilian) * New translations more-test-optimization.md (Portuguese, Brazilian) * New translations ocr-click-on-text.md (Portuguese, Brazilian) * New translations ocr-click-on-text.md (Portuguese, Brazilian) * New translations ocr-faq.md (Portuguese, Brazilian) * New translations ocr-get-element-position-by-text.md (Portuguese, Brazilian) * New translations ocr-get-text.md (Portuguese, Brazilian) * New translations ocr-set-value.md (Portuguese, Brazilian) * New translations materials.md (Portuguese, Brazilian) * New translations considerations.md (Portuguese, Brazilian) * New translations compare-options.md (Portuguese, Brazilian) * New translations faq.md (Portuguese, Brazilian) * New translations integrate-with-app-percy.md (Portuguese, Brazilian) * New translations ocr-wait-for-text-displayed.md (Portuguese, Brazilian) * New translations what-is-wdio-ocr-service.md (Portuguese, Brazilian) * New translations argos.md (Portuguese, Brazilian) * New translations integrate-with-percy.md (Portuguese, Brazilian) * New translations method-options.md (Portuguese, Brazilian) * New translations whywebdriverio.md (Portuguese, Brazilian) * New translations api.md (Portuguese, Brazilian) * New translations frameworks.md (Portuguese, Brazilian) * New translations gettingstarted.md (Portuguese, Brazilian) * New translations selectors.md (Portuguese, Brazilian) * New translations sponsor.md (Portuguese, Brazilian) * update: translate doc to pt-br (#14388) * New translations configuration.md (Portuguese, Brazilian) * more german translations --------- Co-authored-by: Lucas Barbosa <29305833+lucasbarbosaz@users.noreply.github.com>
16 KiB
| id | title |
|---|---|
| browser | 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.
Properties
A browser object has the following properties:
| Name | Type | Details |
|---|---|---|
capabilities |
Object |
Assigned capabilities from the remote server. Example: { |
requestedCapabilities |
Object |
Capabilities requested from the remote server. Example: { browserName: 'chrome' } |
sessionId |
String |
Session id assigned from the remote server. |
options |
Object |
WebdriverIO options depending on how the browser object was created. See more setup types. |
commandList |
String[] |
A list of commands registered to the browser instance |
isW3C |
Boolean |
Indicates if this is a W3C session |
isChrome |
Boolean |
Indicates if this Chrome instance |
isFirefox |
Boolean |
Indicates if this Firefox instance |
isBidi |
Boolean |
Indicates if this session uses Bidi |
isSauce |
Boolean |
Indicates if this session is Running on Sauce Labs |
isMacApp |
Boolean |
Indicates if this session is Running for a native Mac App |
isWindowsApp |
Boolean |
Indicates if this session is Running for a native Windows App |
isMobile |
Boolean |
Indicates a mobile session. See more under Mobile Flags. |
isIOS |
Boolean |
Indicates an iOS session. See more under Mobile Flags. |
isAndroid |
Boolean |
Indicates an Android session. See more under Mobile Flags. |
isNativeContext |
Boolean |
Indicates if the mobile is in the NATIVE_APP context. See more under Mobile Flags. |
mobileContext |
string |
The will provide the current context the driver is in, for example NATIVE_APP, WEBVIEW_<packageName> for Android or WEBVIEW_<pid> for iOS. It will save an extra WebDriver to driver.getContext(). See more under Mobile Flags. |
Methods
Based on the automation backend used for your session, WebdriverIO identifies which Protocol Commands will be attached to the browser object. For example if you run an automated session in Chrome, you will have access to Chromium specific commands like elementHover but not any of the Appium commands.
Furthermore WebdriverIO provides a set of convenient methods that are recommended to use, to interact with the browser or elements on the page.
In addition to that the following commands are available:
| Name | Parameters | Details |
|---|---|---|
addCommand |
- commandName (Type: String)- fn (Type: Function)- attachToElement (Type: boolean) |
Allows to define custom commands that can be called from the browser object for composition purposes. Read more in the Custom Command guide. |
overwriteCommand |
- commandName (Type: String)- fn (Type: Function)- attachToElement (Type: boolean) |
Allows to overwrite any browser command with custom functionality. Use carefully as it can confuse framework users. Read more in the Custom Command guide. |
addLocatorStrategy |
- strategyName (Type: String)- fn (Type: Function) |
Allows to define a custom selector strategy, read more in the Selectors 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.start
This event is fired before a WebDriver request is sent to the driver. It contains information about the request and its payload.
browser.on('request.start', (ev: RequestInit) => {
// ...
})
request.end
This event is fired once the request to the driver received a response. The event object either contains the response body as result or an error if the WebDriver command failed.
browser.on('request.end', (ev: { result: unknown, error?: Error }) => {
// ...
})
request.retry
The retry event can notify you when WebdriverIO attempts to retry running the command, e.g. due to a network issue. It contains information about the error that caused the retry and the amount of retries already done.
browser.on('request.retry', (ev: { error: Error, retryCount: number }) => {
// ...
})
command
This event is emitted whenever WebdriverIO sends a WebDriver Classic command. It contains the following information:
command: the command name, e.g.navigateTomethod: the HTTP method used to send the command request, e.g.POSTendpoint: the command endpoint, e.g./session/fc8dbda381a8bea36a225bd5fd0c069b/urlbody: the command payload, e.g.{ url: 'https://webdriver.io' }
result
This event is emitted whenever WebdriverIO receives a result of a WebDriver Classic command. It contains the same information as the command event with the addition of the following information:
result: the command result
bidiCommand
This event is emitted whenever WebdriverIO sends a WebDriver Bidi command to the browser driver. It contains information about:
method: WebDriver Bidi command methodparams: associated command parameter (see API)
bidiResult
In case of a successful command execution, the event payload will be:
type:successid: the command idresult: the command result (see API)
In case of a command error, the event payload will be:
type:errorid: the command iderror: the error code, e.g.invalid argumentmessage: details about the errorstacktrace: a stack trace
request.start
This event is fired before a WebDriver request is sent to the driver. It contains information about the request and its payload.
browser.on('request.start', (ev: RequestInit) => {
// ...
})
request.end
This event is fired once the request to the driver received a response. The event object either contains the response body as result or an error if the WebDriver command failed.
browser.on('request.end', (ev: { result: unknown, error?: Error }) => {
// ...
})
request.retry
The retry event can notify you when WebdriverIO attempts to retry running the command, e.g. due to a network issue. It contains information about the error that caused the retry and the amount of retries already done.
browser.on('request.retry', (ev: { error: Error, retryCount: number }) => {
// ...
})
request.performance
This is an event to measure WebDriver level operations. Whenever WebdriverIO sends a request to the WebDriver backend, 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's0, 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'sfalse,errorproperty 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.