woocommerce/tests/e2e-tests/utils/flows.js

176 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { clickTab } from "./index";
/**
* @format
*/
const baseUrl = process.env.WP_BASE_URL;
const WP_ADMIN_NEW_PRODUCT = baseUrl + '/wp-admin/post-new.php?post_type=product';
const WP_ADMIN_WC_SETTINGS = baseUrl + '/wp-admin/admin.php?page=wc-settings&tab=';
const WP_ADMIN_NEW_COUPON = baseUrl + '/wp-admin/post-new.php?post_type=shop_coupon';
const WP_ADMIN_NEW_ORDER = baseUrl + '/wp-admin/post-new.php?post_type=shop_order';
const SHOP_PAGE = baseUrl + '/shop/';
const SHOP_PRODUCT = baseUrl + '/?p=';
const SHOP_CART_PAGE = baseUrl + '/cart/';
const SHOP_CHECKOUT_PAGE = baseUrl + '/checkout/';
const getProductColumnExpression = ( productTitle ) => (
'td[@class="product-name" and ' +
`a[contains(text(), "${ productTitle }")]` +
']'
);
const getQtyColumnExpression = ( args ) => (
'td[@class="product-quantity" and ' +
'.//' + getQtyInputExpression( args ) +
']'
);
const getQtyInputExpression = ( args = {} ) => {
let qtyValue = '';
if ( args.checkQty ) {
qtyValue = ` and @value="${ args.qty }"`;
}
return 'input[contains(@class, "input-text")' + qtyValue + ']';
};
const getCartItemExpression = ( productTitle, args ) => (
'//tr[contains(@class, "cart_item") and ' +
getProductColumnExpression( productTitle ) +
' and ' +
getQtyColumnExpression( args ) +
']'
);
const getRemoveExpression = () => (
'td[@class="product-remove"]//a[@class="remove"]'
);
const CustomerFlow = {
addToCart: async () => {
await Promise.all( [
page.waitForNavigation( { waitUntil: 'networkidle0' } ),
page.click( '.single_add_to_cart_button' ),
] );
},
addToCartFromShopPage: async ( productTitle ) => {
const addToCartXPath = `//li[contains(@class, "type-product") and a/h2[contains(text(), "${ productTitle }")]]` +
'//a[contains(@class, "add_to_cart_button") and contains(@class, "ajax_add_to_cart")';
const [ addToCartButton ] = await page.$x( addToCartXPath + ']' );
addToCartButton.click();
await page.waitFor( addToCartXPath + ' and contains(@class, "added")]' );
},
goToCheckout: async () => {
await page.goto( SHOP_CHECKOUT_PAGE, {
waitUntil: 'networkidle0',
} );
},
removeFromCart: async ( productTitle ) => {
const cartItemXPath = getCartItemExpression( productTitle );
const removeItemXPath = cartItemXPath + '//' + getRemoveExpression();
const [ removeButton ] = await page.$x( removeItemXPath );
await removeButton.click();
},
goToProduct: async ( postID ) => {
await page.goto( SHOP_PRODUCT + postID, {
waitUntil: 'networkidle0',
} );
},
goToShop: async () => {
await page.goto(SHOP_PAGE, {
waitUntil: 'networkidle0',
});
},
placeOrder: async () => {
await Promise.all( [
expect( page ).toClick( '#place_order' ),
page.waitForNavigation( { waitUntil: 'networkidle0' } ),
] );
},
productIsInCheckout: async ( productTitle, quantity, total ) => {
const checkoutItemXPath =
'//tr[@class="cart_item" and ' +
`.//td[contains(., "${ productTitle }") and contains(., "× ${ quantity }")] and ` +
`.//td[contains(., "${ total }")]` +
']';
await expect( page.$x( checkoutItemXPath ) ).resolves.toHaveLength( 1 );
},
goToCart: async () => {
await page.goto( SHOP_CART_PAGE, {
waitUntil: 'networkidle0',
} );
},
productIsInCart: async ( productTitle, quantity = null ) => {
const cartItemArgs = quantity ? { qty: quantity } : {};
const cartItemXPath = getCartItemExpression( productTitle, cartItemArgs );
await expect( page.$x( cartItemXPath ) ).resolves.toHaveLength( 1 );
},
};
const StoreOwnerFlow = {
logout: async () => {
await page.goto(baseUrl + '/wp-login.php?action=logout', {
waitUntil: 'networkidle0',
});
await expect(page).toMatch('You are attempting to log out');
await Promise.all([
page.waitForNavigation({ waitUntil: 'networkidle0' }),
page.click('a'),
]);
},
openNewCoupon: async () => {
await page.goto( WP_ADMIN_NEW_COUPON, {
waitUntil: 'networkidle0',
} );
},
openNewOrder: async () => {
await page.goto( WP_ADMIN_NEW_ORDER, {
waitUntil: 'networkidle0',
} );
},
openNewProduct: async () => {
await page.goto( WP_ADMIN_NEW_PRODUCT, {
waitUntil: 'networkidle0',
} );
},
openSettings: async ( tab, section = null ) => {
let settingsUrl = WP_ADMIN_WC_SETTINGS + tab;
if ( section ) {
settingsUrl += `&section=${ section }`;
}
await page.goto( settingsUrl, {
waitUntil: 'networkidle0',
} );
},
};
export { CustomerFlow, StoreOwnerFlow };