-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
user-flow.js
354 lines (304 loc) · 11.4 KB
/
user-flow.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {ReportGenerator} from '../report/generator/report-generator.js';
import {snapshotGather} from './gather/snapshot-runner.js';
import {startTimespanGather} from './gather/timespan-runner.js';
import {navigationGather} from './gather/navigation-runner.js';
import {Runner} from './runner.js';
import {initializeConfig} from './config/config.js';
import {getFormatted} from '../shared/localization/format.js';
import {mergeConfigFragment, deepClone} from './config/config-helpers.js';
import * as i18n from './lib/i18n/i18n.js';
import * as LH from '../types/lh.js';
/** @typedef {WeakMap<LH.UserFlow.GatherStep, LH.Gatherer.GatherResult['runnerOptions']>} GatherStepRunnerOptions */
const UIStrings = {
/**
* @description Default name for a user flow on the given url. "User flow" refers to the series of page navigations and user interactions being tested on the page. "url" is a trimmed version of a url that only includes the domain name.
* @example {example.com} url
*/
defaultFlowName: 'User flow ({url})',
/**
* @description Default name for a Lighthouse report that analyzes a page navigation. "url" is a trimmed version of a url that only includes the domain name and path.
* @example {example.com/page} url
*/
defaultNavigationName: 'Navigation report ({url})',
/**
* @description Default name for a Lighthouse report that analyzes user interactions over a period of time. "url" is a trimmed version of a url that only includes the domain name and path.
* @example {example.com/page} url
*/
defaultTimespanName: 'Timespan report ({url})',
/**
* @description Default name for a Lighthouse report that analyzes the page state at a point in time. "url" is a trimmed version of a url that only includes the domain name and path.
* @example {example.com/page} url
*/
defaultSnapshotName: 'Snapshot report ({url})',
};
const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings);
/**
* @param {string} message
* @param {Record<string, string | number>} values
* @param {LH.Locale} locale
*/
function translate(message, values, locale) {
const icuMessage = str_(message, values);
return getFormatted(icuMessage, locale);
}
class UserFlow {
/**
* @param {LH.Puppeteer.Page} page
* @param {LH.UserFlow.Options} [options]
*/
constructor(page, options) {
/** @type {LH.Puppeteer.Page} */
this._page = page;
/** @type {LH.UserFlow.Options|undefined} */
this._options = options;
/** @type {LH.UserFlow.GatherStep[]} */
this._gatherSteps = [];
/** @type {GatherStepRunnerOptions} */
this._gatherStepRunnerOptions = new WeakMap();
}
/**
* @param {LH.UserFlow.StepFlags|undefined} flags
* @return {LH.UserFlow.StepFlags|undefined}
*/
_getNextFlags(flags) {
const clonedFlowFlags = this._options?.flags && deepClone(this._options?.flags);
const mergedFlags = mergeConfigFragment(clonedFlowFlags || {}, flags || {}, true);
if (mergedFlags.usePassiveGathering === undefined) {
mergedFlags.usePassiveGathering = true;
}
return mergedFlags;
}
/**
* @param {LH.UserFlow.StepFlags|undefined} flags
* @return {LH.UserFlow.StepFlags}
*/
_getNextNavigationFlags(flags) {
const newStepFlags = this._getNextFlags(flags) || {};
if (newStepFlags.skipAboutBlank === undefined) {
newStepFlags.skipAboutBlank = true;
}
// On repeat navigations, we want to disable storage reset by default (i.e. it's not a cold load).
const isSubsequentNavigation = this._gatherSteps
.some(step => step.artifacts.GatherContext.gatherMode === 'navigation');
if (isSubsequentNavigation) {
if (newStepFlags.disableStorageReset === undefined) {
newStepFlags.disableStorageReset = true;
}
}
return newStepFlags;
}
/**
* @param {LH.Gatherer.GatherResult} gatherResult
* @param {LH.UserFlow.StepFlags} [flags]
*/
_addGatherStep(gatherResult, flags) {
const gatherStep = {
artifacts: gatherResult.artifacts,
flags,
};
this._gatherSteps.push(gatherStep);
this._gatherStepRunnerOptions.set(gatherStep, gatherResult.runnerOptions);
}
/**
* @param {LH.NavigationRequestor} requestor
* @param {LH.UserFlow.StepFlags} [flags]
*/
async navigate(requestor, flags) {
if (this.currentTimespan) throw new Error('Timespan already in progress');
if (this.currentNavigation) throw new Error('Navigation already in progress');
const nextFlags = this._getNextNavigationFlags(flags);
const gatherResult = await navigationGather(this._page, requestor, {
config: this._options?.config,
flags: nextFlags,
});
this._addGatherStep(gatherResult, nextFlags);
}
/**
* This is an alternative to `navigate()` that can be used to analyze a navigation triggered by user interaction.
* For more on user triggered navigations, see https://github.com/GoogleChrome/lighthouse/blob/main/docs/user-flows.md#triggering-a-navigation-via-user-interactions.
*
* @param {LH.UserFlow.StepFlags} [stepOptions]
*/
async startNavigation(stepOptions) {
/** @type {(value: () => void) => void} */
let completeSetup;
/** @type {(value: any) => void} */
let rejectDuringSetup;
// This promise will resolve once the setup is done
// and Lighthouse is waiting for a page navigation to be triggered.
const navigationSetupPromise = new Promise((resolve, reject) => {
completeSetup = resolve;
rejectDuringSetup = reject;
});
// The promise in this callback will not resolve until `continueNavigation` is invoked,
// because `continueNavigation` is passed along to `navigateSetupPromise`
// and extracted into `continueAndAwaitResult` below.
const navigationResultPromise = this.navigate(
() => new Promise(continueNavigation => completeSetup(continueNavigation)),
stepOptions
).catch(err => {
if (this.currentNavigation) {
// If the navigation already started, re-throw the error so it is emitted when `navigationResultPromise` is awaited.
throw err;
} else {
// If the navigation has not started, reject the `navigationSetupPromise` so the error throws when it is awaited in `startNavigation`.
rejectDuringSetup(err);
}
});
const continueNavigation = await navigationSetupPromise;
async function continueAndAwaitResult() {
continueNavigation();
await navigationResultPromise;
}
this.currentNavigation = {continueAndAwaitResult};
}
async endNavigation() {
if (this.currentTimespan) throw new Error('Timespan already in progress');
if (!this.currentNavigation) throw new Error('No navigation in progress');
await this.currentNavigation.continueAndAwaitResult();
this.currentNavigation = undefined;
}
/**
* @param {LH.UserFlow.StepFlags} [flags]
*/
async startTimespan(flags) {
if (this.currentTimespan) throw new Error('Timespan already in progress');
if (this.currentNavigation) throw new Error('Navigation already in progress');
const nextFlags = this._getNextFlags(flags);
const timespan = await startTimespanGather(this._page, {
config: this._options?.config,
flags: nextFlags,
});
this.currentTimespan = {timespan, flags: nextFlags};
}
async endTimespan() {
if (!this.currentTimespan) throw new Error('No timespan in progress');
if (this.currentNavigation) throw new Error('Navigation already in progress');
const {timespan, flags} = this.currentTimespan;
const gatherResult = await timespan.endTimespanGather();
this.currentTimespan = undefined;
this._addGatherStep(gatherResult, flags);
}
/**
* @param {LH.UserFlow.StepFlags} [flags]
*/
async snapshot(flags) {
if (this.currentTimespan) throw new Error('Timespan already in progress');
if (this.currentNavigation) throw new Error('Navigation already in progress');
const nextFlags = this._getNextFlags(flags);
const gatherResult = await snapshotGather(this._page, {
config: this._options?.config,
flags: nextFlags,
});
this._addGatherStep(gatherResult, nextFlags);
}
/**
* @returns {Promise<LH.FlowResult>}
*/
async createFlowResult() {
return auditGatherSteps(this._gatherSteps, {
name: this._options?.name,
config: this._options?.config,
gatherStepRunnerOptions: this._gatherStepRunnerOptions,
});
}
/**
* @return {Promise<string>}
*/
async generateReport() {
const flowResult = await this.createFlowResult();
return ReportGenerator.generateFlowReportHtml(flowResult);
}
/**
* @return {LH.UserFlow.FlowArtifacts}
*/
createArtifactsJson() {
return {
gatherSteps: this._gatherSteps,
name: this._options?.name,
};
}
}
/**
* @param {string} longUrl
* @returns {string}
*/
function shortenUrl(longUrl) {
const url = new URL(longUrl);
return `${url.hostname}${url.pathname}`;
}
/**
* @param {LH.UserFlow.StepFlags|undefined} flags
* @param {LH.Artifacts} artifacts
* @return {string}
*/
function getStepName(flags, artifacts) {
if (flags?.name) return flags.name;
const {locale} = artifacts.settings;
const shortUrl = shortenUrl(artifacts.URL.finalDisplayedUrl);
switch (artifacts.GatherContext.gatherMode) {
case 'navigation':
return translate(UIStrings.defaultNavigationName, {url: shortUrl}, locale);
case 'timespan':
return translate(UIStrings.defaultTimespanName, {url: shortUrl}, locale);
case 'snapshot':
return translate(UIStrings.defaultSnapshotName, {url: shortUrl}, locale);
default:
throw new Error('Unsupported gather mode');
}
}
/**
* @param {string|undefined} name
* @param {LH.UserFlow.GatherStep[]} gatherSteps
* @return {string}
*/
function getFlowName(name, gatherSteps) {
if (name) return name;
const firstArtifacts = gatherSteps[0].artifacts;
const {locale} = firstArtifacts.settings;
const url = new URL(firstArtifacts.URL.finalDisplayedUrl).hostname;
return translate(UIStrings.defaultFlowName, {url}, locale);
}
/**
* @param {Array<LH.UserFlow.GatherStep>} gatherSteps
* @param {{name?: string, config?: LH.Config, gatherStepRunnerOptions?: GatherStepRunnerOptions}} options
*/
async function auditGatherSteps(gatherSteps, options) {
if (!gatherSteps.length) {
throw new Error('Need at least one step before getting the result');
}
/** @type {LH.FlowResult['steps']} */
const steps = [];
for (const gatherStep of gatherSteps) {
const {artifacts, flags} = gatherStep;
const name = getStepName(flags, artifacts);
let runnerOptions = options.gatherStepRunnerOptions?.get(gatherStep);
// If the gather step is not active, we must recreate the runner options.
if (!runnerOptions) {
// Step specific configs take precedence over a config for the entire flow.
const config = options.config;
const {gatherMode} = artifacts.GatherContext;
const {resolvedConfig} = await initializeConfig(gatherMode, config, flags);
runnerOptions = {
resolvedConfig,
computedCache: new Map(),
};
}
const result = await Runner.audit(artifacts, runnerOptions);
if (!result) throw new Error(`Step "${name}" did not return a result`);
steps.push({lhr: result.lhr, name});
}
return {steps, name: getFlowName(options.name, gatherSteps)};
}
export {
UserFlow,
auditGatherSteps,
getStepName,
getFlowName,
UIStrings,
};