diff --git a/test/e2e/specs/editor/plugins/wp-editor-meta-box.spec.js b/test/e2e/specs/editor/plugins/wp-editor-meta-box.spec.js index 5579bdb0fa9a36..08fe0f8391e752 100644 --- a/test/e2e/specs/editor/plugins/wp-editor-meta-box.spec.js +++ b/test/e2e/specs/editor/plugins/wp-editor-meta-box.spec.js @@ -29,18 +29,25 @@ test.describe( 'WP Editor Meta Boxes', () => { await page .getByRole( 'button', { name: 'Meta Boxes' } ) .click( { position: { x: 0, y: 0 } } ); + + // Switch to Visual mode and wait for TinyMCE to fully initialize. + // This ensures getSelection() won't return null when TinyMCE tries + // to restore cursor position during subsequent mode switches. await page.locator( 'role=button[name="Visual"i]' ).click(); - // Switch tinymce to Text mode, first waiting for it to initialize - // because otherwise it will flip back to Visual mode once initialized. - await page.locator( '#test_tinymce_id_ifr' ).waitFor(); - await page.locator( 'role=button[name="Code"i]' ).click(); + await page.waitForFunction( + () => window.tinyMCE?.get( 'test_tinymce_id' )?.initialized + ); - // Type something in the tinymce Text mode textarea. + // Switch to Code mode and type into the textarea. + await page.locator( 'role=button[name="Code"i]' ).click(); const metaBoxField = page.locator( '#test_tinymce_id' ); await metaBoxField.type( 'Typing in a metabox' ); - // Switch tinymce back to Visual mode. + // Switch back to Visual mode and wait for re-initialization. await page.locator( 'role=button[name="Visual"i]' ).click(); + await page.waitForFunction( + () => window.tinyMCE?.get( 'test_tinymce_id' )?.initialized + ); await editor.publishPost(); await page.reload();