-
Notifications
You must be signed in to change notification settings - Fork 3
/
chrome-runner.js
50 lines (41 loc) · 1.36 KB
/
chrome-runner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const puppeteer = require('puppeteer');
function getMetrics() {
return {
timing: window.performance.timing.toJSON(),
paint: performance.getEntriesByType('paint').map(mark => mark.toJSON()),
mark: performance.getEntriesByType('mark').map(mark => mark.toJSON())
};
}
module.exports = async function launch({
url,
insecure,
deviceSettings,
networkSettings
}) {
const browser = await puppeteer.launch({ ignoreHTTPSErrors: insecure });
const [page] = await browser.pages();
if (deviceSettings) {
if (deviceSettings.ua) {
await page.setUserAgent(deviceSettings.ua);
}
await page.setViewport({
width: deviceSettings.width,
height: deviceSettings.height,
deviceScaleFactor: deviceSettings.dpr
});
}
if (networkSettings) {
const devTools = await page.target().createCDPSession();
await devTools.send('Network.emulateNetworkConditions', {
offline: false,
downloadThroughput: networkSettings.download,
uploadThroughput: networkSettings.upload,
latency: networkSettings.latency
});
}
await page.goto(url, { waitUntil: 'networkidle2' });
const results = await page.evaluate(getMetrics);
await page.close();
await browser.close();
return results;
};