/
site-editor.spec.js
316 lines (266 loc) · 8.71 KB
/
site-editor.spec.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
/* eslint-disable playwright/no-conditional-in-test, playwright/expect-expect */
/**
* WordPress dependencies
*/
import { test, Metrics } from '@wordpress/e2e-test-utils-playwright';
/**
* Internal dependencies
*/
import { PerfUtils } from '../fixtures';
// See https://github.com/WordPress/gutenberg/issues/51383#issuecomment-1613460429
const BROWSER_IDLE_WAIT = 1000;
const results = {
serverResponse: [],
firstPaint: [],
domContentLoaded: [],
loaded: [],
firstContentfulPaint: [],
firstBlock: [],
type: [],
typeContainer: [],
focus: [],
inserterOpen: [],
inserterHover: [],
inserterSearch: [],
listViewOpen: [],
navigate: [],
loadPatterns: [],
};
test.describe( 'Site Editor Performance', () => {
test.use( {
perfUtils: async ( { page }, use ) => {
await use( new PerfUtils( { page } ) );
},
metrics: async ( { page }, use ) => {
await use( new Metrics( { page } ) );
},
} );
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'emptytheme' );
await requestUtils.deleteAllTemplates( 'wp_template' );
await requestUtils.deleteAllTemplates( 'wp_template_part' );
} );
test.afterAll( async ( { requestUtils }, testInfo ) => {
await testInfo.attach( 'results', {
body: JSON.stringify( results, null, 2 ),
contentType: 'application/json',
} );
await requestUtils.deleteAllTemplates( 'wp_template' );
await requestUtils.deleteAllTemplates( 'wp_template_part' );
await requestUtils.activateTheme( 'twentytwentyone' );
} );
test.describe( 'Loading', () => {
let draftId = null;
test( 'Setup the test page', async ( { admin, perfUtils } ) => {
await admin.createNewPost( { postType: 'page' } );
await perfUtils.loadBlocksForLargePost();
draftId = await perfUtils.saveDraft();
} );
const samples = 10;
const throwaway = 1;
const iterations = samples + throwaway;
for ( let i = 1; i <= iterations; i++ ) {
test( `Run the test (${ i } of ${ iterations })`, async ( {
admin,
perfUtils,
metrics,
} ) => {
// Go to the test draft.
await admin.visitSiteEditor( {
postId: draftId,
postType: 'page',
} );
// Wait for the first block.
const canvas = await perfUtils.getCanvas();
await canvas.locator( '.wp-block' ).first().waitFor();
// Get the durations.
const loadingDurations = await metrics.getLoadingDurations();
// Save the results.
if ( i > throwaway ) {
Object.entries( loadingDurations ).forEach(
( [ metric, duration ] ) => {
if ( metric === 'timeSinceResponseEnd' ) {
results.firstBlock.push( duration );
} else {
results[ metric ].push( duration );
}
}
);
}
} );
}
} );
test.describe( 'Typing', () => {
let draftId = null;
test( 'Setup the test post', async ( { admin, editor, perfUtils } ) => {
await admin.createNewPost( { postType: 'page' } );
await perfUtils.loadBlocksForLargePost();
await editor.insertBlock( { name: 'core/paragraph' } );
draftId = await perfUtils.saveDraft();
} );
test( 'Run the test', async ( { admin, perfUtils, metrics } ) => {
// Go to the test draft.
await admin.visitSiteEditor( {
postId: draftId,
postType: 'page',
} );
// Enter edit mode (second click is needed for the legacy edit mode).
const canvas = await perfUtils.getCanvas();
await canvas.locator( 'body' ).click();
await canvas
.getByRole( 'document', { name: /Block:( Post)? Content/ } )
.click();
const paragraph = canvas.getByRole( 'document', {
name: /Empty block/i,
} );
// The first character typed triggers a longer time (isTyping change).
// It can impact the stability of the metric, so we exclude it. It
// probably deserves a dedicated metric itself, though.
const samples = 10;
const throwaway = 1;
const iterations = samples + throwaway;
// Start tracing.
await metrics.startTracing();
// Type the testing sequence into the empty paragraph.
await paragraph.type( 'x'.repeat( iterations ), {
delay: BROWSER_IDLE_WAIT,
// The extended timeout is needed because the typing is very slow
// and the `delay` value itself does not extend it.
timeout: iterations * BROWSER_IDLE_WAIT * 2, // 2x the total time to be safe.
} );
// Stop tracing.
await metrics.stopTracing();
// Get the durations.
const [ keyDownEvents, keyPressEvents, keyUpEvents ] =
metrics.getTypingEventDurations();
// Save the results.
for ( let i = throwaway; i < iterations; i++ ) {
results.type.push(
keyDownEvents[ i ] + keyPressEvents[ i ] + keyUpEvents[ i ]
);
}
} );
} );
test.describe( 'Navigating', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentythree' );
} );
test.afterAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyone' );
} );
const iterations = 5;
for ( let i = 1; i <= iterations; i++ ) {
test( `Run the test (${ i } of ${ iterations })`, async ( {
admin,
page,
metrics,
} ) => {
await admin.visitSiteEditor( {
path: '/wp_template',
} );
// Start tracing.
await metrics.startTracing();
await page
.getByRole( 'button', { name: 'Single Posts' } )
.click();
// Stop tracing.
await metrics.stopTracing();
// Get the durations.
const [ mouseClickEvents ] = metrics.getClickEventDurations();
// Save the results.
results.navigate.push( mouseClickEvents[ 0 ] );
} );
}
} );
test.describe( 'Loading Patterns', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyfour' );
} );
test.afterAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyfour' );
} );
test( 'Run the test', async ( { page, admin, perfUtils, editor } ) => {
const samples = 10;
for ( let i = 1; i <= samples; i++ ) {
// We want to start from a fresh state each time, without
// queries or patterns already cached.
await admin.visitSiteEditor( {
postId: 'twentytwentyfour//home',
postType: 'wp_template',
canvas: 'edit',
} );
await editor.openDocumentSettingsSidebar();
/*
* https://github.com/WordPress/gutenberg/pull/55091 updated the HTML by
* removing the replace template button in sidebar-edit-mode/template-panel/replace-template-button.js
* with a "transform into" list. https://github.com/WordPress/gutenberg/pull/59259 made these tests
* compatible with the new UI, however, the performance tests compare previous versions of the UI.
*
* The following code is a workaround to test the performance of the new UI.
* `actionsButtonElement` is used to check if the old UI is present.
* If there is a Replace template button (old UI), click it, otherwise, click the "transform into" button.
* Once the performance tests are updated to compare compatible versions this code can be removed.
*/
// eslint-disable-next-line no-restricted-syntax
const isActionsButtonVisible = await page
.locator(
'.edit-site-template-card__actions button[aria-label="Actions"]'
)
.isVisible();
if ( isActionsButtonVisible ) {
await page
.getByRole( 'button', {
name: 'Actions',
} )
.click();
}
// Wait for the browser to be idle before starting the monitoring.
// eslint-disable-next-line no-restricted-syntax
await page.waitForTimeout( BROWSER_IDLE_WAIT );
const startTime = performance.now();
if ( isActionsButtonVisible ) {
await page
.getByRole( 'menuitem', { name: 'Replace template' } )
.click();
} else {
await page
.getByRole( 'button', { name: 'Transform into:' } )
.click();
}
const patterns = [
'Blogging home template',
'Business home template',
'Portfolio home template with post featured images',
'Blogging index template',
];
await Promise.all(
patterns.map( async ( pattern ) => {
const canvas = await perfUtils.getCanvas(
page
.getByRole( 'option', {
name: pattern,
exact: true,
} )
.getByTitle( 'Editor canvas' )
);
// Wait until the first block is rendered AND all
// patterns are replaced.
await Promise.all( [
canvas.locator( '.wp-block' ).first().waitFor(),
page.waitForFunction(
() =>
document.querySelectorAll(
'[data-type="core/pattern"]'
).length === 0
),
] );
} )
);
const endTime = performance.now();
results.loadPatterns.push( endTime - startTime );
await page.keyboard.press( 'Escape' );
}
} );
} );
} );
/* eslint-enable playwright/no-conditional-in-test, playwright/expect-expect */