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!
The easiest way is to keep wdio-devtools-service
as a devDependency in your package.json
.
{
"devDependencies": {
"wdio-devtools-service": "~0.1.1"
}
}
You can simple do it by:
npm install wdio-devtools-service --save-dev
Instructions on how to install WebdriverIO
can be found here.
In order to use the service you just need to add the service to your service list in your wdio.conf.js
like:
// wdio.conf.js
export.config = {
// ...
services: ['devtools'],
// ...
};
For now the service allows two different ways to access the Chrome DevTools Protocol:
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', () => {
/**
* enable necessary domains
*/
browser.cdp('Profiler', 'enable')
browser.cdp('Debugger', 'enable')
/**
* start test coverage profiler
*/
browser.cdp('Profiler', 'startPreciseCoverage', {
callCount: true,
detailed: true
})
browser.url('http://google.com')
/**
* capture test coverage
*/
const { result } = browser.cdp('Profiler', 'takePreciseCoverage')
const coverage = result.filter((res) => res.url !== '')
console.log(coverage)
})
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')
})
All commands can be found in the package.json. The most important are:
Watch changes:
$ npm run watch
Build package:
$ npm build
For more information on WebdriverIO see the homepage.