2023-06-30 07:08:14 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-08-16 05:23:43 +00:00
|
|
|
import { Page, Locator } from '@playwright/test';
|
2023-08-15 10:42:29 +00:00
|
|
|
import { RequestUtils } from '@wordpress/e2e-test-utils-playwright';
|
2023-06-30 07:08:14 +00:00
|
|
|
|
|
|
|
export class FrontendUtils {
|
|
|
|
page: Page;
|
2023-08-15 10:42:29 +00:00
|
|
|
requestUtils: RequestUtils;
|
|
|
|
|
|
|
|
constructor( page: Page, requestUtils: RequestUtils ) {
|
2023-06-30 07:08:14 +00:00
|
|
|
this.page = page;
|
2023-08-15 10:42:29 +00:00
|
|
|
this.requestUtils = requestUtils;
|
2023-06-30 07:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-09 14:17:05 +00:00
|
|
|
async addToCart( itemName = '' ) {
|
|
|
|
if ( itemName !== '' ) {
|
|
|
|
await this.page
|
|
|
|
.getByLabel( `Add “${ itemName }” to your cart` )
|
|
|
|
.click();
|
2023-08-15 10:42:29 +00:00
|
|
|
await this.page.waitForResponse( /add_to_cart|batch/ );
|
2023-08-09 14:17:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-06-30 07:08:14 +00:00
|
|
|
await this.page.click( 'text=Add to cart' );
|
|
|
|
}
|
|
|
|
|
2023-08-15 10:42:29 +00:00
|
|
|
async goToCheckout() {
|
|
|
|
await this.page.goto( '/checkout', {
|
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
|
|
|
|
2023-08-15 10:42:29 +00:00
|
|
|
async goToShop() {
|
|
|
|
await this.page.goto( '/shop', {
|
2023-08-10 14:02:33 +00:00
|
|
|
waitUntil: 'commit',
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2023-08-15 10:42:29 +00:00
|
|
|
async emptyCart() {
|
|
|
|
const cartResponse = await this.requestUtils.request.get(
|
|
|
|
'/wp-json/wc/store/cart'
|
|
|
|
);
|
|
|
|
const nonce = cartResponse.headers()?.nonce;
|
|
|
|
if ( ! nonce ) {
|
|
|
|
throw new Error( 'Could not get cart nonce.' );
|
|
|
|
}
|
|
|
|
const res = await this.requestUtils.request.delete(
|
|
|
|
'/wp-json/wc/store/v1/cart/items',
|
|
|
|
{ headers: { nonce } }
|
|
|
|
);
|
|
|
|
if ( ! res.ok() ) {
|
|
|
|
throw new Error(
|
|
|
|
`Got an error response when trying to empty cart. Status code: ${ res.status() }`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-16 05:23:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Playwright selectText causes flaky tests when running on local
|
|
|
|
* development machine. This method is more reliable on both environments.
|
|
|
|
*/
|
|
|
|
async selectTextInput( locator: Locator ) {
|
|
|
|
await locator.click();
|
|
|
|
await locator.press( 'End' );
|
|
|
|
await locator.press( 'Shift+Home' );
|
|
|
|
}
|
2023-06-30 07:08:14 +00:00
|
|
|
}
|