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.
Note: this service currently only supports Chrome v63 and up!
Installation
The easiest way is to keep @wdio/devtools-service
as a devDependency in your package.json
.
{
"devDependencies": {
"@wdio/devtools-service": "^5.0.0"
}
}
You can simple do it by:
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.config = {
services: [['devtools', {
debuggerAddress: '10.0.0.3:9222'
}]],
};
debuggerAddress
- optional parameter, you could set host and port.
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.:
const assert = require('assert')
describe('JSON.org page', () => {
before(() => {
browser.enablePerformanceAudits()
})
it('should load within performance budget', () => {
browser.url('http://json.org')
let metrics = browser.getMetrics()
assert.ok(metrics.speedIndex < 1500)
let score = browser.getPerformanceScore()
assert.ok(score >= .99)
$('=Esperanto').click()
metrics = browser.getMetrics()
assert.ok(metrics.speedIndex < 1500)
score = browser.getPerformanceScore()
assert.ok(score >= .99)
})
after(() => {
browser.disablePerformanceAudits()
})
})
The following commands with their results are available:
getMetrics
Get most common used performance metrics.
console.log(browser.getMetrics())
getDiagnostics
Get some useful diagnostics about the page load.
console.log(browser.getDiagnostics())
getMainThreadWorkBreakdown
Returns a list with a breakdown of all main thread task and their total duration.
console.log(browser.getMainThreadWorkBreakdown())
getPerformanceScore
Returns the Lighthouse Performance Score which is a weighted mean of the following metrics: firstMeaningfulPaint
, firstCPUIdle
, firstInteractive
, speedIndex
, estimatedInputLatency
.
console.log(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.
browser.enablePerformanceAudits({
networkThrottling: 'Good 3G',
cpuThrottling: 4,
cacheEnabled: true
})
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).
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', () => {
browser.cdp('Profiler', 'enable')
browser.cdp('Debugger', 'enable')
browser.cdp('Profiler', 'startPreciseCoverage', {
callCount: true,
detailed: true
})
browser.url('http://google.com')
const { result } = browser.cdp('Profiler', 'takePreciseCoverage')
const coverage = result.filter((res) => res.url !== '')
console.log(coverage)
})
cdpConnection
Command
Returns the host and port the Chrome DevTools interface is connected to.
const connection = browser.cdpConnection()
console.log(connection);
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 = browser.getNodeId('body')
console.log(nodeId)
const nodeId = 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
).
browser.startTracing()
endTracing
Command
Stop tracing the browser.
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.
browser.startTracing()
browser.url('http://json.org')
browser.endTracing()
fs.writeFileSync('/path/to/tracelog.json', JSON.stringify(browser.getTraceLogs()))
getPageWeight
Command
Returns page weight information of the last page load.
browser.startTracing()
browser.url('https://webdriver.io')
browser.endTracing()
console.log(browser.getPageWeight())
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', () => {
browser.url('http://json.org')
const puppeteer = browser.getPuppeteer()
const page = browser.call(() => puppeteer.browser.pages())[0]
console.log(browser.call(() => page.title()))
})
it('by using async/await', async () => {
const puppeteer = browser.getPuppeteer()
const page = (await puppeteer.browser.pages())[0]
console.log(await page.title())
})
})
Event Listener
In order to capture events in the browser you can register an event listener to a Chrome DevTools event like:
it('should listen on network events', () => {
browser.cdp('Network', 'enable')
browser.on('Network.responseReceived', (params) => {
console.log(`Loaded ${params.response.url}`)
})
browser.url('https://www.google.com')
})
For more information on WebdriverIO see the homepage.