WebdriverIO DevTools Service
A WebdriverIO service that allows you to run Chrome DevTools commands in your tests
With Chrome v63 and up the browser started to support multi clients allowing arbitrary clients to access the Chrome DevTools Protocol. This provides interesting opportunities to automate Chrome beyond the WebDriver protocol. With this service you can enhance the wdio browser object to leverage that access and call Chrome DevTools commands within your tests to e.g. intercept requests, throttle network capabilities or take CSS/JS coverage.
Since Firefox 86, a subset of Chrome DevTools Protocol has been implemented by passing the capability "moz:debuggerAddress": true
.
Note: this service currently only supports Chrome v63 and up, Chromium, and Firefox 86 and up! Given that cloud vendors don't expose access to the Chrome DevTools Protocol this service also usually only works when running tests locally or through a Selenium Grid v4 or higher.
Installation
The easiest way is to keep @wdio/devtools-service
as a devDependency in your package.json
, via:
npm install @wdio/devtools-service --save-dev
Instructions on how to install WebdriverIO
can be found here.
Configuration
In order to use the service you just need to add the service to your service list in your wdio.conf.js
, like:
export const config = {
services: ['devtools'],
};
Usage
The @wdio/devtools-service
offers you a variety of features that helps you to automate Chrome beyond the WebDriver protocol. It gives you access to the Chrome DevTools protocol as well as to a Puppeteer instance that you can use to automate Chrome with the Puppeteer automation interface.
Performance Testing
The DevTools service allows you to capture performance data from every page load or page transition that was caused by a click. To enable it call browser.enablePerformanceAudits(<options>)
. After you are done capturing all necessary performance data disable it to revert the throttling settings, e.g.:
import assert from 'node:assert'
describe('JSON.org page', () => {
before(async () => {
await browser.enablePerformanceAudits()
})
it('should load within performance budget', async () => {
await browser.url('http://json.org')
let metrics = await browser.getMetrics()
assert.ok(metrics.speedIndex < 1500)
let score = await browser.getPerformanceScore()
assert.ok(score >= .99)
$('=Esperanto').click()
metrics = await browser.getMetrics()
assert.ok(metrics.speedIndex < 1500)
score = await browser.getPerformanceScore()
assert.ok(score >= .99)
})
after(async () => {
await browser.disablePerformanceAudits()
})
})
You can emulate a mobile device by using the emulateDevice
command, throttling CPU and network as well as setting mobile
as form factor:
await browser.emulateDevice('iPhone X')
await browser.enablePerformanceAudits({
networkThrottling: 'Good 3G',
cpuThrottling: 4,
formFactor: 'mobile'
})
The following commands with their results are available:
getMetrics
Get most common used performance metrics.
console.log(await browser.getMetrics())
getDiagnostics
Get some useful diagnostics about the page load.
console.log(await browser.getDiagnostics())
getMainThreadWorkBreakdown
Returns a list with a breakdown of all main thread task and their total duration.
console.log(await browser.getMainThreadWorkBreakdown())
getPerformanceScore
Returns the Lighthouse Performance Score which is a weighted mean of the following metrics: firstContentfulPaint
, speedIndex
, largestContentfulPaint
, cumulativeLayoutShift
, totalBlockingTime
, interactive
, maxPotentialFID
or cumulativeLayoutShift
.
console.log(await browser.getPerformanceScore())
enablePerformanceAudits
Enables auto performance audits for all page loads that are cause by calling the url
command or clicking on a link or anything that causes a page load. You can pass in a config object to determine some throttling options. The default throttling profile is Good 3G
network with a 4x CPU trottling.
await browser.enablePerformanceAudits({
networkThrottling: 'Good 3G',
cpuThrottling: 4,
cacheEnabled: true,
formFactor: 'mobile'
})
The following network throttling profiles are available: offline
, GPRS
, Regular 2G
, Good 2G
, Regular 3G
, Good 3G
, Regular 4G
, DSL
, Wifi
and online
(no throttling).
Device Emulation
The service allows you to emulate a specific device type. If set, the browser viewport will be modified to fit the device capabilities as well as the user agent will set according to the device user agent. To set a predefined device profile you can run:
await browser.emulateDevice('iPhone X')
Available predefined device profiles are: Blackberry PlayBook
, BlackBerry Z30
, Galaxy Note 3
, Galaxy Note II
, Galaxy S III
, Galaxy S5
, iPad
, iPad Mini
, iPad Pro
, iPhone 4
, iPhone 5
, iPhone 6
, iPhone 6 Plus
, iPhone 7
, iPhone 7 Plus
, iPhone 8
, iPhone 8 Plus
, iPhone SE
, iPhone X
, JioPhone 2
,
Kindle Fire HDX
, LG Optimus L70
, Microsoft Lumia 550
, Microsoft Lumia 950
, Nexus 10
, Nexus 4
, Nexus 5
, Nexus 5X
, Nexus 6
, Nexus 6P
, Nexus 7
, Nokia Lumia 520
, Nokia N9
, Pixel 2
, Pixel 2 XL
You can also define your own device profile by providing an object as parameter like in the following example:
await browser.emulateDevice({
viewport: {
width: 550,
height: 300,
deviceScaleFactor: 1,
isMobile: true,
hasTouch: true,
isLandscape: true
},
userAgent: 'my custom user agent'
})
Note
This only works if you don't use mobileEmulation
within capabilities['goog:chromeOptions']
.
If mobileEmulation
is present the call to browser.emulateDevice()
won't do anything.
PWA Testing
With the checkPWA
command you can validate if your webapp is compliant to latest web standards when it comes to progressive web apps. It checks:
- whether your app is installable
- provides a service worker
- has a splash screen
- provides apple touch and maskable icons
- can be served on mobile devices
If you are not interested in one of these checks you can pass in a list of checks you like to run. The passed
property will return true
if all checks pass. If they fail you can use the details
property to enrich your failure message with details of the failure.
await browser.url('https://webdriver.io')
const result = await browser.checkPWA()
expect(result.passed).toBe(true)
Capture Code Coverage
The service offers you to capture the code coverage of your application under test. To do so you need to enable this feature as part of the service settings:
services: [
['devtools', {
coverageReporter: {
enable: true,
type: 'html',
logDir: __dirname + '/coverage',
exclude: [/resources/]
}
}]
]
Then you have access to a command that calculates the ratio of covered code lines and branches for you to assert within your test:
const coverage = await browser.getCoverageReport()
expect(coverage.lines.total).toBeAbove(0.9)
expect(coverage.statements.total).toBeAbove(0.9)
expect(coverage.functions.total).toBeAbove(0.9)
expect(coverage.branches.total).toBeAbove(0.9)
Chrome DevTools Access
For now the service allows two different ways to access the Chrome DevTools Protocol:
cdp
Command
The cdp
command is a custom command added to the browser scope that allows you to call directly commands to the protocol.
browser.cdp(<domain>, <command>, <arguments>)
For example if you want to get the JavaScript coverage of your page you can do the following:
it('should take JS coverage', async () => {
await browser.cdp('Profiler', 'enable')
await browser.cdp('Debugger', 'enable')
await browser.cdp('Profiler', 'startPreciseCoverage', {
callCount: true,
detailed: true
})
await browser.url('http://google.com')
const { result } = await browser.cdp('Profiler', 'takePreciseCoverage')
const coverage = result.filter((res) => res.url !== '')
console.log(coverage)
})
getNodeId(selector)
and getNodeIds(selector)
Command
Helper method to get the nodeId of an element in the page. NodeIds are similar like WebDriver node ids an identifier for a node. It can be used as a parameter for other Chrome DevTools methods, e.g. DOM.focus
.
const nodeId = await browser.getNodeId('body')
console.log(nodeId)
const nodeId = await browser.getNodeIds('img')
console.log(nodeId)
startTracing(categories, samplingFrequency)
Command
Start tracing the browser. You can optionally pass in custom tracing categories (defaults to this list) and the sampling frequency (defaults to 10000
).
await browser.startTracing()
endTracing
Command
Stop tracing the browser.
await browser.endTracing()
getTraceLogs
Command
Returns the tracelogs that was captured within the tracing period. You can use this command to store the trace logs on the file system to analyse the trace via Chrome DevTools interface.
import fs from 'node:fs/promises'
await browser.startTracing()
await browser.url('http://json.org')
await browser.endTracing()
await fs.writeFile('/path/to/tracelog.json', JSON.stringify(browser.getTraceLogs()))
getPageWeight
Command
Returns page weight information of the last page load.
await browser.startTracing()
await browser.url('https://webdriver.io')
await browser.endTracing()
console.log(await browser.getPageWeight())
Setting Download Paths for the Browser
The cdp
command can be used to call the Page.setDownloadBehavior
command of Devtools Protocol to set the behavior when downloading a file. Make sure the downloadPath
is an absolute path and the browser.cdp()
call is made before the file is downloaded.
await browser.cdp('Page', 'setDownloadBehavior', {
behavior: 'allow',
downloadPath: '/home/root/webdriverio-project/',
});
Access Puppeteer Instance
The service uses Puppeteer for its automation under the hood. You can get access to the used instance by calling the getPuppeteer
command. Note: Puppeteer commands are async and either needs to be called within the call
command or handled via async/await
:
describe('use Puppeteer', () => {
it('by wrapping commands with call', () => {
await browser.url('http://json.org')
const puppeteer = await browser.getPuppeteer()
const page = await browser.call(() => puppeteer.pages())[0]
console.log(await browser.call(() => page.title()))
})
})
Event Listener
In order to capture network events in the browser you can register an event listener to the Chrome DevTools.
A full list of available CDP Network Events.
it('should listen on network events', () => {
await browser.cdp('Network', 'enable')
await browser.on('Network.requestWillBeSent', (event) => {
console.log(`Request: ${event.request.method} ${event.request.url}`);
});
await browser.on('Network.responseReceived', (event) => {
console.log(`Response: ${event.response.status} ${event.response.url}`);
});
await browser.on('Network.loadingFailed', (event) => {
console.log(`Request failed: ${event.errorText}`);
});
await browser.url('https://www.google.com')
})
For more information on WebdriverIO see the homepage.