1+ import { Page } from '@playwright/test'
12import { test , expect } from 'e2e/test-utils'
23
34test . describe ( 'Block Editor' , ( ) => {
@@ -21,8 +22,9 @@ test.describe( 'Block Editor', () => {
2122
2223 editor,
2324 } ) => {
25+ // Insert Stackable Text Block through block inserter
26+ // Also checks if Stackable Block is in the list of blocks in the Editor
2427 await page . getByLabel ( 'Toggle block inserter' ) . click ( )
25-
2628 await page . locator ( '.editor-block-list-item-stackable-text' ) . click ( )
2729
2830 await expect ( editor . canvas . getByLabel ( 'Block: Text' ) ) . toBeVisible ( )
@@ -54,25 +56,22 @@ test.describe( 'Block Editor', () => {
5456 await editor . canvas . locator ( '[data-type="stackable/text"] > .stk-block-text > p[role="textbox"]' ) . fill ( 'test' )
5557 await page . locator ( '.stk-color-palette-control .stk-control-content > .components-dropdown > .components-button' ) . first ( ) . click ( )
5658 await page . getByLabel ( 'Hex color' ) . fill ( 'ff0000' )
59+
60+ // Click on the body to close the Color Picker popup
5761 await editor . canvas . locator ( 'body' ) . click ( )
5862
63+ // Verify if Text block contains correct content and color
5964 await expect ( editor . canvas . locator ( '[data-type="stackable/text"]' ) ) . toContainText ( 'test' )
6065 await expect ( editor . canvas . locator ( '[data-type="stackable/text"] > .stk-block-text > p[role="textbox"]' ) ) . toHaveCSS ( 'color' , 'rgb(255, 0, 0)' )
6166
6267 await editor . saveDraft ( )
6368
64- await page . waitForFunction (
65- ( ) => window ?. wp ?. blocks && window ?. wp ?. data
66- )
67-
69+ // Verify block attributes
6870 const clientId = await editor . canvas . getByLabel ( 'Block: Text' ) . getAttribute ( 'data-block' )
71+ const attributes = await editor . getBlockAttributes ( clientId )
6972
70- const attributes = await page . evaluate ( async ( [ _clientId ] ) => {
71- return await window . wp . data . select ( 'core/block-editor' ) . getBlockAttributes ( _clientId )
72- } , [ clientId ] )
73-
74- expect ( attributes ) . toHaveAttribute ( 'textColor1' , '#ff0000' )
75- expect ( attributes ) . toHaveAttribute ( 'text' , 'test' )
73+ expect ( attributes ) . toHaveProperty ( 'textColor1' , '#ff0000' )
74+ expect ( attributes ) . toHaveProperty ( 'text' , 'test' )
7675 } )
7776
7877 test ( 'The Stackable block added in the editor should be visible in the frontend' , async ( {
@@ -86,25 +85,27 @@ test.describe( 'Block Editor', () => {
8685 } ,
8786 } )
8887
89- await page . waitForFunction (
90- ( ) => window ?. wp ?. blocks && window ?. wp ?. data
91- )
92-
9388 const clientId = await editor . canvas . getByLabel ( 'Block: Text' ) . getAttribute ( 'data-block' )
94-
95- const attributes = await page . evaluate ( async ( [ _clientId ] ) => {
96- return await window . wp . data . select ( 'core/block-editor' ) . getBlockAttributes ( _clientId )
97- } , [ clientId ] )
98-
99- const uniqueId = attributes . uniqueId
89+ const uniqueId = ( await editor . getBlockAttributes ( clientId ) ) . uniqueId
10090
10191 await editor . saveDraft ( )
10292
103- const preview = await editor . openPreviewPage ( )
93+ let preview : Page
94+
95+ // openPreviewPage() from WordPress may fail as it relies on a button with a attribute name of 'view'
96+ // https://github.com/WordPress/gutenberg/blob/trunk/packages/e2e-test-utils-playwright/src/editor/preview.ts
97+ // Older versions of WordPress uses 'Preview' as the label for the Preview Button
98+ if ( await page . getByLabel ( 'View' , { exact : true } ) . isVisible ( ) ) {
99+ preview = await editor . openPreviewPage ( )
100+ } else {
101+ await page . getByLabel ( 'Preview' ) . click ( )
102+ const previewPromise = page . waitForEvent ( 'popup' )
103+ await page . getByRole ( 'menuitem' , { name : 'Preview in new tab' } ) . click ( )
104+ preview = await previewPromise
105+ }
104106
105107 await expect ( preview . locator ( `[data-block-id="${ uniqueId } "]` ) ) . toBeVisible ( )
106108 await expect ( preview . locator ( `[data-block-id="${ uniqueId } "]` ) ) . toContainText ( 'test' )
107109 await expect ( preview . locator ( `[data-block-id="${ uniqueId } "] p` ) ) . toHaveCSS ( 'color' , 'rgb(255, 0, 0)' )
108110 } )
109111} )
110-
0 commit comments