This repository was archived by the owner on Feb 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (34 loc) · 1.26 KB
/
index.js
File metadata and controls
38 lines (34 loc) · 1.26 KB
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
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const fs = require('fs');
const path = require('path');
const TEST_URL = 'https://m.tokopedia.com/';
function launchChromeAndRunLighthouse(url, opts, config = null) {
return chromeLauncher.launch({ chromeFlags: opts.chromeFlags }).then(chrome => {
opts.port = chrome.port;
return lighthouse(url, opts, config).then(results => {
const date = new Date().toISOString();
const p = path.resolve(`./reports/report-${date}.json`);
console.log('Report created on', p);
fs.writeFile(p, results.report, () => { });
// use results.lhr for the JS-consumable output
// https://github.com/GoogleChrome/lighthouse/blob/master/types/lhr.d.ts
// use results.report for the HTML/JSON/CSV output as a string
// use results.artifacts for the trace/screenshots/other specific case you need (rarer)
return chrome.kill().then(() => results.lhr)
});
});
}
const opts = {
quiet: true,
headless: true,
disableStorageReset: true,
emulatedFormFactor: 'mobile',
onlyCategories: ['performance'],
blockedUrlPatterns: '*.googletagmanager',
chromeFlags: ['--show-paint-rects', '--headless']
};
// Usage:
launchChromeAndRunLighthouse(TEST_URL, opts).then(results => {
// Use results!
});