diff --git a/lib/client-assets.php b/lib/client-assets.php index 47d178667b2ba6..df7c8959d721a4 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -663,3 +663,42 @@ function gutenberg_extend_block_editor_settings_with_fse_theme_flag( $settings ) return $settings; } add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' ); + +/** + * Sets the editor styles to be consumed by JS. + */ +function gutenberg_extend_block_editor_styles_html() { + $handles = array( + 'wp-block-editor', + 'wp-block-library', + 'wp-edit-blocks', + ); + + $block_registry = WP_Block_Type_Registry::get_instance(); + + foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { + if ( ! empty( $block_type->style ) ) { + $handles[] = $block_type->style; + } + + if ( ! empty( $block_type->editor_style ) ) { + $handles[] = $block_type->editor_style; + } + } + + $handles = array_unique( $handles ); + $done = wp_styles()->done; + + ob_start(); + + wp_styles()->done = array(); + wp_styles()->do_items( $handles ); + wp_styles()->done = $done; + + $editor_styles = wp_json_encode( array( 'html' => ob_get_clean() ) ); + + echo ""; +} +add_action( 'admin_footer-post.php', 'gutenberg_extend_block_editor_styles_html' ); +add_action( 'admin_footer-post-new.php', 'gutenberg_extend_block_editor_styles_html' ); +add_action( 'admin_footer-toplevel_page_gutenberg-edit-site', 'gutenberg_extend_block_editor_styles_html' ); diff --git a/package-lock.json b/package-lock.json index 52fe92d9eba9c9..49f6473790c92d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1155,30 +1155,6 @@ "@babel/helper-plugin-utils": "^7.10.4" } }, - "@babel/polyfill": { - "version": "7.12.1", - "resolved": "https://registry.npmjs.org/@babel/polyfill/-/polyfill-7.12.1.tgz", - "integrity": "sha512-X0pi0V6gxLi6lFZpGmeNa4zxtwEmCs42isWLNjZZDE0Y8yVfgu0T2OAHlzBbdYlqbW/YXVvoBHpATEM+goCj8g==", - "dev": true, - "requires": { - "core-js": "^2.6.5", - "regenerator-runtime": "^0.13.4" - }, - "dependencies": { - "core-js": { - "version": "2.6.12", - "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.12.tgz", - "integrity": "sha512-Kb2wC0fvsWfQrgk8HU5lW6U/Lcs8+9aaYcy4ZFc6DDlo4nZ7n70dEgE5rtR0oG6ufKDUnrwfWL1mXR5ljDatrQ==", - "dev": true - }, - "regenerator-runtime": { - "version": "0.13.7", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.7.tgz", - "integrity": "sha512-a54FxoJDIr27pgf7IgeQGxmqUNYrcV338lf/6gH456HZ/PhX+5BcwHXG9ajESmwe6WRO0tAzRUrRmNONWgkrew==", - "dev": true - } - } - }, "@babel/preset-env": { "version": "7.12.7", "resolved": "https://registry.npmjs.org/@babel/preset-env/-/preset-env-7.12.7.tgz", @@ -13246,6 +13222,7 @@ "lodash": "^4.17.19", "memize": "^1.1.0", "react-autosize-textarea": "^7.1.0", + "react-merge-refs": "^1.0.0", "react-spring": "^8.0.19", "reakit": "1.1.0", "redux-multi": "^0.1.12", @@ -14834,6 +14811,33 @@ "keypather": "^1.10.2" } }, + "@babel/polyfill": { + "version": "7.10.4", + "resolved": "https://registry.npmjs.org/@babel/polyfill/-/polyfill-7.10.4.tgz", + "integrity": "sha512-8BYcnVqQ5kMD2HXoHInBH7H1b/uP3KdnwCYXOqFnXqguOyuu443WXusbIUbWEfY3Z0Txk0M1uG/8YuAMhNl6zg==", + "dev": true, + "requires": { + "core-js": "^2.6.5", + "regenerator-runtime": "^0.13.4" + }, + "dependencies": { + "core-js": { + "version": "2.6.11", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.11.tgz", + "integrity": "sha512-5wjnpaT/3dV+XB4borEsnAYQchn00XSgTAWKDkEqv+K8KevjbzmofK6hfJ9TZIlpj2N0xQpazy7PiRQiWHqzWg==", + "dev": true + } + } + }, + "@babel/runtime": { + "version": "7.10.4", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.10.4.tgz", + "integrity": "sha512-UpTN5yUJr9b4EX2CnGNWIvER7Ab83ibv0pcvvHc4UOdrBI5jb8bj+32cCwPX6xu0mt2daFNjYhoi+X7beH0RSw==", + "dev": true, + "requires": { + "regenerator-runtime": "^0.13.4" + } + }, "@dabh/diagnostics": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/@dabh/diagnostics/-/diagnostics-2.0.2.tgz", diff --git a/packages/block-editor/package.json b/packages/block-editor/package.json index bd98e24d88e2c3..0f0b58c305473b 100644 --- a/packages/block-editor/package.json +++ b/packages/block-editor/package.json @@ -60,6 +60,7 @@ "lodash": "^4.17.19", "memize": "^1.1.0", "react-autosize-textarea": "^7.1.0", + "react-merge-refs": "^1.0.0", "react-spring": "^8.0.19", "reakit": "1.1.0", "redux-multi": "^0.1.12", diff --git a/packages/block-editor/src/components/block-edit/edit.js b/packages/block-editor/src/components/block-edit/edit.js index 0746699eb145dc..9b1907c4cfb2b5 100644 --- a/packages/block-editor/src/components/block-edit/edit.js +++ b/packages/block-editor/src/components/block-edit/edit.js @@ -7,13 +7,13 @@ import { pick } from 'lodash'; /** * WordPress dependencies */ -import { withFilters } from '@wordpress/components'; +import { withFilters, Popover } from '@wordpress/components'; import { getBlockDefaultClassName, hasBlockSupport, getBlockType, } from '@wordpress/blocks'; -import { useContext, useMemo } from '@wordpress/element'; +import { useContext, useMemo, useState } from '@wordpress/element'; /** * Internal dependencies @@ -30,6 +30,31 @@ import BlockContext from '../block-context'; */ const DEFAULT_BLOCK_CONTEXT = {}; +function IframeCompat( { children } ) { + const [ ref, setRef ] = useState(); + const [ height, setHeight ] = useState(); + + return ( +
invalid paragraph' ); @@ -35,7 +36,7 @@ describe( 'invalid blocks', () => { expect( console ).toHaveWarned(); // Click on the 'three-dots' menu toggle - await page.click( + await canvas().click( '.block-editor-warning__actions button[aria-label="More options"]' ); diff --git a/packages/e2e-tests/specs/editor/various/keyboard-navigable-blocks.test.js b/packages/e2e-tests/specs/editor/various/keyboard-navigable-blocks.test.js index 2b092c9b466906..c6758c5a7dcd8e 100644 --- a/packages/e2e-tests/specs/editor/various/keyboard-navigable-blocks.test.js +++ b/packages/e2e-tests/specs/editor/various/keyboard-navigable-blocks.test.js @@ -8,13 +8,24 @@ import { clickBlockAppender, getEditedPostContent, showBlockToolbar, + canvas, + clickButton, } from '@wordpress/e2e-test-utils'; async function getActiveLabel() { return await page.evaluate( () => { + function getActiveElement( { activeElement } ) { + if ( activeElement.nodeName === 'IFRAME' ) { + return getActiveElement( activeElement.contentDocument ); + } + return activeElement; + } + + const activeElement = getActiveElement( document ); + return ( - document.activeElement.getAttribute( 'aria-label' ) || - document.activeElement.innerHTML + activeElement.getAttribute( 'aria-label' ) || + activeElement.innerHTML ); } ); } @@ -23,6 +34,7 @@ const navigateToContentEditorTop = async () => { // Use 'Ctrl+`' to return to the top of the editor await pressKeyWithModifier( 'ctrl', '`' ); await pressKeyWithModifier( 'ctrl', '`' ); + await expect( await getActiveLabel() ).toBe( 'Editor content' ); }; const tabThroughParagraphBlock = async ( paragraphText ) => { @@ -34,7 +46,7 @@ const tabThroughParagraphBlock = async ( paragraphText ) => { await page.keyboard.press( 'Tab' ); await expect( await getActiveLabel() ).toBe( 'Paragraph block' ); await expect( - await page.evaluate( () => document.activeElement.innerHTML ) + await canvas().evaluate( () => document.activeElement.innerHTML ) ).toBe( paragraphText ); await page.keyboard.press( 'Tab' ); @@ -109,13 +121,18 @@ describe( 'Order of block keyboard navigation', () => { } // Clear the selected block. - const paragraph = await page.$( '[data-type="core/paragraph"]' ); - const box = await paragraph.boundingBox(); - await page.mouse.click( box.x - 1, box.y ); + await clickButton( 'Document' ); + + // Put focus in front of the block list. + await page.evaluate( () => { + document + .querySelector( '.interface-interface-skeleton__content' ) + .focus(); + } ); await page.keyboard.press( 'Tab' ); await expect( - await page.evaluate( () => { + await canvas().evaluate( () => { return document.activeElement.placeholder; } ) ).toBe( 'Add title' ); @@ -144,9 +161,7 @@ describe( 'Order of block keyboard navigation', () => { } // Clear the selected block. - const paragraph = await page.$( '[data-type="core/paragraph"]' ); - const box = await paragraph.boundingBox(); - await page.mouse.click( box.x - 1, box.y ); + await clickButton( 'Document' ); // Put focus behind the block list. await page.evaluate( () => { @@ -167,10 +182,13 @@ describe( 'Order of block keyboard navigation', () => { await pressKeyWithModifier( 'shift', 'Tab' ); await expect( - await page.evaluate( () => { + await canvas().evaluate( () => { return document.activeElement.placeholder; } ) ).toBe( 'Add title' ); + + await pressKeyWithModifier( 'shift', 'Tab' ); + await expect( await getActiveLabel() ).toBe( 'Options' ); } ); it( 'should navigate correctly with multi selection', async () => { diff --git a/packages/e2e-tests/specs/editor/various/links.test.js b/packages/e2e-tests/specs/editor/various/links.test.js index 21c50094a0d1f3..481e4dbecb5672 100644 --- a/packages/e2e-tests/specs/editor/various/links.test.js +++ b/packages/e2e-tests/specs/editor/various/links.test.js @@ -8,6 +8,7 @@ import { createNewPost, pressKeyWithModifier, showBlockToolbar, + canvas, } from '@wordpress/e2e-test-utils'; describe( 'Links', () => { @@ -49,7 +50,7 @@ describe( 'Links', () => { await page.keyboard.press( 'Enter' ); - const actualText = await page.evaluate( + const actualText = await canvas().evaluate( () => document.querySelector( '.block-editor-rich-text__editable a' ) .textContent @@ -182,7 +183,7 @@ describe( 'Links', () => { await page.keyboard.type( 'https://wordpress.org/gutenberg' ); // Click somewhere else - it doesn't really matter where - await page.click( '.editor-post-title' ); + await canvas().click( '.editor-post-title' ); } ); const createAndReselectLink = async () => { @@ -312,7 +313,7 @@ describe( 'Links', () => { const createPostWithTitle = async ( titleText ) => { await createNewPost(); - await page.type( '.editor-post-title__input', titleText ); + await canvas().type( '.editor-post-title__input', titleText ); await page.click( '.editor-post-publish-panel__toggle' ); // Disable reason: Wait for the animation to complete, since otherwise the @@ -510,7 +511,7 @@ describe( 'Links', () => { await page.keyboard.press( 'Enter' ); // Wait for Gutenberg to finish the job. - await page.waitForXPath( + await canvas().waitForXPath( '//a[contains(@href,"w.org") and @target="_blank"]' ); @@ -551,7 +552,7 @@ describe( 'Links', () => { await page.keyboard.press( 'Space' ); // Wait for Gutenberg to finish the job. - await page.waitForXPath( + await canvas().waitForXPath( '//a[contains(@href,"wordpress.org") and not(@target)]' ); diff --git a/packages/e2e-tests/specs/editor/various/multi-block-selection.test.js b/packages/e2e-tests/specs/editor/various/multi-block-selection.test.js index bfa098f7f42406..9bb642a0cfbf06 100644 --- a/packages/e2e-tests/specs/editor/various/multi-block-selection.test.js +++ b/packages/e2e-tests/specs/editor/various/multi-block-selection.test.js @@ -9,10 +9,11 @@ import { getEditedPostContent, clickBlockToolbarButton, clickButton, + canvas, } from '@wordpress/e2e-test-utils'; async function getSelectedFlatIndices() { - return await page.evaluate( () => { + return await canvas().evaluate( () => { const indices = []; let single; @@ -38,7 +39,7 @@ async function getSelectedFlatIndices() { async function testNativeSelection() { // Wait for the selection to update. await page.evaluate( () => new Promise( window.requestAnimationFrame ) ); - await page.evaluate( () => { + await canvas().evaluate( () => { const selection = window.getSelection(); const elements = Array.from( document.querySelectorAll( '.is-multi-selected' ) @@ -263,7 +264,7 @@ describe( 'Multi-block selection', () => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '2' ); await page.keyboard.down( 'Shift' ); - await page.click( '[data-type="core/paragraph"]' ); + await canvas().click( '[data-type="core/paragraph"]' ); await page.keyboard.up( 'Shift' ); await testNativeSelection(); @@ -277,20 +278,21 @@ describe( 'Multi-block selection', () => { await page.keyboard.type( '2' ); await page.keyboard.press( 'ArrowUp' ); - const [ coord1, coord2 ] = await page.evaluate( () => { + const [ coord1, coord2 ] = await canvas().evaluate( () => { const elements = Array.from( document.querySelectorAll( '[data-type="core/paragraph"]' ) ); const rect1 = elements[ 0 ].getBoundingClientRect(); const rect2 = elements[ 1 ].getBoundingClientRect(); + const winRect = window.frameElement.getBoundingClientRect(); return [ { - x: rect1.x + rect1.width / 2, - y: rect1.y + rect1.height / 2, + x: winRect.x + rect1.x + rect1.width / 2, + y: winRect.y + rect1.y + rect1.height / 2, }, { - x: rect2.x + rect2.width / 2, - y: rect2.y + rect2.height / 2, + x: winRect.x + rect2.x + rect2.width / 2, + y: winRect.y + rect2.y + rect2.height / 2, }, ]; } ); @@ -314,7 +316,7 @@ describe( 'Multi-block selection', () => { ); await page.keyboard.press( 'Enter' ); - const groupAppender = await page.waitForSelector( + const groupAppender = await canvas().waitForSelector( '.block-editor-button-block-appender' ); await groupAppender.click(); @@ -326,21 +328,22 @@ describe( 'Multi-block selection', () => { await page.keyboard.type( '2' ); - const [ coord1, coord2 ] = await page.evaluate( () => { + const [ coord1, coord2 ] = await canvas().evaluate( () => { const elements = Array.from( document.querySelectorAll( '[data-type="core/paragraph"]' ) ); + elements[ 0 ].scrollIntoView(); const rect1 = elements[ 0 ].getBoundingClientRect(); const rect2 = elements[ 1 ].getBoundingClientRect(); - + const winRect = window.frameElement.getBoundingClientRect(); return [ { - x: rect1.x + rect1.width / 2, - y: rect1.y + rect1.height / 2, + x: winRect.x + rect1.x + rect1.width / 2, + y: winRect.y + rect1.y + rect1.height / 2, }, { - x: rect2.x + rect2.width / 2, - y: rect2.y + rect2.height / 2, + x: winRect.x + rect2.x + rect2.width / 2, + y: winRect.y + rect2.y + rect2.height / 2, }, ]; } ); @@ -396,7 +399,7 @@ describe( 'Multi-block selection', () => { await page.keyboard.type( '2' ); await page.keyboard.press( 'ArrowLeft' ); - const [ coord1, coord2 ] = await page.evaluate( () => { + const [ coord1, coord2 ] = await canvas().evaluate( () => { const selection = window.getSelection(); if ( ! selection.rangeCount ) { @@ -409,16 +412,17 @@ describe( 'Multi-block selection', () => { '[data-type="core/paragraph"]' ); const rect2 = element.getBoundingClientRect(); + const winRect = window.frameElement.getBoundingClientRect(); return [ { - x: rect1.x, - y: rect1.y + rect1.height / 2, + x: winRect.x + rect1.x, + y: winRect.y + rect1.y + rect1.height / 2, }, { // Move a bit outside the paragraph. - x: rect2.x - 10, - y: rect2.y + rect2.height / 2, + x: winRect.x + rect2.x - 10, + y: winRect.y + rect2.y + rect2.height / 2, }, ]; } ); @@ -447,20 +451,21 @@ describe( 'Multi-block selection', () => { await page.keyboard.press( 'Enter' ); await page.keyboard.type( '3' ); - const [ coord1, coord2 ] = await page.evaluate( () => { + const [ coord1, coord2 ] = await canvas().evaluate( () => { const elements = Array.from( document.querySelectorAll( '[data-type="core/paragraph"]' ) ); const rect1 = elements[ 2 ].getBoundingClientRect(); const rect2 = elements[ 1 ].getBoundingClientRect(); + const winRect = window.frameElement.getBoundingClientRect(); return [ { - x: rect1.x + rect1.width / 2, - y: rect1.y + rect1.height / 2, + x: winRect.x + rect1.x + rect1.width / 2, + y: winRect.y + rect1.y + rect1.height / 2, }, { - x: rect2.x + rect2.width / 2, - y: rect2.y + rect2.height / 2, + x: winRect.x + rect2.x + rect2.width / 2, + y: winRect.y + rect2.y + rect2.height / 2, }, ]; } ); @@ -491,14 +496,15 @@ describe( 'Multi-block selection', () => { await testNativeSelection(); expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2 ] ); - const coord = await page.evaluate( () => { + const coord = await canvas().evaluate( () => { const element = document.querySelector( '[data-type="core/paragraph"]' ); const rect = element.getBoundingClientRect(); + const winRect = window.frameElement.getBoundingClientRect(); return { - x: rect.x - 1, - y: rect.y + rect.height / 2, + x: winRect.x + rect.x - 1, + y: winRect.y + rect.y + rect.height / 2, }; } ); diff --git a/packages/e2e-tests/specs/editor/various/navigable-toolbar.test.js b/packages/e2e-tests/specs/editor/various/navigable-toolbar.test.js index e108828c31e638..d50807177427eb 100644 --- a/packages/e2e-tests/specs/editor/various/navigable-toolbar.test.js +++ b/packages/e2e-tests/specs/editor/various/navigable-toolbar.test.js @@ -1,7 +1,11 @@ /** * WordPress dependencies */ -import { createNewPost, pressKeyWithModifier } from '@wordpress/e2e-test-utils'; +import { + createNewPost, + pressKeyWithModifier, + canvas, +} from '@wordpress/e2e-test-utils'; describe.each( [ [ 'unified', true ], @@ -49,32 +53,21 @@ describe.each( [ if ( ! isUnifiedToolbar ) { it( 'should not scroll page', async () => { - while ( - await page.evaluate( () => { - const scrollable = wp.dom.getScrollContainer( - document.activeElement - ); - return ! scrollable || scrollable.scrollTop === 0; - } ) - ) { + while ( await canvas().evaluate( () => window.scrollY === 0 ) ) { await page.keyboard.press( 'Enter' ); } await page.keyboard.type( 'a' ); - const scrollTopBefore = await page.evaluate( - () => - wp.dom.getScrollContainer( document.activeElement ) - .scrollTop + const scrollTopBefore = await canvas().evaluate( + () => window.scrollY ); await pressKeyWithModifier( 'alt', 'F10' ); expect( await isInBlockToolbar() ).toBe( true ); - const scrollTopAfter = await page.evaluate( - () => - wp.dom.getScrollContainer( document.activeElement ) - .scrollTop + const scrollTopAfter = await canvas().evaluate( + () => window.scrollY ); expect( scrollTopBefore ).toBe( scrollTopAfter ); diff --git a/packages/e2e-tests/specs/editor/various/new-post-default-content.test.js b/packages/e2e-tests/specs/editor/various/new-post-default-content.test.js index 4c917f265eaf30..857f1b7a5dfd6e 100644 --- a/packages/e2e-tests/specs/editor/various/new-post-default-content.test.js +++ b/packages/e2e-tests/specs/editor/various/new-post-default-content.test.js @@ -8,6 +8,8 @@ import { findSidebarPanelToggleButtonWithTitle, getEditedPostContent, openDocumentSettingsSidebar, + canvas, + clickButton, } from '@wordpress/e2e-test-utils'; describe( 'new editor filtered state', () => { @@ -25,7 +27,7 @@ describe( 'new editor filtered state', () => { it( 'should respect default content', async () => { // get the values that should have their defaults changed. - const title = await page.$eval( + const title = await canvas().$eval( '.editor-post-title__input', ( element ) => element.innerHTML ); @@ -33,6 +35,7 @@ describe( 'new editor filtered state', () => { // open the sidebar, we want to see the excerpt. await openDocumentSettingsSidebar(); + await clickButton( 'Post' ); const excerptButton = await findSidebarPanelToggleButtonWithTitle( 'Excerpt' ); diff --git a/packages/e2e-tests/specs/editor/various/new-post.test.js b/packages/e2e-tests/specs/editor/various/new-post.test.js index e8baff2d573fb2..a24fc390d21b4b 100644 --- a/packages/e2e-tests/specs/editor/various/new-post.test.js +++ b/packages/e2e-tests/specs/editor/various/new-post.test.js @@ -5,6 +5,7 @@ import { activatePlugin, createNewPost, deactivatePlugin, + canvas, } from '@wordpress/e2e-test-utils'; describe( 'new editor state', () => { @@ -25,7 +26,7 @@ describe( 'new editor state', () => { expect.stringContaining( 'post-new.php' ) ); // Should display the blank title. - const title = await page.$( '[placeholder="Add title"]' ); + const title = await canvas().$( '[placeholder="Add title"]' ); expect( title ).not.toBeNull(); expect( title.innerHTML ).toBeFalsy(); // Should display the Preview button. @@ -51,10 +52,10 @@ describe( 'new editor state', () => { } ); it( 'should focus the title if the title is empty', async () => { - const activeElementClasses = await page.evaluate( () => { + const activeElementClasses = await canvas().evaluate( () => { return Object.values( document.activeElement.classList ); } ); - const activeElementTagName = await page.evaluate( () => { + const activeElementTagName = await canvas().evaluate( () => { return document.activeElement.tagName.toLowerCase(); } ); @@ -64,7 +65,7 @@ describe( 'new editor state', () => { it( 'should not focus the title if the title exists', async () => { // Enter a title for this post. - await page.type( '.editor-post-title__input', 'Here is the title' ); + await canvas().type( '.editor-post-title__input', 'Here is the title' ); // Save the post as a draft. await page.click( '.editor-post-save-draft' ); await page.waitForSelector( '.editor-post-saved-state.is-saved' ); @@ -72,10 +73,10 @@ describe( 'new editor state', () => { await page.reload(); await page.waitForSelector( '.edit-post-layout' ); - const activeElementClasses = await page.evaluate( () => { + const activeElementClasses = await canvas().evaluate( () => { return Object.values( document.activeElement.classList ); } ); - const activeElementTagName = await page.evaluate( () => { + const activeElementTagName = await canvas().evaluate( () => { return document.activeElement.tagName.toLowerCase(); } ); diff --git a/packages/e2e-tests/specs/editor/various/post-visibility.test.js b/packages/e2e-tests/specs/editor/various/post-visibility.test.js index 6844b9bd97345b..84ef7ce75af324 100644 --- a/packages/e2e-tests/specs/editor/various/post-visibility.test.js +++ b/packages/e2e-tests/specs/editor/various/post-visibility.test.js @@ -5,6 +5,7 @@ import { setBrowserViewport, createNewPost, openDocumentSettingsSidebar, + canvas, } from '@wordpress/e2e-test-utils'; describe( 'Post visibility', () => { @@ -40,7 +41,7 @@ describe( 'Post visibility', () => { await createNewPost(); // Enter a title for this post. - await page.type( '.editor-post-title__input', 'Title' ); + await canvas().type( '.editor-post-title__input', 'Title' ); await openDocumentSettingsSidebar(); @@ -61,7 +62,7 @@ describe( 'Post visibility', () => { await privateLabel.click(); // Enter a title for this post. - await page.type( '.editor-post-title__input', ' Changed' ); + await canvas().type( '.editor-post-title__input', ' Changed' ); // Wait for the button to be clickable before attempting to click. // This could cause errors when we try to click before changes are registered. diff --git a/packages/e2e-tests/specs/editor/various/preview.test.js b/packages/e2e-tests/specs/editor/various/preview.test.js index d0996211a512c9..d2761365c30d3b 100644 --- a/packages/e2e-tests/specs/editor/various/preview.test.js +++ b/packages/e2e-tests/specs/editor/various/preview.test.js @@ -11,6 +11,7 @@ import { publishPost, saveDraft, openPreviewPage, + canvas, } from '@wordpress/e2e-test-utils'; /** @typedef {import('puppeteer').Page} Page */ @@ -95,7 +96,7 @@ describe( 'Preview', () => { ); expect( isPreviewDisabled ).toBe( true ); - await editorPage.type( '.editor-post-title__input', 'Hello World' ); + await canvas().type( '.editor-post-title__input', 'Hello World' ); const previewPage = await openPreviewPage( editorPage ); await previewPage.waitForSelector( '.entry-title' ); @@ -123,7 +124,7 @@ describe( 'Preview', () => { // Return to editor to change title. await editorPage.bringToFront(); - await editorPage.type( '.editor-post-title__input', '!' ); + await canvas().type( '.editor-post-title__input', '!' ); await waitForPreviewDropdownOpen( editorPage ); await waitForPreviewNavigation( previewPage ); @@ -150,7 +151,7 @@ describe( 'Preview', () => { // Return to editor to change title. await editorPage.bringToFront(); - await editorPage.type( '.editor-post-title__input', ' And more.' ); + await canvas().type( '.editor-post-title__input', ' And more.' ); await waitForPreviewDropdownOpen( editorPage ); await waitForPreviewNavigation( previewPage ); @@ -189,7 +190,7 @@ describe( 'Preview', () => { const editorPage = page; // Type aaaaa in the title filed. - await editorPage.type( '.editor-post-title__input', 'aaaaa' ); + await canvas().type( '.editor-post-title__input', 'aaaaa' ); await editorPage.keyboard.press( 'Tab' ); // Save the post as a draft. @@ -211,7 +212,7 @@ describe( 'Preview', () => { await editorPage.bringToFront(); // Append bbbbb to the title, and tab away from the title so blur event is triggered. - await editorPage.type( '.editor-post-title__input', 'bbbbb' ); + await canvas().type( '.editor-post-title__input', 'bbbbb' ); await editorPage.keyboard.press( 'Tab' ); // Save draft and open the preview page right after. @@ -246,9 +247,9 @@ describe( 'Preview with Custom Fields enabled', () => { const editorPage = page; // Make sure input is mounted in editor before adding content. - await editorPage.waitForSelector( '.editor-post-title__input' ); + await canvas().waitForSelector( '.editor-post-title__input' ); // Add an initial title and content. - await editorPage.type( '.editor-post-title__input', 'title 1' ); + await canvas().type( '.editor-post-title__input', 'title 1' ); await editorPage.keyboard.press( 'Tab' ); await editorPage.keyboard.type( 'content 1' ); @@ -275,7 +276,7 @@ describe( 'Preview with Custom Fields enabled', () => { // Return to editor and modify the title and content. await editorPage.bringToFront(); - await editorPage.click( '.editor-post-title__input' ); + await canvas().click( '.editor-post-title__input' ); await editorPage.keyboard.press( 'End' ); await editorPage.keyboard.press( 'Backspace' ); await editorPage.keyboard.type( '2' ); @@ -318,7 +319,7 @@ describe( 'Preview with private custom post type', () => { await createNewPost( { postType: 'not_public' } ); // Type in the title filed. - await page.type( '.editor-post-title__input', 'aaaaa' ); + await canvas().type( '.editor-post-title__input', 'aaaaa' ); await page.keyboard.press( 'Tab' ); // Open the preview menu. diff --git a/packages/e2e-tests/specs/editor/various/publish-button.test.js b/packages/e2e-tests/specs/editor/various/publish-button.test.js index d9396b7f48c386..26e6cdb0532fcf 100644 --- a/packages/e2e-tests/specs/editor/various/publish-button.test.js +++ b/packages/e2e-tests/specs/editor/various/publish-button.test.js @@ -6,6 +6,7 @@ import { disablePrePublishChecks, enablePrePublishChecks, createNewPost, + canvas, } from '@wordpress/e2e-test-utils'; describe( 'PostPublishButton', () => { @@ -32,7 +33,7 @@ describe( 'PostPublishButton', () => { } ); it( 'should be disabled when post is being saved', async () => { - await page.type( '.editor-post-title__input', 'E2E Test Post' ); // Make it saveable + await canvas().type( '.editor-post-title__input', 'E2E Test Post' ); // Make it saveable expect( await page.$( '.editor-post-publish-button[aria-disabled="true"]' ) ).toBeNull(); @@ -44,7 +45,7 @@ describe( 'PostPublishButton', () => { } ); it( 'should be disabled when metabox is being saved', async () => { - await page.type( '.editor-post-title__input', 'E2E Test Post' ); // Make it saveable + await canvas().type( '.editor-post-title__input', 'E2E Test Post' ); // Make it saveable expect( await page.$( '.editor-post-publish-button[aria-disabled="true"]' ) ).toBeNull(); diff --git a/packages/e2e-tests/specs/editor/various/publish-panel.test.js b/packages/e2e-tests/specs/editor/various/publish-panel.test.js index 3a6aefd8f66870..333f2f1c2a8b3b 100644 --- a/packages/e2e-tests/specs/editor/various/publish-panel.test.js +++ b/packages/e2e-tests/specs/editor/various/publish-panel.test.js @@ -9,6 +9,7 @@ import { openPublishPanel, pressKeyWithModifier, publishPost, + canvas, } from '@wordpress/e2e-test-utils'; describe( 'PostPublishPanel', () => { @@ -28,7 +29,7 @@ describe( 'PostPublishPanel', () => { } ); it( 'PrePublish: publish button should have the focus', async () => { - await page.type( '.editor-post-title__input', 'E2E Test Post' ); + await canvas().type( '.editor-post-title__input', 'E2E Test Post' ); await openPublishPanel(); const focusedElementClassList = await page.$eval( @@ -44,7 +45,7 @@ describe( 'PostPublishPanel', () => { it( 'PostPublish: post link should have the focus', async () => { const postTitle = 'E2E Test Post'; - await page.type( '.editor-post-title__input', postTitle ); + await canvas().type( '.editor-post-title__input', postTitle ); await publishPost(); const focusedElementTag = await page.$eval( @@ -64,7 +65,7 @@ describe( 'PostPublishPanel', () => { } ); it( 'should retain focus within the panel', async () => { - await page.type( '.editor-post-title__input', 'E2E Test Post' ); + await canvas().type( '.editor-post-title__input', 'E2E Test Post' ); await openPublishPanel(); await pressKeyWithModifier( 'shift', 'Tab' ); diff --git a/packages/e2e-tests/specs/editor/various/publishing.test.js b/packages/e2e-tests/specs/editor/various/publishing.test.js index 745008a0a15f1f..b86a06c50118b2 100644 --- a/packages/e2e-tests/specs/editor/various/publishing.test.js +++ b/packages/e2e-tests/specs/editor/various/publishing.test.js @@ -9,6 +9,7 @@ import { disablePrePublishChecks, arePrePublishChecksEnabled, setBrowserViewport, + canvas, } from '@wordpress/e2e-test-utils'; describe( 'Publishing', () => { @@ -30,7 +31,7 @@ describe( 'Publishing', () => { } ); it( `should publish the ${ postType } and close the panel once we start editing again.`, async () => { - await page.type( '.editor-post-title__input', 'E2E Test Post' ); + await canvas().type( '.editor-post-title__input', 'E2E Test Post' ); await publishPost(); @@ -40,7 +41,7 @@ describe( 'Publishing', () => { ).not.toBeNull(); // Start editing again. - await page.type( '.editor-post-title__input', ' (Updated)' ); + await canvas().type( '.editor-post-title__input', ' (Updated)' ); // The post-publishing panel is not visible anymore. expect( await page.$( '.editor-post-publish-panel' ) ).toBeNull(); @@ -67,7 +68,10 @@ describe( 'Publishing', () => { } ); it( `should publish the ${ postType } without opening the post-publish sidebar.`, async () => { - await page.type( '.editor-post-title__input', 'E2E Test Post' ); + await canvas().type( + '.editor-post-title__input', + 'E2E Test Post' + ); // The "Publish" button should be shown instead of the "Publish..." toggle expect( diff --git a/packages/e2e-tests/specs/editor/various/reusable-blocks.test.js b/packages/e2e-tests/specs/editor/various/reusable-blocks.test.js index 0d7c6d8baf236f..e438a722ce71a3 100644 --- a/packages/e2e-tests/specs/editor/various/reusable-blocks.test.js +++ b/packages/e2e-tests/specs/editor/various/reusable-blocks.test.js @@ -12,6 +12,7 @@ import { trashAllPosts, visitAdminPage, toggleGlobalBlockInserter, + canvas, } from '@wordpress/e2e-test-utils'; describe( 'Reusable blocks', () => { @@ -44,7 +45,7 @@ describe( 'Reusable blocks', () => { await page.waitForXPath( '//*[contains(@class, "components-snackbar")]/*[text()="Block created."]' ); - await page.waitForXPath( + await canvas().waitForXPath( '//*[@class="block-library-block__reusable-block-container"]' ); @@ -55,24 +56,20 @@ describe( 'Reusable blocks', () => { await page.keyboard.type( 'Greeting block' ); // Save the reusable block - const [ saveButton ] = await page.$x( '//button[text()="Save"]' ); + const [ saveButton ] = await canvas().$x( '//button[text()="Save"]' ); await saveButton.click(); // Wait for saving to finish - await page.waitForXPath( '//button[text()="Edit"]' ); + await canvas().waitForXPath( '//button[text()="Edit"]' ); // Check that we have a reusable block on the page - const block = await page.$( + const block = await canvas().$( '.block-editor-block-list__block[data-type="core/block"]' ); expect( block ).not.toBeNull(); // Check that its title is displayed - const title = await page.$eval( - '.reusable-block-edit-panel__info', - ( element ) => element.innerText - ); - expect( title ).toBe( 'Greeting block' ); + await canvas().waitForXPath( '//b[text()="Greeting block"]' ); } ); it( 'can be created with no title', async () => { @@ -87,29 +84,25 @@ describe( 'Reusable blocks', () => { await page.waitForXPath( '//*[contains(@class, "components-snackbar")]/*[text()="Block created."]' ); - await page.waitForXPath( + await canvas().waitForXPath( '//*[@class="block-library-block__reusable-block-container"]' ); // Save the reusable block - const [ saveButton ] = await page.$x( '//button[text()="Save"]' ); + const [ saveButton ] = await canvas().$x( '//button[text()="Save"]' ); await saveButton.click(); // Wait for saving to finish - await page.waitForXPath( '//button[text()="Edit"]' ); + await canvas().waitForXPath( '//button[text()="Edit"]' ); // Check that we have a reusable block on the page - const block = await page.$( + const block = await canvas().$( '.block-editor-block-list__block[data-type="core/block"]' ); expect( block ).not.toBeNull(); // Check that it is untitled - const title = await page.$eval( - '.reusable-block-edit-panel__info', - ( element ) => element.innerText - ); - expect( title ).toBe( 'Untitled Reusable Block' ); + await canvas().waitForXPath( '//b[text()="Untitled Reusable Block"]' ); } ); it( 'can be inserted and edited', async () => { @@ -117,9 +110,10 @@ describe( 'Reusable blocks', () => { await insertReusableBlock( 'Greeting block' ); // Put the reusable block in edit mode - const editButton = await page.waitForXPath( + const editButton = await canvas().waitForXPath( '//button[text()="Edit" and not(@disabled)]' ); + await editButton.click(); // Change the block's title @@ -137,27 +131,23 @@ describe( 'Reusable blocks', () => { await page.keyboard.type( 'Oh! ' ); // Save the reusable block - const [ saveButton ] = await page.$x( '//button[text()="Save"]' ); + const [ saveButton ] = await canvas().$x( '//button[text()="Save"]' ); await saveButton.click(); // Wait for saving to finish - await page.waitForXPath( '//button[text()="Edit"]' ); + await canvas().waitForXPath( '//button[text()="Edit"]' ); // Check that we have a reusable block on the page - const block = await page.$( + const block = await canvas().$( '.block-editor-block-list__block[data-type="core/block"]' ); expect( block ).not.toBeNull(); // Check that its title is displayed - const title = await page.$eval( - '.reusable-block-edit-panel__info', - ( element ) => element.innerText - ); - expect( title ).toBe( 'Surprised greeting block' ); + await canvas().waitForXPath( '//b[text()="Surprised greeting block"]' ); // Check that its content is up to date - const text = await page.$eval( + const text = await canvas().$eval( '.block-editor-block-list__block[data-type="core/block"] p', ( element ) => element.innerText ); @@ -177,7 +167,7 @@ describe( 'Reusable blocks', () => { await page.waitForXPath( '//*[contains(@class, "components-snackbar")]/*[text()="Block created."]' ); - await page.waitForXPath( + await canvas().waitForXPath( '//*[@class="block-library-block__reusable-block-container"]' ); @@ -188,7 +178,7 @@ describe( 'Reusable blocks', () => { await page.keyboard.type( 'Awesome block' ); // Save the reusable block - const [ saveButton ] = await page.$x( '//button[text()="Save"]' ); + const [ saveButton ] = await canvas().$x( '//button[text()="Save"]' ); await saveButton.click(); // Step 2. Create new post. @@ -200,17 +190,13 @@ describe( 'Reusable blocks', () => { await insertReusableBlock( 'Awesome block' ); // Check that we have a reusable block on the page - const block = await page.$( + const block = await canvas().$( '.block-editor-block-list__block[data-type="core/block"]' ); expect( block ).not.toBeNull(); // Check that its title is displayed - const title = await page.$eval( - '.reusable-block-edit-panel__info', - ( element ) => element.innerText - ); - expect( title ).toBe( 'Awesome block' ); + await canvas().waitForXPath( '//b[text()="Awesome block"]' ); } ); it( 'can be converted to a regular block', async () => { @@ -221,13 +207,13 @@ describe( 'Reusable blocks', () => { await clickBlockToolbarButton( 'Convert to regular blocks', 'content' ); // Check that we have a paragraph block on the page - const block = await page.$( + const block = await canvas().$( '.block-editor-block-list__block[data-type="core/paragraph"]' ); expect( block ).not.toBeNull(); // Check that its content is up to date - const text = await page.$eval( + const text = await canvas().$eval( '.block-editor-block-list__block[data-type="core/paragraph"]', ( element ) => element.innerText ); @@ -255,7 +241,7 @@ describe( 'Reusable blocks', () => { await page.waitForXPath( '//*[contains(@class, "components-snackbar")]/*[text()="Block created."]' ); - await page.waitForXPath( + await canvas().waitForXPath( '//*[@class="block-library-block__reusable-block-container"]' ); @@ -266,24 +252,22 @@ describe( 'Reusable blocks', () => { await page.keyboard.type( 'Multi-selection reusable block' ); // Save the reusable block - const [ saveButton ] = await page.$x( '//button[text()="Save"]' ); + const [ saveButton ] = await canvas().$x( '//button[text()="Save"]' ); await saveButton.click(); // Wait for saving to finish - await page.waitForXPath( '//button[text()="Edit"]' ); + await canvas().waitForXPath( '//button[text()="Edit"]' ); // Check that we have a reusable block on the page - const block = await page.$( + const block = await canvas().$( '.block-editor-block-list__block[data-type="core/block"]' ); expect( block ).not.toBeNull(); // Check that its title is displayed - const title = await page.$eval( - '.reusable-block-edit-panel__info', - ( element ) => element.innerText + await canvas().waitForXPath( + '//b[text()="Multi-selection reusable block"]' ); - expect( title ).toBe( 'Multi-selection reusable block' ); } ); it( 'multi-selection reusable block can be converted back to regular blocks', async () => { @@ -309,10 +293,12 @@ describe( 'Reusable blocks', () => { await page.waitForNavigation(); + await page.waitForSelector( 'iframe[title="Editor canvas"]' ); + // Click the block to give it focus const blockSelector = 'p[data-title="Paragraph"]'; - await page.waitForSelector( blockSelector ); - await page.click( blockSelector ); + await canvas().waitForSelector( blockSelector ); + await canvas().click( blockSelector ); // Delete the block, leaving the reusable block empty await clickBlockToolbarButton( 'More options' ); diff --git a/packages/e2e-tests/specs/editor/various/rich-text.test.js b/packages/e2e-tests/specs/editor/various/rich-text.test.js index 17e064b7c58523..eda4c58d9b7003 100644 --- a/packages/e2e-tests/specs/editor/various/rich-text.test.js +++ b/packages/e2e-tests/specs/editor/various/rich-text.test.js @@ -8,6 +8,7 @@ import { clickBlockAppender, pressKeyWithModifier, showBlockToolbar, + canvas, } from '@wordpress/e2e-test-utils'; describe( 'RichText', () => { @@ -73,7 +74,7 @@ describe( 'RichText', () => { await pressKeyWithModifier( 'shift', 'ArrowLeft' ); await pressKeyWithModifier( 'primary', 'b' ); - const count = await page.evaluate( + const count = await canvas().evaluate( () => document.querySelectorAll( '*[data-rich-text-format-boundary]' ) .length @@ -155,7 +156,7 @@ describe( 'RichText', () => { await pressKeyWithModifier( 'primary', 'b' ); await page.keyboard.type( '3' ); - await page.evaluate( () => { + await canvas().evaluate( () => { let called; const { body } = document; const config = { @@ -215,7 +216,7 @@ describe( 'RichText', () => { await page.keyboard.type( '4' ); - await page.evaluate( () => { + await canvas().evaluate( () => { // The selection change event should be called once. If there's only // one item in `window.unsubscribes`, it means that only one // function is present to disconnect the `mutationObserver`. @@ -256,7 +257,7 @@ describe( 'RichText', () => { await page.keyboard.press( 'Enter' ); // Wait for rich text editor to load. - await page.waitForSelector( '.block-editor-rich-text__editable' ); + await canvas().waitForSelector( '.block-editor-rich-text__editable' ); await pressKeyWithModifier( 'primary', 'b' ); await page.keyboard.type( '12' ); @@ -288,7 +289,14 @@ describe( 'RichText', () => { // Simulate moving focus to a different app, then moving focus back, // without selection being changed. await page.evaluate( () => { - const activeElement = document.activeElement; + function getActiveElement( { activeElement } ) { + if ( activeElement.nodeName === 'IFRAME' ) { + return getActiveElement( activeElement.contentDocument ); + } + return activeElement; + } + + const activeElement = getActiveElement( document ); activeElement.blur(); activeElement.focus(); } ); diff --git a/packages/e2e-tests/specs/editor/various/rtl.test.js b/packages/e2e-tests/specs/editor/various/rtl.test.js index 67f66ee77cc63a..8667a653841275 100644 --- a/packages/e2e-tests/specs/editor/various/rtl.test.js +++ b/packages/e2e-tests/specs/editor/various/rtl.test.js @@ -5,6 +5,7 @@ import { createNewPost, getEditedPostContent, pressKeyWithModifier, + canvas, } from '@wordpress/e2e-test-utils'; // Avoid using three, as it looks too much like two with some fonts. @@ -15,7 +16,7 @@ const ARABIC_TWO = '٢'; describe( 'RTL', () => { beforeEach( async () => { await createNewPost(); - await page.evaluate( () => { + await canvas().evaluate( () => { document.querySelector( '.is-root-container' ).dir = 'rtl'; } ); } ); @@ -102,7 +103,7 @@ describe( 'RTL', () => { await page.keyboard.press( 'Enter' ); // Wait for rich text editor to load. - await page.waitForSelector( '.block-editor-rich-text__editable' ); + await canvas().waitForSelector( '.block-editor-rich-text__editable' ); await pressKeyWithModifier( 'primary', 'b' ); await page.keyboard.type( ARABIC_ONE ); diff --git a/packages/e2e-tests/specs/editor/various/sidebar-permalink-panel.test.js b/packages/e2e-tests/specs/editor/various/sidebar-permalink-panel.test.js index 35b51001788cdb..075325966bce54 100644 --- a/packages/e2e-tests/specs/editor/various/sidebar-permalink-panel.test.js +++ b/packages/e2e-tests/specs/editor/various/sidebar-permalink-panel.test.js @@ -7,6 +7,7 @@ import { deactivatePlugin, findSidebarPanelWithTitle, publishPost, + canvas, } from '@wordpress/e2e-test-utils'; const permalinkPanelXPath = `//div[contains(@class, "edit-post-sidebar")]//button[contains(@class, "components-panel__body-toggle") and contains(text(),"Permalink")]`; @@ -38,7 +39,7 @@ describe( 'Sidebar Permalink Panel', () => { await page.keyboard.type( 'aaaaa' ); await publishPost(); // Start editing again. - await page.type( '.editor-post-title__input', ' (Updated)' ); + await canvas().type( '.editor-post-title__input', ' (Updated)' ); expect( await page.$x( permalinkPanelXPath ) ).toEqual( [] ); } ); @@ -47,7 +48,7 @@ describe( 'Sidebar Permalink Panel', () => { await page.keyboard.type( 'aaaaa' ); await publishPost(); // Start editing again. - await page.type( '.editor-post-title__input', ' (Updated)' ); + await canvas().type( '.editor-post-title__input', ' (Updated)' ); expect( await page.$x( permalinkPanelXPath ) ).toEqual( [] ); } ); @@ -56,7 +57,7 @@ describe( 'Sidebar Permalink Panel', () => { await page.keyboard.type( 'aaaaa' ); await publishPost(); // Start editing again. - await page.type( '.editor-post-title__input', ' (Updated)' ); + await canvas().type( '.editor-post-title__input', ' (Updated)' ); expect( await findSidebarPanelWithTitle( 'Permalink' ) ).toBeDefined(); } ); } ); diff --git a/packages/e2e-tests/specs/editor/various/splitting-merging.test.js b/packages/e2e-tests/specs/editor/various/splitting-merging.test.js index 05e08db82afce6..aadfb9e43efed3 100644 --- a/packages/e2e-tests/specs/editor/various/splitting-merging.test.js +++ b/packages/e2e-tests/specs/editor/various/splitting-merging.test.js @@ -8,6 +8,7 @@ import { getEditedPostContent, pressKeyTimes, pressKeyWithModifier, + canvas, } from '@wordpress/e2e-test-utils'; describe( 'splitting and merging blocks', () => { @@ -195,7 +196,7 @@ describe( 'splitting and merging blocks', () => { // There is a default block: expect( - await page.$$( '.block-editor-block-list__block' ) + await canvas().$$( '.block-editor-block-list__block' ) ).toHaveLength( 1 ); // But the effective saved content is still empty: diff --git a/packages/e2e-tests/specs/editor/various/taxonomies.test.js b/packages/e2e-tests/specs/editor/various/taxonomies.test.js index 2ed1c18d2c361e..8784ee051b26c3 100644 --- a/packages/e2e-tests/specs/editor/various/taxonomies.test.js +++ b/packages/e2e-tests/specs/editor/various/taxonomies.test.js @@ -11,6 +11,7 @@ import { findSidebarPanelWithTitle, openDocumentSettingsSidebar, publishPost, + canvas, } from '@wordpress/e2e-test-utils'; /** @@ -108,7 +109,7 @@ describe( 'Taxonomies', () => { expect( selectedCategories[ 0 ] ).toEqual( 'z rand category 1' ); // Type something in the title so we can publish the post. - await page.type( '.editor-post-title__input', 'Hello World' ); + await canvas().type( '.editor-post-title__input', 'Hello World' ); // Publish the post. await publishPost(); @@ -166,7 +167,7 @@ describe( 'Taxonomies', () => { expect( tags[ 0 ] ).toEqual( tagName ); // Type something in the title so we can publish the post. - await page.type( '.editor-post-title__input', 'Hello World' ); + await canvas().type( '.editor-post-title__input', 'Hello World' ); // Publish the post. await publishPost(); @@ -225,7 +226,7 @@ describe( 'Taxonomies', () => { expect( tags[ 0 ] ).toEqual( tagName ); // Type something in the title so we can publish the post. - await page.type( '.editor-post-title__input', 'Hello World' ); + await canvas().type( '.editor-post-title__input', 'Hello World' ); // Publish the post. await publishPost(); diff --git a/packages/e2e-tests/specs/editor/various/toolbar-roving-tabindex.test.js b/packages/e2e-tests/specs/editor/various/toolbar-roving-tabindex.test.js index b31c088352a63a..d490cb9c37c7be 100644 --- a/packages/e2e-tests/specs/editor/various/toolbar-roving-tabindex.test.js +++ b/packages/e2e-tests/specs/editor/various/toolbar-roving-tabindex.test.js @@ -6,15 +6,16 @@ import { pressKeyWithModifier, clickBlockToolbarButton, insertBlock, + canvas, } from '@wordpress/e2e-test-utils'; async function focusBlockToolbar() { await pressKeyWithModifier( 'alt', 'F10' ); } -async function expectLabelToHaveFocus( label ) { +async function expectLabelToHaveFocus( label, p = page ) { await expect( - await page.evaluate( () => + await p.evaluate( () => document.activeElement.getAttribute( 'aria-label' ) ) ).toBe( label ); @@ -26,7 +27,7 @@ async function testBlockToolbarKeyboardNavigation( currentBlockLabel ) { await page.keyboard.press( 'ArrowRight' ); await expectLabelToHaveFocus( 'Move up' ); await page.keyboard.press( 'Tab' ); - await expectLabelToHaveFocus( currentBlockLabel ); + await expectLabelToHaveFocus( currentBlockLabel, canvas() ); await pressKeyWithModifier( 'shift', 'Tab' ); await expectLabelToHaveFocus( 'Move up' ); } @@ -39,9 +40,9 @@ async function wrapCurrentBlockWithGroup() { } async function testGroupKeyboardNavigation( currentBlockLabel ) { - await expectLabelToHaveFocus( 'Block: Group' ); + await expectLabelToHaveFocus( 'Block: Group', canvas() ); await page.keyboard.press( 'Tab' ); - await expectLabelToHaveFocus( currentBlockLabel ); + await expectLabelToHaveFocus( currentBlockLabel, canvas() ); await pressKeyWithModifier( 'shift', 'Tab' ); await expectLabelToHaveFocus( 'Select parent (Group)' ); await page.keyboard.press( 'ArrowRight' ); @@ -92,7 +93,7 @@ describe( 'Toolbar roving tabindex', () => { // Move focus to the first toolbar item await page.keyboard.press( 'Home' ); await expectLabelToHaveFocus( 'Change block type or style' ); - await page.click( '.blocks-table__placeholder-button' ); + await canvas().click( '.blocks-table__placeholder-button' ); await testBlockToolbarKeyboardNavigation( 'Block: Table' ); await wrapCurrentBlockWithGroup(); await testGroupKeyboardNavigation( 'Block: Table' ); diff --git a/packages/e2e-tests/specs/editor/various/typewriter.test.js b/packages/e2e-tests/specs/editor/various/typewriter.test.js index d1983e465e785d..60daf820bb2793 100644 --- a/packages/e2e-tests/specs/editor/various/typewriter.test.js +++ b/packages/e2e-tests/specs/editor/various/typewriter.test.js @@ -1,15 +1,18 @@ /** * WordPress dependencies */ -import { createNewPost } from '@wordpress/e2e-test-utils'; +import { createNewPost, canvas } from '@wordpress/e2e-test-utils'; describe( 'TypeWriter', () => { beforeEach( async () => { await createNewPost(); } ); - const getCaretPosition = async () => - await page.evaluate( () => wp.dom.computeCaretRect( window ).y ); + async function getCaretPosition() { + return await canvas().evaluate( () => { + return window.top.wp.dom.computeCaretRect( window ).top; + } ); + } // Allow the scroll position to be 1px off. const BUFFER = 1; @@ -21,9 +24,6 @@ describe( 'TypeWriter', () => { // Create first block. await page.keyboard.press( 'Enter' ); - // Create second block. - await page.keyboard.press( 'Enter' ); - const initialPosition = await getCaretPosition(); // The page shouldn't be scrolled when it's being filled. @@ -32,13 +32,7 @@ describe( 'TypeWriter', () => { expect( await getCaretPosition() ).toBeGreaterThan( initialPosition ); // Create blocks until the the typewriter effect kicks in. - while ( - await page.evaluate( - () => - wp.dom.getScrollContainer( document.activeElement ) - .scrollTop === 0 - ) - ) { + while ( await canvas().evaluate( () => window.scrollY === 0 ) ) { await page.keyboard.press( 'Enter' ); } @@ -51,7 +45,7 @@ describe( 'TypeWriter', () => { // Type until the text wraps. while ( - await page.evaluate( + await canvas().evaluate( () => document.activeElement.clientHeight <= parseInt( @@ -91,19 +85,16 @@ describe( 'TypeWriter', () => { await page.keyboard.press( 'Enter' ); // Create blocks until there is a scrollable container. - while ( - await page.evaluate( - () => ! wp.dom.getScrollContainer( document.activeElement ) - ) - ) { + while ( await canvas().evaluate( () => window.scrollY === 0 ) ) { await page.keyboard.press( 'Enter' ); } + await page.evaluate( () => { + window.scrollY = 1; + } ); + await page.evaluate( - () => - ( wp.dom.getScrollContainer( - document.activeElement - ).scrollTop = 1 ) + () => new Promise( window.requestAnimationFrame ) ); const initialPosition = await getCaretPosition(); @@ -142,11 +133,7 @@ describe( 'TypeWriter', () => { await page.keyboard.press( 'Enter' ); // Create blocks until there is a scrollable container. - while ( - await page.evaluate( - () => ! wp.dom.getScrollContainer( document.activeElement ) - ) - ) { + while ( await canvas().evaluate( () => window.scrollY === 0 ) ) { await page.keyboard.press( 'Enter' ); } @@ -154,24 +141,16 @@ describe( 'TypeWriter', () => { // Create blocks until the the typewriter effect kicks in, create at // least 10 blocks to properly test the . - while ( - ( await page.evaluate( - () => - wp.dom.getScrollContainer( document.activeElement ) - .scrollTop === 0 - ) ) || - count < 10 - ) { + while ( count < 10 ) { await page.keyboard.press( 'Enter' ); count++; } // Scroll the active element to the very bottom of the scroll container, // then scroll up, so the caret is partially hidden. - await page.evaluate( () => { + await canvas().evaluate( () => { document.activeElement.scrollIntoView( false ); - wp.dom.getScrollContainer( document.activeElement ).scrollTop -= - document.activeElement.offsetHeight + 10; + window.scrollBy( 0, -document.activeElement.offsetHeight + 10 ); } ); const bottomPostition = await getCaretPosition(); @@ -198,10 +177,9 @@ describe( 'TypeWriter', () => { // Scroll the active element to the very top of the scroll container, // then scroll down, so the caret is partially hidden. - await page.evaluate( () => { + await canvas().evaluate( () => { document.activeElement.scrollIntoView(); - wp.dom.getScrollContainer( document.activeElement ).scrollTop += - document.activeElement.offsetHeight + 10; + window.scrollBy( 0, document.activeElement.offsetHeight + 10 ); } ); const topPostition = await getCaretPosition(); diff --git a/packages/e2e-tests/specs/editor/various/undo.test.js b/packages/e2e-tests/specs/editor/various/undo.test.js index 30b3a4c80d494b..b6476e703a8f92 100644 --- a/packages/e2e-tests/specs/editor/various/undo.test.js +++ b/packages/e2e-tests/specs/editor/various/undo.test.js @@ -10,10 +10,11 @@ import { getAllBlocks, saveDraft, publishPost, + canvas, } from '@wordpress/e2e-test-utils'; const getSelection = async () => { - return await page.evaluate( () => { + return await canvas().evaluate( () => { const selectedBlock = document.activeElement.closest( '.wp-block' ); const blocks = Array.from( document.querySelectorAll( '.wp-block' ) ); const blockIndex = blocks.indexOf( selectedBlock ); @@ -174,12 +175,12 @@ describe( 'undo', () => { await saveDraft(); await page.reload(); await page.waitForSelector( '.edit-post-layout' ); - await page.click( '[data-type="core/paragraph"]' ); + await canvas().click( '[data-type="core/paragraph"]' ); await pressKeyWithModifier( 'primary', 'a' ); await pressKeyWithModifier( 'primary', 'b' ); await pressKeyWithModifier( 'primary', 'z' ); - const visibleResult = await page.evaluate( + const visibleResult = await canvas().evaluate( () => document.activeElement.innerHTML ); expect( visibleResult ).toBe( 'test' ); @@ -363,7 +364,7 @@ describe( 'undo', () => { // regression present was accurate, it would produce the correct // content. The issue had manifested in the form of what was shown to // the user since the blocks state failed to sync to block editor. - const visibleContent = await page.evaluate( + const visibleContent = await canvas().evaluate( () => document.activeElement.textContent ); expect( visibleContent ).toBe( 'original' ); @@ -400,7 +401,7 @@ describe( 'undo', () => { await page.$( '.editor-history__undo[aria-disabled="true"]' ) ).not.toBeNull(); - await page.click( '[data-type="core/paragraph"]' ); + await canvas().click( '[data-type="core/paragraph"]' ); await page.keyboard.type( '2' ); diff --git a/packages/e2e-tests/specs/editor/various/writing-flow.test.js b/packages/e2e-tests/specs/editor/various/writing-flow.test.js index f7d8b031f2911c..ee65d035337800 100644 --- a/packages/e2e-tests/specs/editor/various/writing-flow.test.js +++ b/packages/e2e-tests/specs/editor/various/writing-flow.test.js @@ -10,6 +10,7 @@ import { insertBlock, clickBlockToolbarButton, clickButton, + canvas, } from '@wordpress/e2e-test-utils'; const getActiveBlockName = async () => @@ -27,8 +28,8 @@ const addParagraphsAndColumnsDemo = async () => { `//*[contains(@class, "components-autocomplete__result") and contains(@class, "is-selected") and contains(text(), 'Columns')]` ); await page.keyboard.press( 'Enter' ); - await page.click( ':focus [aria-label="Two columns; equal split"]' ); - await page.click( ':focus .block-editor-button-block-appender' ); + await canvas().click( ':focus [aria-label="Two columns; equal split"]' ); + await canvas().click( ':focus .block-editor-button-block-appender' ); await page.waitForSelector( ':focus.block-editor-inserter__search-input' ); await page.keyboard.type( 'Paragraph' ); await pressKeyTimes( 'Tab', 2 ); // Tab to paragraph result. @@ -38,8 +39,8 @@ const addParagraphsAndColumnsDemo = async () => { // TODO: ArrowDown should traverse into the second column. In slower // CPUs, it can sometimes remain in the first column paragraph. This // is a temporary solution. - await page.focus( '.wp-block[data-type="core/column"]:nth-child(2)' ); - await page.click( ':focus .block-editor-button-block-appender' ); + await canvas().focus( '.wp-block[data-type="core/column"]:nth-child(2)' ); + await canvas().click( ':focus .block-editor-button-block-appender' ); await page.waitForSelector( ':focus.block-editor-inserter__search-input' ); await page.keyboard.type( 'Paragraph' ); await pressKeyTimes( 'Tab', 2 ); // Tab to paragraph result. @@ -75,7 +76,7 @@ describe( 'Writing Flow', () => { await page.keyboard.press( 'ArrowUp' ); activeBlockName = await getActiveBlockName(); expect( activeBlockName ).toBe( 'core/paragraph' ); - activeElementText = await page.evaluate( + activeElementText = await canvas().evaluate( () => document.activeElement.textContent ); expect( activeElementText ).toBe( '2nd col' ); @@ -86,7 +87,7 @@ describe( 'Writing Flow', () => { activeBlockName = await getActiveBlockName(); expect( activeBlockName ).toBe( 'core/column' ); await page.keyboard.press( 'ArrowUp' ); - const activeElementBlockType = await page.evaluate( () => + const activeElementBlockType = await canvas().evaluate( () => document.activeElement.getAttribute( 'data-type' ) ); expect( activeElementBlockType ).toBe( 'core/columns' ); @@ -98,7 +99,7 @@ describe( 'Writing Flow', () => { await page.keyboard.press( 'ArrowUp' ); activeBlockName = await getActiveBlockName(); expect( activeBlockName ).toBe( 'core/paragraph' ); - activeElementText = await page.evaluate( + activeElementText = await canvas().evaluate( () => document.activeElement.textContent ); expect( activeElementText ).toBe( 'First paragraph' ); @@ -290,25 +291,25 @@ describe( 'Writing Flow', () => { // See: https://github.com/WordPress/gutenberg/issues/9626 // Title is within the editor's writing flow, and is a