This repository was archived by the owner on Apr 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
283 lines (262 loc) · 7.35 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const lighthouse = require('lighthouse/core/index.cjs');
const config = require('../../config');
const utils = require('../../utils');
const CheckReportCustomData = require('../../report/CheckReportCustomData');
/**
* @param {HTMLElement} el
*/
function getInnerText(el) {
return el.innerText;
}
/**
* Checks that selector's innerText contains the target text
*
* @param {import('../context').ActionContext} context
* @param {string} selector
* @param {string} targetText
*/
exports.selectorContains = async (context, selector, targetText) => {
await context.page.$eval(selector, getInnerText).then((content) => {
if (content.includes(targetText)) {
return;
}
throw new Error(`Element '${selector}' does not contain '${targetText}'`);
});
};
/**
* Checks that selector's innerText doesn't contain target text
*
* @param {import('../context').ActionContext} context
* @param {string} selector
* @param {string} targetText
*/
exports.selectorNotContains = async (context, selector, targetText) => {
await context.page.$eval(selector, getInnerText).then((content) => {
if (!content.includes(targetText)) {
return;
}
throw new Error(`Element '${selector}' should not contain '${targetText}'`);
});
};
/**
* Returns iframe with specified selector
*
* @param {import('../context').ActionContext} context
* @param {string} frameSelector
* @param {string} selector
*/
exports.getFrame = async (context, frameSelector) => {
let frame;
try {
const frameElement = await context.page.waitForSelector(frameSelector);
frame = await frameElement.contentFrame();
if (!frame) {
throw new Error(`Selector '${frameSelector}' is not IFrame`);
}
} catch (err) {
throw utils.enrichError(
err,
`IFrame '${frameSelector}' not found: ${err.message}`
);
}
return frame;
};
/**
* Checks that iframe contains specified selector
*
* @param {import('../context').ActionContext} context
* @param {string} frameSelector
* @param {string} selector
*/
exports.frameWaitForSelector = async (context, frameSelector, selector) => {
try {
const frame = await exports.getFrame(context, frameSelector, selector);
await frame.waitForSelector(selector);
} catch (err) {
throw utils.enrichError(
err,
`Selector '${selector}' not found in IFrame '${frameSelector}': ` +
`${err.message}`
);
}
};
/**
* Checks that iframe selector's innerText contains the target text
*
* @param {import('../context').ActionContext} context
* @param {string} frameSelector
* @param {string} selector
* @param {string} targetText
*/
exports.frameSelectorContains = async (
context,
frameSelector,
selector,
targetText
) => {
try {
const frame = await exports.getFrame(context, frameSelector, selector);
await frame.$eval(selector, getInnerText).then((content) => {
if (content.includes(targetText)) {
return;
}
throw new Error(`Element '${selector}' does not contain '${targetText}'`);
});
} catch (err) {
throw utils.enrichError(
err,
`IFrame '${frameSelector}' selector '${selector}' does not contain ` +
`'${targetText}': ${err.message}`
);
}
};
/**
* Checks that iframe selector's innerText doesn't contain target text
*
* @param {import('../context').ActionContext} context
* @param {string} frameSelector
* @param {string} selector
* @param {string} targetText
*/
exports.frameSelectorNotContains = async (
context,
frameSelector,
selector,
targetText
) => {
try {
const frame = await exports.getFrame(context, frameSelector, selector);
await frame.$eval(selector, getInnerText).then((content) => {
if (!content.includes(targetText)) {
return;
}
throw new Error(
`Element '${selector}' should not contain '${targetText}'`
);
});
} catch (err) {
throw utils.enrichError(
err,
`IFrame '${frameSelector}' selector '${selector}' should not contain ` +
`'${targetText}': ${err.message}`
);
}
};
/**
* Logs lighthouse report performance metrics
*
* @param {import('../context').ActionContext} context
* @param {string} id Id label for reported metrics
* @param {string} url URL to measure
*
* @returns {Promise<CheckReportCustomData>}
*/
exports.runLighthouse = async (context, id, url) => {
if (config.traces) {
throw new Error('traces config option must be false to run Lighthouse');
}
if (!config.chromiumRemoteDebugging) {
throw new Error(
'chromiumRemoteDebugging config option must be true to run Lighthouse'
);
}
const result = await lighthouse(url, {
port: config.chromiumRemoteDebuggingPort,
onlyCategories: ['performance'],
});
const { lhr } = result;
const customReport = new CheckReportCustomData();
customReport.metrics = [
{
value: lhr.audits['first-contentful-paint'].numericValue,
labels: { name: 'first-contentful-paint', id },
},
{
value: lhr.audits['largest-contentful-paint'].numericValue,
labels: { name: 'largest-contentful-paint', id },
},
{
value: lhr.audits['first-meaningful-paint'].numericValue,
labels: { name: 'first-meaningful-paint', id },
},
{
value: lhr.audits['speed-index'].numericValue,
labels: { name: 'speed-index', id },
},
{
value: lhr.audits['server-response-time'].numericValue,
labels: { name: 'server-response-time', id },
},
{
value: lhr.audits.interactive.numericValue,
labels: { name: 'interactive', id },
},
{
value: lhr.audits['total-blocking-time'].numericValue,
labels: { name: 'total-blocking-time', id },
},
{
value: lhr.audits['cumulative-layout-shift'].numericValue,
labels: { name: 'cumulative-layout-shift', id },
},
];
return customReport;
};
/**
* Logs browser performance metrics
*
* @param {import('../context').ActionContext} context
* @param {string} id Id label for reported metrics
*
* @returns {Promise<CheckReportCustomData>}
*/
exports.logPerformanceMetrics = async (context, id) => {
const firstPaint = JSON.parse(
await context.page.evaluate(() =>
JSON.stringify(window.performance.getEntriesByName('first-paint'))
)
);
const firstContentfulPaint = JSON.parse(
await context.page.evaluate(() =>
JSON.stringify(
window.performance.getEntriesByName('first-contentful-paint')
)
)
);
const windowPerformance = JSON.parse(
await context.page.evaluate(() =>
JSON.stringify(window.performance.toJSON())
)
);
const customReport = new CheckReportCustomData();
customReport.metrics = [
{
value: firstPaint[0].startTime,
labels: { name: 'first-paint', id },
},
{
value: firstContentfulPaint[0].startTime,
labels: { name: 'first-contentful-paint', id },
},
{
value:
windowPerformance.timing.domainLookupEnd -
windowPerformance.timing.domainLookupStart,
labels: { name: 'domain-lookup', id },
},
{
value:
windowPerformance.timing.connectEnd -
windowPerformance.timing.connectStart,
labels: { name: 'connect', id },
},
{
value:
windowPerformance.timing.responseStart -
windowPerformance.timing.requestStart,
labels: { name: 'server-response-time', id },
},
];
return customReport;
};
module.exports = exports;