2023-06-30 07:08:14 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { Page } from '@playwright/test';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
export class FrontendUtils {
|
|
|
|
page: Page;
|
|
|
|
constructor( page: Page ) {
|
|
|
|
this.page = page;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getBlockByName( name: string ) {
|
|
|
|
return this.page.locator( `[data-block-name="${ name }"]` );
|
|
|
|
}
|
|
|
|
|
2023-08-04 11:07:31 +00:00
|
|
|
async getBlockByClassWithParent( blockClass: string, parentName: string ) {
|
|
|
|
const parentBlock = await this.getBlockByName( parentName );
|
|
|
|
if ( ! parentBlock ) {
|
|
|
|
throw new Error( `Parent block "${ parentName }" not found.` );
|
|
|
|
}
|
2023-08-08 13:25:45 +00:00
|
|
|
const block = parentBlock.locator( `.${ blockClass }` );
|
2023-08-04 11:07:31 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2023-06-30 07:08:14 +00:00
|
|
|
async addToCart() {
|
|
|
|
await this.page.click( 'text=Add to cart' );
|
|
|
|
}
|
|
|
|
|
|
|
|
async goToShop() {
|
|
|
|
await this.page.goto( '/shop', {
|
2023-08-08 13:25:45 +00:00
|
|
|
waitUntil: 'commit',
|
2023-06-30 07:08:14 +00:00
|
|
|
} );
|
|
|
|
}
|
2023-08-04 11:07:31 +00:00
|
|
|
|
|
|
|
async isBlockEarlierThan< T >(
|
|
|
|
containerBlock: T,
|
|
|
|
firstBlock: string,
|
|
|
|
secondBlock: string
|
|
|
|
) {
|
|
|
|
const container =
|
|
|
|
containerBlock instanceof Function
|
|
|
|
? await containerBlock()
|
|
|
|
: containerBlock;
|
|
|
|
|
|
|
|
if ( ! container ) {
|
|
|
|
throw new Error( 'Container block not found.' );
|
|
|
|
}
|
|
|
|
|
|
|
|
const childBlocks = container.locator( '[data-block-name]' );
|
|
|
|
|
|
|
|
let firstBlockIndex = -1;
|
|
|
|
let secondBlockIndex = -1;
|
|
|
|
|
|
|
|
for ( let i = 0; i < ( await childBlocks.count() ); i++ ) {
|
|
|
|
const blockName = await childBlocks
|
|
|
|
.nth( i )
|
|
|
|
.getAttribute( 'data-block-name' );
|
|
|
|
|
|
|
|
if ( blockName === firstBlock ) {
|
|
|
|
firstBlockIndex = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( blockName === secondBlock ) {
|
|
|
|
secondBlockIndex = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( firstBlockIndex !== -1 && secondBlockIndex !== -1 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( firstBlockIndex === -1 || secondBlockIndex === -1 ) {
|
|
|
|
throw new Error( 'Both blocks must exist within the editor' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstBlockIndex < secondBlockIndex;
|
|
|
|
}
|
2023-06-30 07:08:14 +00:00
|
|
|
}
|