-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (60 loc) · 1.8 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const fs = require('fs');
const puppeteer = require('puppeteer');
const { promisify } = require('util');
const { harFromMessages } = require('chrome-har');
// list of events
const events = [];
// list of event types to watch
const watchedEvents = [
'Network.dataReceived',
'Network.loadingFailed',
'Network.loadingFinished',
'Network.requestServedFromCache',
'Network.requestWillBeSent',
'Network.resourceChangedPriority',
'Network.responseReceived',
'Page.domContentEventFired',
'Page.frameAttached',
'Page.frameStartedLoading',
'Page.loadEventFired',
];
const grabPerf = async () => {
// take in site from environment var
var urlTarget = process.env.GRAB_SITE;
// set snapshot option
const options = {
path: urlTarget +'.png',
fullpage: true
};
// set the browser flags
// TODO: improve for custom browser args
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--enable-logging', '--v=1'
]
});
// create a page
const page = await browser.newPage();
// create a Chrome Devtools Protocol session
const client = await page.target().createCDPSession();
await client.send('Page.enable');
await client.send('Network.enable');
// for every watched event, push to to the events list
watchedEvents.forEach(method => {
client.on(method, params => {
events.push({ method, params });
});
});
// load the page from the environment
await page.goto('https://'+urlTarget);
// grab screen shot of rendered page
await page.screenshot(options);
// close browser session
await browser.close();
// write out all captured events in .har file format
const har = harFromMessages(events);
await promisify(fs.writeFile)(urlTarget+'.har', JSON.stringify(har));
}
grabPerf();