2023-06-01 11:51:59 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-05-27 14:54:06 +00:00
|
|
|
import { Editor as CoreEditor } from '@wordpress/e2e-test-utils-playwright';
|
2024-03-27 09:54:44 +00:00
|
|
|
|
2024-05-27 14:54:06 +00:00
|
|
|
export class Editor extends CoreEditor {
|
2023-06-01 11:51:59 +00:00
|
|
|
async getBlockByName( name: string ) {
|
2024-05-27 14:54:06 +00:00
|
|
|
const blockSelector = `[data-type="${ name }"]`;
|
|
|
|
const canvasLocator = this.page
|
|
|
|
.locator( '.editor-styles-wrapper, iframe[name=editor-canvas]' )
|
|
|
|
.first();
|
2024-01-29 09:11:43 +00:00
|
|
|
|
2024-05-27 14:54:06 +00:00
|
|
|
const isFramed = await canvasLocator.evaluate(
|
|
|
|
( node ) => node.tagName === 'IFRAME'
|
2024-05-02 08:36:51 +00:00
|
|
|
);
|
|
|
|
|
2024-05-27 14:54:06 +00:00
|
|
|
if ( isFramed ) {
|
|
|
|
return this.canvas.locator( blockSelector );
|
2023-08-28 10:04:28 +00:00
|
|
|
}
|
2024-05-27 14:54:06 +00:00
|
|
|
|
|
|
|
return this.page.locator( blockSelector );
|
2023-08-16 09:54:09 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 10:01:38 +00:00
|
|
|
async getBlockRootClientId( clientId: string ) {
|
|
|
|
return this.page.evaluate< string | null, string >( ( id ) => {
|
|
|
|
return window.wp.data
|
|
|
|
.select( 'core/block-editor' )
|
|
|
|
.getBlockRootClientId( id );
|
|
|
|
}, clientId );
|
|
|
|
}
|
2023-06-29 13:41:22 +00:00
|
|
|
|
2023-08-07 15:59:06 +00:00
|
|
|
/**
|
2024-05-27 14:54:06 +00:00
|
|
|
* Opens the global inserter.
|
2023-08-07 15:59:06 +00:00
|
|
|
*/
|
2024-05-27 14:54:06 +00:00
|
|
|
async openGlobalBlockInserter() {
|
|
|
|
const toggleButton = this.page.getByRole( 'button', {
|
2024-03-27 09:54:44 +00:00
|
|
|
name: 'Toggle block inserter',
|
2023-08-07 15:59:06 +00:00
|
|
|
} );
|
2024-05-27 14:54:06 +00:00
|
|
|
const isOpen =
|
|
|
|
( await toggleButton.getAttribute( 'aria-pressed' ) ) === 'true';
|
2024-03-27 09:54:44 +00:00
|
|
|
|
2024-05-27 14:54:06 +00:00
|
|
|
if ( ! isOpen ) {
|
|
|
|
await toggleButton.click();
|
2024-05-14 08:23:17 +00:00
|
|
|
await this.page.locator( '.block-editor-inserter__menu' ).waitFor();
|
2023-08-07 15:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 13:41:22 +00:00
|
|
|
async enterEditMode() {
|
2024-05-27 14:54:06 +00:00
|
|
|
await this.page
|
2023-08-10 23:28:38 +00:00
|
|
|
.getByRole( 'button', {
|
|
|
|
name: 'Edit',
|
|
|
|
exact: true,
|
|
|
|
} )
|
2024-03-27 09:54:44 +00:00
|
|
|
.dispatchEvent( 'click' );
|
|
|
|
|
2024-05-16 12:49:32 +00:00
|
|
|
const sidebar = this.page.locator( '.edit-site-layout__sidebar' );
|
|
|
|
const canvasLoader = this.page.locator( '.edit-site-canvas-loader' );
|
|
|
|
|
|
|
|
await sidebar.waitFor( {
|
|
|
|
state: 'hidden',
|
|
|
|
} );
|
|
|
|
|
|
|
|
await canvasLoader.waitFor( {
|
2024-03-27 09:54:44 +00:00
|
|
|
state: 'hidden',
|
|
|
|
} );
|
2023-06-29 13:41:22 +00:00
|
|
|
}
|
2023-08-04 11:07:31 +00:00
|
|
|
|
2023-09-20 12:56:00 +00:00
|
|
|
async transformIntoBlocks() {
|
2023-11-14 10:32:19 +00:00
|
|
|
// Select the block, so the button is visible.
|
2024-05-27 14:54:06 +00:00
|
|
|
const block = this.canvas
|
2023-11-14 10:32:19 +00:00
|
|
|
.locator( `[data-type="woocommerce/legacy-template"]` )
|
|
|
|
.first();
|
2023-09-20 12:56:00 +00:00
|
|
|
|
2023-11-14 10:32:19 +00:00
|
|
|
if ( ! ( await block.isVisible() ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-27 14:54:06 +00:00
|
|
|
await this.selectBlocks( block );
|
2023-11-14 10:32:19 +00:00
|
|
|
|
|
|
|
const transformButton = block.getByRole( 'button', {
|
|
|
|
name: 'Transform into blocks',
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( transformButton ) {
|
|
|
|
await transformButton.click();
|
2023-09-20 12:56:00 +00:00
|
|
|
|
|
|
|
// save changes
|
|
|
|
await this.saveSiteEditorEntities();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-22 08:33:18 +00:00
|
|
|
async publishAndVisitPost() {
|
2024-05-27 14:54:06 +00:00
|
|
|
const postId = await this.publishPost();
|
|
|
|
await this.page.goto( `/?p=${ postId }` );
|
2023-12-28 10:33:53 +00:00
|
|
|
}
|
2024-01-10 11:17:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlike the `insertBlock` method, which manipulates the block tree
|
|
|
|
* directly, this method simulates real user behavior when inserting a
|
|
|
|
* block to the editor by searching for block name then clicking on the
|
|
|
|
* first matching result.
|
|
|
|
*
|
|
|
|
* Besides, some blocks that manipulate their attributes after insertion
|
|
|
|
* aren't work probably with `insertBlock` as that method requires
|
|
|
|
* attributes object and uses that data to creat the block object.
|
|
|
|
*/
|
|
|
|
async insertBlockUsingGlobalInserter( blockTitle: string ) {
|
|
|
|
await this.openGlobalBlockInserter();
|
|
|
|
await this.page.getByPlaceholder( 'Search' ).fill( blockTitle );
|
2024-01-11 11:27:33 +00:00
|
|
|
await this.page
|
|
|
|
.getByRole( 'option', { name: blockTitle, exact: true } )
|
|
|
|
.first()
|
|
|
|
.click();
|
2024-01-10 11:17:00 +00:00
|
|
|
}
|
2023-06-01 11:51:59 +00:00
|
|
|
}
|