Skip to content

Commit 6fe6d2b

Browse files
authored
E2E Utils: Use frameLocator for retrieving editor canvas (#54911)
1 parent c0cacda commit 6fe6d2b

File tree

85 files changed

+1037
-710
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1037
-710
lines changed

packages/e2e-test-utils-playwright/src/admin/create-new-post.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,18 @@ export async function createNewPost( {
3030

3131
await this.visitAdminPage( 'post-new.php', query );
3232

33-
// Wait for both iframed and non-iframed canvas and resolve once the
34-
// currently available one is ready. To make this work, we need an inner
35-
// legacy canvas selector that is unavailable directly when the canvas is
36-
// iframed.
37-
await Promise.any( [
38-
this.page.locator( '.wp-block-post-content' ).waitFor(),
39-
this.page
40-
.frameLocator( '[name=editor-canvas]' )
41-
.locator( 'body > *' )
42-
.first()
43-
.waitFor(),
44-
] );
45-
46-
await this.page.evaluate( ( welcomeGuide ) => {
33+
await this.page.waitForFunction( ( welcomeGuide ) => {
34+
if ( ! window?.wp?.data?.dispatch ) {
35+
return false;
36+
}
4737
window.wp.data
4838
.dispatch( 'core/preferences' )
4939
.set( 'core/edit-post', 'welcomeGuide', welcomeGuide );
5040

5141
window.wp.data
5242
.dispatch( 'core/preferences' )
5343
.set( 'core/edit-post', 'fullscreenMode', false );
44+
45+
return true;
5446
}, showWelcomeGuide );
5547
}

packages/e2e-test-utils-playwright/src/editor/get-blocks.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ type Block = {
1919
* @return The blocks.
2020
*/
2121
export async function getBlocks( this: Editor, { full = false } = {} ) {
22+
await this.page.waitForFunction(
23+
() => window?.wp?.blocks && window?.wp?.data
24+
);
25+
2226
return await this.page.evaluate(
2327
( [ _full ] ) => {
2428
// Remove other unpredictable properties like clientId from blocks for testing purposes.

packages/e2e-test-utils-playwright/src/editor/get-edited-post-content.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import type { Editor } from './index';
1111
* @return Promise resolving with post content markup.
1212
*/
1313
export async function getEditedPostContent( this: Editor ) {
14+
await this.page.waitForFunction( () => window?.wp?.data );
15+
1416
return await this.page.evaluate( () =>
1517
window.wp.data.select( 'core/editor' ).getEditedPostContent()
1618
);

packages/e2e-test-utils-playwright/src/editor/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/**
22
* External dependencies
33
*/
4-
import type { Browser, Page, BrowserContext, Frame } from '@playwright/test';
4+
import type {
5+
Browser,
6+
Page,
7+
BrowserContext,
8+
FrameLocator,
9+
} from '@playwright/test';
510

611
/**
712
* Internal dependencies
@@ -19,6 +24,7 @@ import { setContent } from './set-content';
1924
import { showBlockToolbar } from './show-block-toolbar';
2025
import { saveSiteEditorEntities } from './site-editor';
2126
import { setIsFixedToolbar } from './set-is-fixed-toolbar';
27+
import { switchToLegacyCanvas } from './switch-to-legacy-canvas';
2228
import { transformBlockTo } from './transform-block-to';
2329

2430
type EditorConstructorProps = {
@@ -36,8 +42,8 @@ export class Editor {
3642
this.browser = this.context.browser()!;
3743
}
3844

39-
get canvas(): Frame | Page {
40-
return this.page.frame( 'editor-canvas' ) || this.page;
45+
get canvas(): FrameLocator {
46+
return this.page.frameLocator( '[name="editor-canvas"]' );
4147
}
4248

4349
/** @borrows clickBlockOptionsMenuItem as this.clickBlockOptionsMenuItem */
@@ -72,6 +78,9 @@ export class Editor {
7278
/** @borrows setIsFixedToolbar as this.setIsFixedToolbar */
7379
setIsFixedToolbar: typeof setIsFixedToolbar =
7480
setIsFixedToolbar.bind( this );
81+
/** @borrows switchToLegacyCanvas as this.switchToLegacyCanvas */
82+
switchToLegacyCanvas: typeof switchToLegacyCanvas =
83+
switchToLegacyCanvas.bind( this );
7584
/** @borrows transformBlockTo as this.transformBlockTo */
7685
transformBlockTo: typeof transformBlockTo = transformBlockTo.bind( this );
7786
}

packages/e2e-test-utils-playwright/src/editor/insert-block.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ async function insertBlock(
1919
this: Editor,
2020
blockRepresentation: BlockRepresentation
2121
) {
22+
await this.page.waitForFunction(
23+
() => window?.wp?.blocks && window?.wp?.data
24+
);
25+
2226
await this.page.evaluate( ( _blockRepresentation ) => {
2327
function recursiveCreateBlock( {
2428
name,

packages/e2e-test-utils-playwright/src/editor/set-content.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import type { Editor } from './index';
1010
* @param html Serialized block HTML.
1111
*/
1212
async function setContent( this: Editor, html: string ) {
13+
await this.page.waitForFunction(
14+
() => window?.wp?.blocks && window?.wp?.data
15+
);
16+
1317
await this.page.evaluate( ( _html ) => {
1418
const blocks = window.wp.blocks.parse( _html );
1519

packages/e2e-test-utils-playwright/src/editor/set-is-fixed-toolbar.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import type { Editor } from './index';
1010
* @param isFixed Boolean value true/false for on/off.
1111
*/
1212
export async function setIsFixedToolbar( this: Editor, isFixed: boolean ) {
13+
await this.page.waitForFunction( () => window?.wp?.data );
14+
1315
await this.page.evaluate( ( _isFixed ) => {
1416
window.wp.data
1517
.dispatch( 'core/preferences' )
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import type { Editor } from './index';
5+
6+
/**
7+
* Switches to legacy (non-iframed) canvas.
8+
*
9+
* @param this
10+
*/
11+
export async function switchToLegacyCanvas( this: Editor ) {
12+
await this.page.waitForFunction( () => window?.wp?.blocks );
13+
14+
await this.page.evaluate( () => {
15+
window.wp.blocks.registerBlockType( 'test/v2', {
16+
apiVersion: '2',
17+
title: 'test',
18+
} );
19+
} );
20+
}

packages/e2e-test-utils-playwright/src/editor/transform-block-to.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import type { Editor } from './index';
1010
* @param name Block name.
1111
*/
1212
export async function transformBlockTo( this: Editor, name: string ) {
13+
await this.page.waitForFunction(
14+
() => window?.wp?.blocks && window?.wp?.data
15+
);
16+
1317
await this.page.evaluate(
1418
( [ blockName ] ) => {
1519
const clientIds = window.wp.data

packages/e2e-test-utils-playwright/src/page-utils/drag-files.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,6 @@ async function dragFiles(
5454
} )
5555
);
5656

57-
const dataTransfer = await this.page.evaluateHandle(
58-
async ( _fileObjects ) => {
59-
const dt = new DataTransfer();
60-
const fileInstances = await Promise.all(
61-
_fileObjects.map( async ( fileObject ) => {
62-
const blob = await fetch(
63-
`data:${ fileObject.mimeType };base64,${ fileObject.base64 }`
64-
).then( ( res ) => res.blob() );
65-
return new File( [ blob ], fileObject.name, {
66-
type: fileObject.mimeType ?? undefined,
67-
} );
68-
} )
69-
);
70-
71-
fileInstances.forEach( ( file ) => {
72-
dt.items.add( file );
73-
} );
74-
75-
return dt;
76-
},
77-
fileObjects
78-
);
79-
8057
// CDP doesn't actually support dragging files, this is only a _good enough_
8158
// dummy data so that it will correctly send the relevant events.
8259
const dragData = {
@@ -159,6 +136,29 @@ async function dragFiles(
159136
throw new Error( 'Element not found.' );
160137
}
161138

139+
const dataTransfer = await locator.evaluateHandle(
140+
async ( _node, _fileObjects ) => {
141+
const dt = new DataTransfer();
142+
const fileInstances = await Promise.all(
143+
_fileObjects.map( async ( fileObject: any ) => {
144+
const blob = await fetch(
145+
`data:${ fileObject.mimeType };base64,${ fileObject.base64 }`
146+
).then( ( res ) => res.blob() );
147+
return new File( [ blob ], fileObject.name, {
148+
type: fileObject.mimeType ?? undefined,
149+
} );
150+
} )
151+
);
152+
153+
fileInstances.forEach( ( file ) => {
154+
dt.items.add( file );
155+
} );
156+
157+
return dt;
158+
},
159+
fileObjects
160+
);
161+
162162
await locator.dispatchEvent( 'drop', { dataTransfer } );
163163

164164
await cdpSession.detach();

test/e2e/specs/editor/blocks/buttons.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ test.describe( 'Buttons', () => {
3030
editor,
3131
page,
3232
} ) => {
33-
await editor.canvas.click( 'role=button[name="Add default block"i]' );
33+
await editor.canvas
34+
.locator( 'role=button[name="Add default block"i]' )
35+
.click();
3436
await page.keyboard.type( '/buttons' );
3537
await page.keyboard.press( 'Enter' );
3638
await page.keyboard.type( 'Content' );

test/e2e/specs/editor/blocks/classic.spec.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@ test.use( {
1818
} );
1919

2020
test.describe( 'Classic', () => {
21-
test.beforeEach( async ( { admin, page } ) => {
21+
test.beforeEach( async ( { admin, editor } ) => {
2222
await admin.createNewPost();
2323
// To do: run with iframe.
24-
await page.evaluate( () => {
25-
window.wp.blocks.registerBlockType( 'test/v2', {
26-
apiVersion: '2',
27-
title: 'test',
28-
} );
29-
} );
24+
await editor.switchToLegacyCanvas();
3025
} );
3126

3227
test.afterAll( async ( { requestUtils } ) => {
@@ -134,12 +129,7 @@ test.describe( 'Classic', () => {
134129
await page.unroute( '**' );
135130

136131
// To do: run with iframe.
137-
await page.evaluate( () => {
138-
window.wp.blocks.registerBlockType( 'test/v2', {
139-
apiVersion: '2',
140-
title: 'test',
141-
} );
142-
} );
132+
await editor.switchToLegacyCanvas();
143133

144134
const errors = [];
145135
page.on( 'pageerror', ( exception ) => {

test/e2e/specs/editor/blocks/code.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ test.describe( 'Code', () => {
1212
editor,
1313
page,
1414
} ) => {
15-
await editor.canvas.click( 'role=button[name="Add default block"i]' );
15+
await editor.canvas
16+
.locator( 'role=button[name="Add default block"i]' )
17+
.click();
1618
await page.keyboard.type( '```' );
1719
await page.keyboard.press( 'Enter' );
1820
await page.keyboard.type( '<?php' );

test/e2e/specs/editor/blocks/comments.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ test.describe( 'Comments', () => {
165165
await expect( warning ).toBeVisible();
166166
await expect( placeholder ).toBeVisible();
167167

168-
await editor.canvas.click(
169-
'role=button[name="Switch to editable mode"i]'
170-
);
168+
await editor.canvas
169+
.locator( 'role=button[name="Switch to editable mode"i]' )
170+
.click();
171171

172172
const commentTemplate = editor.canvas.locator(
173173
'role=document[name="Block: Comment Template"i]'

test/e2e/specs/editor/blocks/gallery.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ test.describe( 'Gallery', () => {
5151
plainText: `[gallery ids="${ uploadedMedia.id }"]`,
5252
} );
5353

54-
await editor.canvas.click( 'role=button[name="Add default block"i]' );
54+
await editor.canvas
55+
.locator( 'role=button[name="Add default block"i]' )
56+
.click();
5557
await pageUtils.pressKeys( 'primary+v' );
5658

5759
const img = editor.canvas.locator(
@@ -204,7 +206,9 @@ test.describe( 'Gallery', () => {
204206
} ) => {
205207
await admin.createNewPost();
206208
await editor.insertBlock( { name: 'core/gallery' } );
207-
await editor.canvas.click( 'role=button[name="Media Library"i]' );
209+
await editor.canvas
210+
.locator( 'role=button[name="Media Library"i]' )
211+
.click();
208212

209213
const mediaLibrary = page.locator(
210214
'role=dialog[name="Create gallery"i]'

test/e2e/specs/editor/blocks/group.spec.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ test.describe( 'Group', () => {
2929
);
3030

3131
// Select the default, selected Group layout from the variation picker.
32-
await editor.canvas.click(
33-
'role=button[name="Group: Gather blocks in a container."i]'
34-
);
32+
await editor.canvas
33+
.locator(
34+
'role=button[name="Group: Gather blocks in a container."i]'
35+
)
36+
.click();
3537

3638
expect( await editor.getEditedPostContent() ).toMatchSnapshot();
3739
} );
@@ -40,17 +42,21 @@ test.describe( 'Group', () => {
4042
editor,
4143
page,
4244
} ) => {
43-
await editor.canvas.click( 'role=button[name="Add default block"i]' );
45+
await editor.canvas
46+
.locator( 'role=button[name="Add default block"i]' )
47+
.click();
4448
await page.keyboard.type( '/group' );
4549
await expect(
4650
page.locator( 'role=option[name="Group"i][selected]' )
4751
).toBeVisible();
4852
await page.keyboard.press( 'Enter' );
4953

5054
// Select the default, selected Group layout from the variation picker.
51-
await editor.canvas.click(
52-
'role=button[name="Group: Gather blocks in a container."i]'
53-
);
55+
await editor.canvas
56+
.locator(
57+
'role=button[name="Group: Gather blocks in a container."i]'
58+
)
59+
.click();
5460

5561
expect( await editor.getEditedPostContent() ).toMatchSnapshot();
5662
} );
@@ -60,10 +66,12 @@ test.describe( 'Group', () => {
6066
page,
6167
} ) => {
6268
await editor.insertBlock( { name: 'core/group' } );
63-
await editor.canvas.click(
64-
'button[aria-label="Group: Gather blocks in a container."]'
65-
);
66-
await editor.canvas.click( 'role=button[name="Add block"i]' );
69+
await editor.canvas
70+
.locator(
71+
'button[aria-label="Group: Gather blocks in a container."]'
72+
)
73+
.click();
74+
await editor.canvas.locator( 'role=button[name="Add block"i]' ).click();
6775
await page.click(
6876
'role=listbox[name="Blocks"i] >> role=option[name="Paragraph"i]'
6977
);

0 commit comments

Comments
 (0)