forked from ceckoslab/inp-measure-puppeteer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·134 lines (103 loc) · 3.49 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const fs = require("fs");
const path = require("path");
const puppeteer = require("puppeteer");
// const { PuppeteerScreenRecorder } = require("puppeteer-screen-recorder");
const recording = false;
const Cookies = require("./src/Cookies");
const CWV = require("./src/CWV");
const DeviceEmulation = require("./src/DeviceEmulation");
// TODO, Change here to the element you would like Puppeteer to interact with.
const elementToInteractWith = "A.wp-block-button__link";
// TODO, Change here to the url you would like to load in Puppeteer.
//add ?nowprocket=1 to override wp rocket
const navigateTo = "https://www.spendwithpennies.com/";
var args = process.argv.slice(2);
let experiment = 0;
if (args[0]) {
const paramParts = args[0].split("=");
if (paramParts[0] === "experiment") {
experiment = parseInt(paramParts[1]);
}
}
if (experiment === 0) {
console.log("Running on the original page.");
}
else {
console.log("Running experiment {" + experiment + "} on a modified page.");
}
(async() => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
if (recording) {
// Configure the screen recorder
const recorder = new PuppeteerScreenRecorder(page, {
followNewTab: true,
fps: 30,
videoFrame: {
width: 1080,
height: 2400
}
// Other configurations
});
await recorder.start("output.mp4");
}
await DeviceEmulation.emulate(page);
// Listen for console events within the page and log them in Node.js context
page.on("console", (msg) => {
console.log(msg.text());
});
// Enable request interception
await page.setRequestInterception(true);
// Add event listener to intercept requests
page.on("request", (interceptedRequest) => {
// Check if the request is for the resource you want to override
if (interceptedRequest.url().endsWith("example-js-of-interest.js")) {
console.log("Intercepted and overriding: " + interceptedRequest.url());
// Create a response from a local file
const overrideContent = fs.readFileSync(path.join(__dirname, "overrides", "example-js-of-interest.js"), "utf8");
interceptedRequest.respond({
status: 200,
contentType: "text/javascript; charset=utf-8",
body: overrideContent
});
return;
}
// Allow all other requests to continue normally
interceptedRequest.continue();
});
// Navigate to the page
await page.goto(navigateTo);
await CWV.attachCWV_Lib(page);
await Cookies.setOptanonConsent(page);
// Wait for a specified timeout in milliseconds
await page.waitForTimeout(5000); // waits for 5 seconds
// Wait for the element to be present in the DOM
await page.waitForSelector(elementToInteractWith);
// Click the input input
await page.click(elementToInteractWith);
await page.focus(elementToInteractWith);
await page.waitForTimeout(5); // waits for 5 seconds
// Execute JS code after the timeout
await page.evaluateHandle(() => {
window.webVitals.getINP(function(info) {
if (info.value) {
console.log("inp: " + info.value);
}
else {
console.log("inp: not measured");
}
},
{
reportAllChanges: true
}
);
});
await page.waitForTimeout(5000); // waits for 5 seconds
if (recording) {
// Stop recording
await recorder.stop();
}
// You can close the browser after a timeout, or after some specific action
// that would ensure all metrics have been captured.
await browser.close();
})();