@wordpress/e2e-test-utils-playwright 1.47.0 → 1.48.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/admin/create-new-post.ts +47 -0
- package/src/admin/edit-post.ts +24 -0
- package/{build/admin/get-page-error.js → src/admin/get-page-error.ts} +12 -9
- package/src/admin/index.ts +52 -0
- package/src/admin/visit-admin-page.ts +43 -0
- package/src/admin/visit-site-editor.ts +62 -0
- package/src/admin/wait-for-site-editor.ts +52 -0
- package/src/config.ts +12 -0
- package/src/editor/click-block-options-menu-item.ts +18 -0
- package/src/editor/click-block-toolbar-button.ts +21 -0
- package/src/editor/get-blocks.ts +75 -0
- package/src/editor/get-edited-post-content.ts +19 -0
- package/src/editor/index.ts +95 -0
- package/src/editor/insert-block.ts +54 -0
- package/src/editor/open-document-settings-sidebar.ts +31 -0
- package/src/editor/preview.ts +32 -0
- package/src/editor/publish-post.ts +51 -0
- package/src/editor/save-draft.ts +20 -0
- package/src/editor/select-blocks.ts +48 -0
- package/src/editor/set-content.ts +24 -0
- package/src/editor/set-is-fixed-toolbar.ts +20 -0
- package/src/editor/set-preferences.ts +37 -0
- package/src/editor/show-block-toolbar.ts +20 -0
- package/src/editor/site-editor.ts +55 -0
- package/src/editor/switch-editor-tool.ts +34 -0
- package/src/editor/switch-to-legacy-canvas.ts +20 -0
- package/src/editor/transform-block-to.ts +34 -0
- package/src/index.ts +8 -0
- package/src/lighthouse/index.ts +75 -0
- package/src/metrics/index.ts +441 -0
- package/src/page-utils/drag-files.ts +173 -0
- package/src/page-utils/emulate-network-conditions.ts +101 -0
- package/src/page-utils/index.ts +54 -0
- package/src/page-utils/is-current-url.ts +20 -0
- package/src/page-utils/keycodes.ts +48 -0
- package/src/page-utils/press-keys.ts +195 -0
- package/{build → src}/page-utils/set-browser-viewport.js +14 -12
- package/src/request-utils/blocks.ts +58 -0
- package/src/request-utils/comments.ts +80 -0
- package/src/request-utils/gutenberg-experiments.ts +62 -0
- package/src/request-utils/index.ts +222 -0
- package/src/request-utils/login.ts +34 -0
- package/src/request-utils/media.ts +102 -0
- package/src/request-utils/menus.ts +145 -0
- package/src/request-utils/pages.ts +75 -0
- package/src/request-utils/patterns.ts +31 -0
- package/src/request-utils/plugins.ts +103 -0
- package/src/request-utils/posts.ts +69 -0
- package/src/request-utils/preferences.ts +63 -0
- package/{build/request-utils/records.js → src/request-utils/records.ts} +15 -10
- package/src/request-utils/rest.ts +222 -0
- package/src/request-utils/site-settings.ts +58 -0
- package/src/request-utils/templates.ts +83 -0
- package/src/request-utils/themes.ts +110 -0
- package/src/request-utils/users.ts +125 -0
- package/src/request-utils/widgets.js +68 -0
- package/src/test.ts +204 -0
- package/src/types.ts +13 -0
- package/build/admin/create-new-post.js +0 -31
- package/build/admin/create-new-post.js.map +0 -1
- package/build/admin/edit-post.js +0 -20
- package/build/admin/edit-post.js.map +0 -1
- package/build/admin/get-page-error.js.map +0 -1
- package/build/admin/index.js +0 -40
- package/build/admin/index.js.map +0 -1
- package/build/admin/visit-admin-page.js +0 -32
- package/build/admin/visit-admin-page.js.map +0 -1
- package/build/admin/visit-site-editor.js +0 -45
- package/build/admin/visit-site-editor.js.map +0 -1
- package/build/admin/wait-for-site-editor.js +0 -41
- package/build/admin/wait-for-site-editor.js.map +0 -1
- package/build/config.js +0 -13
- package/build/config.js.map +0 -1
- package/build/editor/click-block-options-menu-item.js +0 -17
- package/build/editor/click-block-options-menu-item.js.map +0 -1
- package/build/editor/click-block-toolbar-button.js +0 -16
- package/build/editor/click-block-toolbar-button.js.map +0 -1
- package/build/editor/get-blocks.js +0 -45
- package/build/editor/get-blocks.js.map +0 -1
- package/build/editor/get-edited-post-content.js +0 -15
- package/build/editor/get-edited-post-content.js.map +0 -1
- package/build/editor/index.js +0 -75
- package/build/editor/index.js.map +0 -1
- package/build/editor/insert-block.js +0 -23
- package/build/editor/insert-block.js.map +0 -1
- package/build/editor/open-document-settings-sidebar.js +0 -27
- package/build/editor/open-document-settings-sidebar.js.map +0 -1
- package/build/editor/preview.js +0 -21
- package/build/editor/preview.js.map +0 -1
- package/build/editor/publish-post.js +0 -45
- package/build/editor/publish-post.js.map +0 -1
- package/build/editor/save-draft.js +0 -18
- package/build/editor/save-draft.js.map +0 -1
- package/build/editor/select-blocks.js +0 -28
- package/build/editor/select-blocks.js.map +0 -1
- package/build/editor/set-content.js +0 -17
- package/build/editor/set-content.js.map +0 -1
- package/build/editor/set-is-fixed-toolbar.js +0 -18
- package/build/editor/set-is-fixed-toolbar.js.map +0 -1
- package/build/editor/set-preferences.js +0 -21
- package/build/editor/set-preferences.js.map +0 -1
- package/build/editor/show-block-toolbar.js +0 -19
- package/build/editor/show-block-toolbar.js.map +0 -1
- package/build/editor/site-editor.js +0 -44
- package/build/editor/site-editor.js.map +0 -1
- package/build/editor/switch-editor-tool.js +0 -33
- package/build/editor/switch-editor-tool.js.map +0 -1
- package/build/editor/switch-to-legacy-canvas.js +0 -18
- package/build/editor/switch-to-legacy-canvas.js.map +0 -1
- package/build/editor/transform-block-to.js +0 -24
- package/build/editor/transform-block-to.js.map +0 -1
- package/build/index.js +0 -19
- package/build/index.js.map +0 -1
- package/build/lighthouse/index.js +0 -61
- package/build/lighthouse/index.js.map +0 -1
- package/build/metrics/index.js +0 -314
- package/build/metrics/index.js.map +0 -1
- package/build/page-utils/drag-files.js +0 -119
- package/build/page-utils/drag-files.js.map +0 -1
- package/build/page-utils/emulate-network-conditions.js +0 -71
- package/build/page-utils/emulate-network-conditions.js.map +0 -1
- package/build/page-utils/index.js +0 -37
- package/build/page-utils/index.js.map +0 -1
- package/build/page-utils/is-current-url.js +0 -21
- package/build/page-utils/is-current-url.js.map +0 -1
- package/build/page-utils/keycodes.js +0 -41
- package/build/page-utils/keycodes.js.map +0 -1
- package/build/page-utils/press-keys.js +0 -141
- package/build/page-utils/press-keys.js.map +0 -1
- package/build/page-utils/set-browser-viewport.js.map +0 -1
- package/build/request-utils/blocks.js +0 -40
- package/build/request-utils/blocks.js.map +0 -1
- package/build/request-utils/comments.js +0 -52
- package/build/request-utils/comments.js.map +0 -1
- package/build/request-utils/gutenberg-experiments.js +0 -47
- package/build/request-utils/gutenberg-experiments.js.map +0 -1
- package/build/request-utils/index.js +0 -196
- package/build/request-utils/index.js.map +0 -1
- package/build/request-utils/login.js +0 -21
- package/build/request-utils/login.js.map +0 -1
- package/build/request-utils/media.js +0 -106
- package/build/request-utils/media.js.map +0 -1
- package/build/request-utils/menus.js +0 -116
- package/build/request-utils/menus.js.map +0 -1
- package/build/request-utils/pages.js +0 -53
- package/build/request-utils/pages.js.map +0 -1
- package/build/request-utils/patterns.js +0 -27
- package/build/request-utils/patterns.js.map +0 -1
- package/build/request-utils/plugins.js +0 -85
- package/build/request-utils/plugins.js.map +0 -1
- package/build/request-utils/posts.js +0 -42
- package/build/request-utils/posts.js.map +0 -1
- package/build/request-utils/preferences.js +0 -54
- package/build/request-utils/preferences.js.map +0 -1
- package/build/request-utils/records.js.map +0 -1
- package/build/request-utils/rest.js +0 -181
- package/build/request-utils/rest.js.map +0 -1
- package/build/request-utils/site-settings.js +0 -33
- package/build/request-utils/site-settings.js.map +0 -1
- package/build/request-utils/templates.js +0 -54
- package/build/request-utils/templates.js.map +0 -1
- package/build/request-utils/themes.js +0 -69
- package/build/request-utils/themes.js.map +0 -1
- package/build/request-utils/users.js +0 -81
- package/build/request-utils/users.js.map +0 -1
- package/build/request-utils/widgets.js +0 -61
- package/build/request-utils/widgets.js.map +0 -1
- package/build/test.js +0 -189
- package/build/test.js.map +0 -1
- package/build/types.js +0 -3
- package/build/types.js.map +0 -1
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { mkdir, writeFile } from 'fs/promises';
|
|
5
|
+
import { join } from 'path';
|
|
6
|
+
import type { Page, Browser } from '@playwright/test';
|
|
7
|
+
// resolution-mode support in TypeScript 5.3 will resolve this.
|
|
8
|
+
// See https://devblogs.microsoft.com/typescript/announcing-typescript-5-3-beta/
|
|
9
|
+
// @ts-expect-error
|
|
10
|
+
import type { Metric } from 'web-vitals';
|
|
11
|
+
|
|
12
|
+
type EventType =
|
|
13
|
+
| 'click'
|
|
14
|
+
| 'focus'
|
|
15
|
+
| 'focusin'
|
|
16
|
+
| 'keydown'
|
|
17
|
+
| 'keypress'
|
|
18
|
+
| 'keyup'
|
|
19
|
+
| 'mouseout'
|
|
20
|
+
| 'mouseover';
|
|
21
|
+
|
|
22
|
+
interface TraceEvent {
|
|
23
|
+
cat: string;
|
|
24
|
+
name: string;
|
|
25
|
+
dur?: number;
|
|
26
|
+
args: {
|
|
27
|
+
data?: {
|
|
28
|
+
type: EventType;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface Trace {
|
|
34
|
+
traceEvents: TraceEvent[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type MetricsConstructorProps = {
|
|
38
|
+
page: Page;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
interface WebVitalsMeasurements {
|
|
42
|
+
CLS?: number;
|
|
43
|
+
FCP?: number;
|
|
44
|
+
FID?: number;
|
|
45
|
+
INP?: number;
|
|
46
|
+
LCP?: number;
|
|
47
|
+
TTFB?: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class Metrics {
|
|
51
|
+
browser: Browser;
|
|
52
|
+
page: Page;
|
|
53
|
+
trace: Trace;
|
|
54
|
+
|
|
55
|
+
webVitals: WebVitalsMeasurements = {};
|
|
56
|
+
|
|
57
|
+
constructor( { page }: MetricsConstructorProps ) {
|
|
58
|
+
this.page = page;
|
|
59
|
+
this.browser = page.context().browser()!;
|
|
60
|
+
this.trace = { traceEvents: [] };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns durations from the Server-Timing header.
|
|
65
|
+
*
|
|
66
|
+
* @param fields Optional fields to filter.
|
|
67
|
+
*/
|
|
68
|
+
async getServerTiming( fields: string[] = [] ) {
|
|
69
|
+
return this.page.evaluate< Record< string, number >, string[] >(
|
|
70
|
+
( f: string[] ) =>
|
|
71
|
+
(
|
|
72
|
+
performance.getEntriesByType(
|
|
73
|
+
'navigation'
|
|
74
|
+
) as PerformanceNavigationTiming[]
|
|
75
|
+
)[ 0 ].serverTiming.reduce(
|
|
76
|
+
( acc, entry ) => {
|
|
77
|
+
if ( f.length === 0 || f.includes( entry.name ) ) {
|
|
78
|
+
acc[ entry.name ] = entry.duration;
|
|
79
|
+
}
|
|
80
|
+
return acc;
|
|
81
|
+
},
|
|
82
|
+
{} as Record< string, number >
|
|
83
|
+
),
|
|
84
|
+
fields
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns time to first byte (TTFB) using the Navigation Timing API.
|
|
90
|
+
*
|
|
91
|
+
* @see https://web.dev/ttfb/#measure-ttfb-in-javascript
|
|
92
|
+
*
|
|
93
|
+
* @return TTFB value.
|
|
94
|
+
*/
|
|
95
|
+
async getTimeToFirstByte() {
|
|
96
|
+
return await this.page.evaluate< number >( () => {
|
|
97
|
+
const { responseStart, startTime } = (
|
|
98
|
+
performance.getEntriesByType(
|
|
99
|
+
'navigation'
|
|
100
|
+
) as PerformanceNavigationTiming[]
|
|
101
|
+
)[ 0 ];
|
|
102
|
+
return responseStart - startTime;
|
|
103
|
+
} );
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Returns the Largest Contentful Paint (LCP) value using the dedicated API.
|
|
108
|
+
*
|
|
109
|
+
* @see https://w3c.github.io/largest-contentful-paint/
|
|
110
|
+
* @see https://web.dev/lcp/#measure-lcp-in-javascript
|
|
111
|
+
*
|
|
112
|
+
* @return LCP value.
|
|
113
|
+
*/
|
|
114
|
+
async getLargestContentfulPaint() {
|
|
115
|
+
return await this.page.evaluate< number >(
|
|
116
|
+
() =>
|
|
117
|
+
new Promise( ( resolve ) => {
|
|
118
|
+
new PerformanceObserver( ( entryList ) => {
|
|
119
|
+
const entries = entryList.getEntries();
|
|
120
|
+
// The last entry is the largest contentful paint.
|
|
121
|
+
const largestPaintEntry = entries.at( -1 );
|
|
122
|
+
|
|
123
|
+
resolve( largestPaintEntry?.startTime || 0 );
|
|
124
|
+
} ).observe( {
|
|
125
|
+
type: 'largest-contentful-paint',
|
|
126
|
+
buffered: true,
|
|
127
|
+
} );
|
|
128
|
+
} )
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Returns the Cumulative Layout Shift (CLS) value using the dedicated API.
|
|
134
|
+
*
|
|
135
|
+
* @see https://github.com/WICG/layout-instability
|
|
136
|
+
* @see https://web.dev/cls/#measure-layout-shifts-in-javascript
|
|
137
|
+
*
|
|
138
|
+
* @return CLS value.
|
|
139
|
+
*/
|
|
140
|
+
async getCumulativeLayoutShift() {
|
|
141
|
+
return await this.page.evaluate< number >(
|
|
142
|
+
() =>
|
|
143
|
+
new Promise( ( resolve ) => {
|
|
144
|
+
let CLS = 0;
|
|
145
|
+
|
|
146
|
+
new PerformanceObserver( ( l ) => {
|
|
147
|
+
const entries = l.getEntries() as LayoutShift[];
|
|
148
|
+
|
|
149
|
+
entries.forEach( ( entry ) => {
|
|
150
|
+
if ( ! entry.hadRecentInput ) {
|
|
151
|
+
CLS += entry.value;
|
|
152
|
+
}
|
|
153
|
+
} );
|
|
154
|
+
|
|
155
|
+
resolve( CLS );
|
|
156
|
+
} ).observe( {
|
|
157
|
+
type: 'layout-shift',
|
|
158
|
+
buffered: true,
|
|
159
|
+
} );
|
|
160
|
+
} )
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Returns the loading durations using the Navigation Timing API. All the
|
|
166
|
+
* durations exclude the server response time.
|
|
167
|
+
*
|
|
168
|
+
* @return Object with loading metrics durations.
|
|
169
|
+
*/
|
|
170
|
+
async getLoadingDurations() {
|
|
171
|
+
return await this.page.evaluate( () => {
|
|
172
|
+
const [
|
|
173
|
+
{
|
|
174
|
+
requestStart,
|
|
175
|
+
responseStart,
|
|
176
|
+
responseEnd,
|
|
177
|
+
domContentLoadedEventEnd,
|
|
178
|
+
loadEventEnd,
|
|
179
|
+
},
|
|
180
|
+
] = performance.getEntriesByType(
|
|
181
|
+
'navigation'
|
|
182
|
+
) as PerformanceNavigationTiming[];
|
|
183
|
+
const paintTimings = performance.getEntriesByType(
|
|
184
|
+
'paint'
|
|
185
|
+
) as PerformancePaintTiming[];
|
|
186
|
+
|
|
187
|
+
const firstPaintStartTime = paintTimings.find(
|
|
188
|
+
( { name } ) => name === 'first-paint'
|
|
189
|
+
)!.startTime;
|
|
190
|
+
|
|
191
|
+
const firstContentfulPaintStartTime = paintTimings.find(
|
|
192
|
+
( { name } ) => name === 'first-contentful-paint'
|
|
193
|
+
)!.startTime;
|
|
194
|
+
|
|
195
|
+
return {
|
|
196
|
+
// Server side metric.
|
|
197
|
+
serverResponse: responseStart - requestStart,
|
|
198
|
+
// For client side metrics, consider the end of the response (the
|
|
199
|
+
// browser receives the HTML) as the start time (0).
|
|
200
|
+
firstPaint: firstPaintStartTime - responseEnd,
|
|
201
|
+
domContentLoaded: domContentLoadedEventEnd - responseEnd,
|
|
202
|
+
loaded: loadEventEnd - responseEnd,
|
|
203
|
+
firstContentfulPaint:
|
|
204
|
+
firstContentfulPaintStartTime - responseEnd,
|
|
205
|
+
timeSinceResponseEnd: performance.now() - responseEnd,
|
|
206
|
+
};
|
|
207
|
+
} );
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Starts Chromium tracing with predefined options for performance testing.
|
|
212
|
+
*
|
|
213
|
+
* The category set mirrors what Chrome DevTools enables when recording in
|
|
214
|
+
* the Performance panel: `devtools.timeline` provides the top-level event
|
|
215
|
+
* tree, and the `disabled-by-default-v8.cpu_profiler` + companion
|
|
216
|
+
* `devtools.timeline.stack` categories enable the V8 sampler that
|
|
217
|
+
* populates JavaScript call stacks. Without the latter, the saved trace
|
|
218
|
+
* shows only opaque "Function call" blocks with no JS frames inside.
|
|
219
|
+
*
|
|
220
|
+
* @param options Options to pass to `browser.startTracing()`.
|
|
221
|
+
*/
|
|
222
|
+
async startTracing( options = {} ) {
|
|
223
|
+
return await this.browser.startTracing( this.page, {
|
|
224
|
+
screenshots: false,
|
|
225
|
+
categories: [
|
|
226
|
+
'devtools.timeline',
|
|
227
|
+
'disabled-by-default-devtools.timeline',
|
|
228
|
+
'disabled-by-default-devtools.timeline.stack',
|
|
229
|
+
'disabled-by-default-v8.cpu_profiler',
|
|
230
|
+
'v8.execute',
|
|
231
|
+
],
|
|
232
|
+
...options,
|
|
233
|
+
} );
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Stops Chromium tracing.
|
|
238
|
+
*
|
|
239
|
+
* When `name` is a non-empty string and the `WP_ARTIFACTS_PATH` environment
|
|
240
|
+
* variable is set, the raw trace is written to
|
|
241
|
+
* `${WP_ARTIFACTS_PATH}/traces/<name>.trace.json`. The resulting file can
|
|
242
|
+
* be opened in Chrome DevTools (Performance panel → "Load profile…") to
|
|
243
|
+
* inspect the flame graph. Pass a falsy value (or omit the argument) to
|
|
244
|
+
* just parse the trace into `this.trace` without writing — this is the
|
|
245
|
+
* default for iteration loops, where you typically want to save only one
|
|
246
|
+
* representative sample. Callers pick which iteration that is, e.g.
|
|
247
|
+
* `i === Math.floor( iterations / 2 ) && 'post-editor-first-block'`.
|
|
248
|
+
*
|
|
249
|
+
* @param name File name (without extension) identifying the scenario, or
|
|
250
|
+
* `false`/`undefined` to skip writing.
|
|
251
|
+
*/
|
|
252
|
+
async stopTracing( name?: string | false ) {
|
|
253
|
+
const traceBuffer = await this.browser.stopTracing();
|
|
254
|
+
const traceJSON = JSON.parse( traceBuffer.toString() );
|
|
255
|
+
|
|
256
|
+
this.trace = traceJSON;
|
|
257
|
+
|
|
258
|
+
if ( ! name ) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const artifactsPath = process.env.WP_ARTIFACTS_PATH;
|
|
263
|
+
if ( ! artifactsPath ) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// The perf comparison flow runs the same suite against multiple
|
|
268
|
+
// branches into a single artifacts directory; the comparison branches
|
|
269
|
+
// set WP_PERF_NO_TRACE so the head branch's traces aren't overwritten.
|
|
270
|
+
if ( process.env.WP_PERF_NO_TRACE ) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Traces are saved minified. Run the following against a downloaded trace
|
|
275
|
+
// + matching `build/` directory to rewrite minified `functionName`s back
|
|
276
|
+
// to their source identifiers:
|
|
277
|
+
//
|
|
278
|
+
// node tools/build-scripts/packages/resolve-trace-source-maps.cjs
|
|
279
|
+
//
|
|
280
|
+
// Or via the workspace script:
|
|
281
|
+
//
|
|
282
|
+
// npm run --workspace @wordpress/build-scripts resolve-trace-source-maps -- <trace.json> [--build-dir <dir>]
|
|
283
|
+
const tracesDir = join( artifactsPath, 'traces' );
|
|
284
|
+
const filePath = join( tracesDir, `${ name }.trace.json` );
|
|
285
|
+
await mkdir( tracesDir, { recursive: true } );
|
|
286
|
+
await writeFile( filePath, JSON.stringify( traceJSON ) );
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* @return Durations of all traced `keydown`, `keypress`, and `keyup`
|
|
291
|
+
* events.
|
|
292
|
+
*/
|
|
293
|
+
getTypingEventDurations() {
|
|
294
|
+
return [
|
|
295
|
+
this.getEventDurations( 'keydown' ),
|
|
296
|
+
this.getEventDurations( 'keypress' ),
|
|
297
|
+
this.getEventDurations( 'keyup' ),
|
|
298
|
+
];
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* @return Durations of all traced `focus` and `focusin` events.
|
|
303
|
+
*/
|
|
304
|
+
getSelectionEventDurations() {
|
|
305
|
+
return [
|
|
306
|
+
this.getEventDurations( 'focus' ),
|
|
307
|
+
this.getEventDurations( 'focusin' ),
|
|
308
|
+
];
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* @return Durations of all traced `click` events.
|
|
313
|
+
*/
|
|
314
|
+
getClickEventDurations() {
|
|
315
|
+
return [ this.getEventDurations( 'click' ) ];
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* @return Durations of all traced `mouseover` and `mouseout` events.
|
|
320
|
+
*/
|
|
321
|
+
getHoverEventDurations() {
|
|
322
|
+
return [
|
|
323
|
+
this.getEventDurations( 'mouseover' ),
|
|
324
|
+
this.getEventDurations( 'mouseout' ),
|
|
325
|
+
];
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* @param eventType Type of event to filter.
|
|
330
|
+
* @return Durations of all events of a given type.
|
|
331
|
+
*/
|
|
332
|
+
getEventDurations( eventType: EventType ) {
|
|
333
|
+
if ( this.trace.traceEvents.length === 0 ) {
|
|
334
|
+
throw new Error(
|
|
335
|
+
'No trace events found. Did you forget to call stopTracing()?'
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return this.trace.traceEvents
|
|
340
|
+
.filter(
|
|
341
|
+
( item: TraceEvent ): boolean =>
|
|
342
|
+
item.cat === 'devtools.timeline' &&
|
|
343
|
+
item.name === 'EventDispatch' &&
|
|
344
|
+
item?.args?.data?.type === eventType &&
|
|
345
|
+
!! item.dur
|
|
346
|
+
)
|
|
347
|
+
.map( ( item ) => ( item.dur ? item.dur / 1000 : 0 ) );
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Initializes the web-vitals library upon next page navigation.
|
|
352
|
+
*
|
|
353
|
+
* Defaults to automatically triggering the navigation,
|
|
354
|
+
* but it can also be done manually.
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* ```js
|
|
358
|
+
* await metrics.initWebVitals();
|
|
359
|
+
* console.log( await metrics.getWebVitals() );
|
|
360
|
+
* ```
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```js
|
|
364
|
+
* await metrics.initWebVitals( false );
|
|
365
|
+
* await page.goto( '/some-other-page' );
|
|
366
|
+
* console.log( await metrics.getWebVitals() );
|
|
367
|
+
* ```
|
|
368
|
+
*
|
|
369
|
+
* @param reload Whether to force navigation by reloading the current page.
|
|
370
|
+
*/
|
|
371
|
+
async initWebVitals( reload = true ) {
|
|
372
|
+
await this.page.addInitScript( {
|
|
373
|
+
path: join(
|
|
374
|
+
__dirname,
|
|
375
|
+
'../../../../node_modules/web-vitals/dist/web-vitals.umd.cjs'
|
|
376
|
+
),
|
|
377
|
+
} );
|
|
378
|
+
|
|
379
|
+
await this.page.exposeFunction(
|
|
380
|
+
'__reportVitals__',
|
|
381
|
+
( data: string ) => {
|
|
382
|
+
const measurement: Metric = JSON.parse( data );
|
|
383
|
+
this.webVitals[ measurement.name ] = measurement.value;
|
|
384
|
+
}
|
|
385
|
+
);
|
|
386
|
+
|
|
387
|
+
await this.page.addInitScript( () => {
|
|
388
|
+
const reportVitals = ( measurement: unknown ) =>
|
|
389
|
+
window.__reportVitals__( JSON.stringify( measurement ) );
|
|
390
|
+
|
|
391
|
+
window.addEventListener( 'DOMContentLoaded', () => {
|
|
392
|
+
// @ts-expect-error This is valid but web-vitals does not register the global types.
|
|
393
|
+
window.webVitals.onCLS( reportVitals );
|
|
394
|
+
// @ts-expect-error This is valid but web-vitals does not register the global types.
|
|
395
|
+
window.webVitals.onFCP( reportVitals );
|
|
396
|
+
// @ts-expect-error This is valid but web-vitals does not register the global types.
|
|
397
|
+
window.webVitals.onFID( reportVitals );
|
|
398
|
+
// @ts-expect-error This is valid but web-vitals does not register the global types.
|
|
399
|
+
window.webVitals.onINP( reportVitals );
|
|
400
|
+
// @ts-expect-error This is valid but web-vitals does not register the global types.
|
|
401
|
+
window.webVitals.onLCP( reportVitals );
|
|
402
|
+
// @ts-expect-error This is valid but web-vitals does not register the global types.
|
|
403
|
+
window.webVitals.onTTFB( reportVitals );
|
|
404
|
+
} );
|
|
405
|
+
} );
|
|
406
|
+
|
|
407
|
+
if ( reload ) {
|
|
408
|
+
// By reloading the page the script will be applied.
|
|
409
|
+
await this.page.reload();
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Returns web vitals as collected by the web-vitals library.
|
|
415
|
+
*
|
|
416
|
+
* If the web-vitals library hasn't been loaded on the current page yet,
|
|
417
|
+
* it will be initialized with a page reload.
|
|
418
|
+
*
|
|
419
|
+
* Reloads the page to force web-vitals to report all collected metrics.
|
|
420
|
+
*
|
|
421
|
+
* @return {WebVitalsMeasurements} Web vitals measurements.
|
|
422
|
+
*/
|
|
423
|
+
async getWebVitals() {
|
|
424
|
+
// Reset values.
|
|
425
|
+
this.webVitals = {};
|
|
426
|
+
|
|
427
|
+
const hasScript = await this.page.evaluate(
|
|
428
|
+
// @ts-expect-error This is valid but web-vitals does not register the global types.
|
|
429
|
+
() => typeof window.webVitals !== 'undefined'
|
|
430
|
+
);
|
|
431
|
+
|
|
432
|
+
if ( ! hasScript ) {
|
|
433
|
+
await this.initWebVitals();
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// Trigger navigation so the web-vitals library reports values on unload.
|
|
437
|
+
await this.page.reload();
|
|
438
|
+
|
|
439
|
+
return this.webVitals;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { readFile } from 'fs/promises';
|
|
5
|
+
import { basename } from 'path';
|
|
6
|
+
import { getType } from 'mime';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Internal dependencies
|
|
10
|
+
*/
|
|
11
|
+
import type { PageUtils } from './index';
|
|
12
|
+
import type { Locator } from '@playwright/test';
|
|
13
|
+
|
|
14
|
+
type FileObject = {
|
|
15
|
+
name: string;
|
|
16
|
+
mimeType?: string;
|
|
17
|
+
buffer: Buffer;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type Options = {
|
|
21
|
+
position?: { x: number; y: number };
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Simulate dragging files from outside the current page.
|
|
26
|
+
*
|
|
27
|
+
* @param this
|
|
28
|
+
* @param files The files to be dragged.
|
|
29
|
+
* @return The methods of the drag operation.
|
|
30
|
+
*/
|
|
31
|
+
async function dragFiles(
|
|
32
|
+
this: PageUtils,
|
|
33
|
+
files: string | string[] | FileObject | FileObject[]
|
|
34
|
+
) {
|
|
35
|
+
const filesList = Array.isArray( files ) ? files : [ files ];
|
|
36
|
+
const fileObjects = await Promise.all(
|
|
37
|
+
filesList.map( async ( filePathOrObject ) => {
|
|
38
|
+
if ( typeof filePathOrObject !== 'string' ) {
|
|
39
|
+
return {
|
|
40
|
+
name: filePathOrObject.name,
|
|
41
|
+
mimeType:
|
|
42
|
+
filePathOrObject.mimeType ||
|
|
43
|
+
getType( filePathOrObject.name ),
|
|
44
|
+
base64: filePathOrObject.buffer.toString( 'base64' ),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const base64 = await readFile( filePathOrObject, 'base64' );
|
|
48
|
+
const name = basename( filePathOrObject );
|
|
49
|
+
return {
|
|
50
|
+
name,
|
|
51
|
+
mimeType: getType( filePathOrObject ),
|
|
52
|
+
base64,
|
|
53
|
+
};
|
|
54
|
+
} )
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
// CDP doesn't actually support dragging files, this is only a _good enough_
|
|
58
|
+
// dummy data so that it will correctly send the relevant events.
|
|
59
|
+
const dragData = {
|
|
60
|
+
items: fileObjects.map( ( fileObject ) => ( {
|
|
61
|
+
mimeType: fileObject.mimeType ?? 'File',
|
|
62
|
+
data: fileObject.base64,
|
|
63
|
+
} ) ),
|
|
64
|
+
files: fileObjects.map( ( fileObject ) => fileObject.name ),
|
|
65
|
+
// Copy = 1, Link = 2, Move = 16.
|
|
66
|
+
dragOperationsMask: 1,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const cdpSession = await this.context.newCDPSession( this.page );
|
|
70
|
+
|
|
71
|
+
const position = {
|
|
72
|
+
x: 0,
|
|
73
|
+
y: 0,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
/**
|
|
78
|
+
* Drag the files over an element (fires `dragenter` and `dragover` events).
|
|
79
|
+
*
|
|
80
|
+
* @param selectorOrLocator A selector or a locator to search for an element.
|
|
81
|
+
* @param options The optional options.
|
|
82
|
+
* @param options.position A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.
|
|
83
|
+
*/
|
|
84
|
+
dragOver: async (
|
|
85
|
+
selectorOrLocator: string | Locator,
|
|
86
|
+
options: Options = {}
|
|
87
|
+
) => {
|
|
88
|
+
const locator =
|
|
89
|
+
typeof selectorOrLocator === 'string'
|
|
90
|
+
? this.page.locator( selectorOrLocator )
|
|
91
|
+
: selectorOrLocator;
|
|
92
|
+
const boundingBox = await locator.boundingBox();
|
|
93
|
+
|
|
94
|
+
if ( ! boundingBox ) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
'Cannot find the element or the element is not visible on the viewport.'
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
position.x =
|
|
101
|
+
boundingBox.x +
|
|
102
|
+
( options.position?.x ?? boundingBox.width / 2 );
|
|
103
|
+
position.y =
|
|
104
|
+
boundingBox.y +
|
|
105
|
+
( options.position?.y ?? boundingBox.height / 2 );
|
|
106
|
+
|
|
107
|
+
await cdpSession.send( 'Input.dispatchDragEvent', {
|
|
108
|
+
type: 'dragEnter',
|
|
109
|
+
...position,
|
|
110
|
+
data: dragData,
|
|
111
|
+
} );
|
|
112
|
+
await cdpSession.send( 'Input.dispatchDragEvent', {
|
|
113
|
+
type: 'dragOver',
|
|
114
|
+
...position,
|
|
115
|
+
data: dragData,
|
|
116
|
+
} );
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Drop the files at the current position.
|
|
121
|
+
*/
|
|
122
|
+
drop: async () => {
|
|
123
|
+
const topMostElement = await this.page.evaluateHandle(
|
|
124
|
+
( { x, y } ) => {
|
|
125
|
+
const element = document.elementFromPoint( x, y );
|
|
126
|
+
if ( element instanceof HTMLIFrameElement ) {
|
|
127
|
+
const offsetBox = element.getBoundingClientRect();
|
|
128
|
+
return element.contentDocument!.elementFromPoint(
|
|
129
|
+
x - offsetBox.x,
|
|
130
|
+
y - offsetBox.y
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
return element;
|
|
134
|
+
},
|
|
135
|
+
position
|
|
136
|
+
);
|
|
137
|
+
const elementHandle = topMostElement.asElement();
|
|
138
|
+
|
|
139
|
+
if ( ! elementHandle ) {
|
|
140
|
+
throw new Error( 'Element not found.' );
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const dataTransfer = await elementHandle.evaluateHandle(
|
|
144
|
+
async ( _node, _fileObjects ) => {
|
|
145
|
+
const dt = new DataTransfer();
|
|
146
|
+
const fileInstances = await Promise.all(
|
|
147
|
+
_fileObjects.map( async ( fileObject: any ) => {
|
|
148
|
+
const blob = await fetch(
|
|
149
|
+
`data:${ fileObject.mimeType };base64,${ fileObject.base64 }`
|
|
150
|
+
).then( ( res ) => res.blob() );
|
|
151
|
+
return new File( [ blob ], fileObject.name, {
|
|
152
|
+
type: fileObject.mimeType ?? undefined,
|
|
153
|
+
} );
|
|
154
|
+
} )
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
fileInstances.forEach( ( file ) => {
|
|
158
|
+
dt.items.add( file );
|
|
159
|
+
} );
|
|
160
|
+
|
|
161
|
+
return dt;
|
|
162
|
+
},
|
|
163
|
+
fileObjects
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
await elementHandle.dispatchEvent( 'drop', { dataTransfer } );
|
|
167
|
+
|
|
168
|
+
await cdpSession.detach();
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export { dragFiles };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { PageUtils } from './';
|
|
5
|
+
|
|
6
|
+
interface NetworkConditions {
|
|
7
|
+
/**
|
|
8
|
+
* True to emulate internet disconnection.
|
|
9
|
+
*/
|
|
10
|
+
offline: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Minimum latency from request sent to response headers received (ms).
|
|
13
|
+
*/
|
|
14
|
+
latency: number;
|
|
15
|
+
/**
|
|
16
|
+
* Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
|
|
17
|
+
*/
|
|
18
|
+
downloadThroughput: number;
|
|
19
|
+
/**
|
|
20
|
+
* Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
|
|
21
|
+
*/
|
|
22
|
+
uploadThroughput: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Defaults from https://github.com/puppeteer/puppeteer/blob/401355610874beac23a51dcb75739a4bb4191a2b/packages/puppeteer-core/src/cdp/PredefinedNetworkConditions.ts.
|
|
26
|
+
const PredefinedNetworkConditions: Record< string, NetworkConditions > = {
|
|
27
|
+
// Generally aligned with DevTools
|
|
28
|
+
// https://source.chromium.org/chromium/chromium/src/+/main:third_party/devtools-frontend/src/front_end/core/sdk/NetworkManager.ts;l=398;drc=225e1240f522ca684473f541ae6dae6cd766dd33.
|
|
29
|
+
'Slow 3G': {
|
|
30
|
+
offline: false,
|
|
31
|
+
// ~500Kbps down
|
|
32
|
+
downloadThroughput: ( ( 500 * 1000 ) / 8 ) * 0.8,
|
|
33
|
+
// ~500Kbps up
|
|
34
|
+
uploadThroughput: ( ( 500 * 1000 ) / 8 ) * 0.8,
|
|
35
|
+
// 400ms RTT
|
|
36
|
+
latency: 400 * 5,
|
|
37
|
+
},
|
|
38
|
+
'Fast 3G': {
|
|
39
|
+
offline: false,
|
|
40
|
+
// ~1.6 Mbps down
|
|
41
|
+
downloadThroughput: ( ( 1.6 * 1000 * 1000 ) / 8 ) * 0.9,
|
|
42
|
+
// ~0.75 Mbps up
|
|
43
|
+
uploadThroughput: ( ( 750 * 1000 ) / 8 ) * 0.9,
|
|
44
|
+
// 150ms RTT
|
|
45
|
+
latency: 150 * 3.75,
|
|
46
|
+
},
|
|
47
|
+
// alias to Fast 3G to align with Lighthouse (crbug.com/342406608)
|
|
48
|
+
// and DevTools (crbug.com/342406608),
|
|
49
|
+
'Slow 4G': {
|
|
50
|
+
offline: false,
|
|
51
|
+
// ~1.6 Mbps down
|
|
52
|
+
downloadThroughput: ( ( 1.6 * 1000 * 1000 ) / 8 ) * 0.9,
|
|
53
|
+
// ~0.75 Mbps up
|
|
54
|
+
uploadThroughput: ( ( 750 * 1000 ) / 8 ) * 0.9,
|
|
55
|
+
// 150ms RTT
|
|
56
|
+
latency: 150 * 3.75,
|
|
57
|
+
},
|
|
58
|
+
'Fast 4G': {
|
|
59
|
+
offline: false,
|
|
60
|
+
// 9 Mbps down
|
|
61
|
+
downloadThroughput: ( ( 9 * 1000 * 1000 ) / 8 ) * 0.9,
|
|
62
|
+
// 1.5 Mbps up
|
|
63
|
+
uploadThroughput: ( ( 1.5 * 1000 * 1000 ) / 8 ) * 0.9,
|
|
64
|
+
// 60ms RTT
|
|
65
|
+
latency: 60 * 2.75,
|
|
66
|
+
},
|
|
67
|
+
/**
|
|
68
|
+
* Network conditions used for desktop in Lighthouse/PSI.
|
|
69
|
+
*
|
|
70
|
+
* 10,240 kb/s throughput with 40 ms TCP RTT.
|
|
71
|
+
*
|
|
72
|
+
* @see https://github.com/paulirish/lighthouse/blob/f0855904aaffaecf3089169449646960782d7e92/core/config/constants.js#L40-L49
|
|
73
|
+
* @see https://docs.google.com/document/d/1-p4HSp42REEA5-jCBVB6PqQcVhI1nQIblBCNKhPJUXg/edit?tab=t.0#heading=h.jsap7yf4phk6
|
|
74
|
+
*/
|
|
75
|
+
Broadband: {
|
|
76
|
+
offline: false,
|
|
77
|
+
downloadThroughput: ( 10240 * 1000 ) / 8,
|
|
78
|
+
uploadThroughput: ( 10240 * 1000 ) / 8,
|
|
79
|
+
latency: 40,
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export async function emulateNetworkConditions(
|
|
84
|
+
this: PageUtils,
|
|
85
|
+
condition: keyof typeof PredefinedNetworkConditions | NetworkConditions
|
|
86
|
+
) {
|
|
87
|
+
if ( 'chromium' !== this.browserName ) {
|
|
88
|
+
throw new Error(
|
|
89
|
+
'CDP sessions are only supported on Chromium-based browsers'
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const session = await this.page.context().newCDPSession( this.page );
|
|
94
|
+
await session.send(
|
|
95
|
+
'Network.emulateNetworkConditions',
|
|
96
|
+
'string' === typeof condition
|
|
97
|
+
? PredefinedNetworkConditions[ condition ]
|
|
98
|
+
: condition
|
|
99
|
+
);
|
|
100
|
+
await session.detach();
|
|
101
|
+
}
|