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
|
|
|
}
|
|
|
|
|
2024-03-26 08:38:06 +00:00
|
|
|
async getBlockByName( name: string, parentSelector?: string ) {
|
|
|
|
let selector = `[data-block-name="${ name }"]`;
|
|
|
|
if ( parentSelector ) {
|
|
|
|
selector = `${ parentSelector } [data-block-name="${ name }"]`;
|
|
|
|
}
|
|
|
|
return this.page.locator( selector );
|
2023-06-30 07:08:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 14:17:05 +00:00
|
|
|
async addToCart( itemName = '' ) {
|
2023-08-28 10:04:28 +00:00
|
|
|
await this.page.waitForLoadState( 'domcontentloaded' );
|
2023-08-09 14:17:05 +00:00
|
|
|
if ( itemName !== '' ) {
|
2024-01-25 13:55:17 +00:00
|
|
|
// We can't use `getByRole()` here because the Add to Cart button
|
|
|
|
// might be a button (in blocks) or a link (in the legacy template).
|
2023-08-09 14:17:05 +00:00
|
|
|
await this.page
|
2024-01-25 13:55:17 +00:00
|
|
|
.getByLabel( `Add to cart: “${ itemName }”` )
|
2023-08-09 14:17:05 +00:00
|
|
|
.click();
|
2023-09-19 09:58:18 +00:00
|
|
|
} else {
|
|
|
|
await this.page.click( 'text=Add to cart' );
|
2023-08-09 14:17:05 +00:00
|
|
|
}
|
2023-09-19 09:58:18 +00:00
|
|
|
|
|
|
|
await this.page.waitForResponse( ( request ) => {
|
|
|
|
const url = request.url();
|
|
|
|
return url.includes( 'add_to_cart' ) || url.includes( 'batch' );
|
|
|
|
} );
|
2023-06-30 07:08:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 10:42:29 +00:00
|
|
|
async goToCheckout() {
|
|
|
|
await this.page.goto( '/checkout', {
|
2023-11-28 14:00:40 +00:00
|
|
|
waitUntil: 'domcontentloaded',
|
2023-06-30 07:08:14 +00:00
|
|
|
} );
|
2024-03-14 12:39:30 +00:00
|
|
|
|
2024-05-14 08:23:17 +00:00
|
|
|
await this.page.locator( '#email' ).waitFor();
|
2023-06-30 07:08:14 +00:00
|
|
|
}
|
2023-08-04 11:07:31 +00:00
|
|
|
|
2023-10-27 20:54:13 +00:00
|
|
|
async goToCart() {
|
|
|
|
await this.page.goto( '/cart', {
|
|
|
|
waitUntil: 'commit',
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2024-02-27 12:46:31 +00:00
|
|
|
async goToCartShortcode() {
|
|
|
|
await this.page.goto( '/cart-shortcode', {
|
|
|
|
waitUntil: 'commit',
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2024-01-16 06:58:54 +00:00
|
|
|
async goToMiniCart() {
|
|
|
|
await this.page.goto( '/mini-cart', {
|
|
|
|
waitUntil: 'domcontentloaded',
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
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-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-12-08 16:44:59 +00:00
|
|
|
|
|
|
|
async gotoMyAccount() {
|
|
|
|
await this.page.goto( '/my-account', {
|
|
|
|
waitUntil: 'commit',
|
|
|
|
} );
|
|
|
|
}
|
2023-06-30 07:08:14 +00:00
|
|
|
}
|