2019-08-27 12:22:24 +00:00
|
|
|
/**
|
|
|
|
* @format
|
|
|
|
*/
|
|
|
|
|
2019-09-24 11:56:02 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { pressKeyWithModifier } from '@wordpress/e2e-test-utils';
|
|
|
|
|
2019-08-27 12:22:24 +00:00
|
|
|
const baseUrl = process.env.WP_BASE_URL;
|
|
|
|
|
|
|
|
const WP_ADMIN_NEW_PRODUCT = baseUrl + '/wp-admin/post-new.php?post_type=product';
|
|
|
|
|
2019-09-24 11:56:02 +00:00
|
|
|
const SHOP_PAGE = baseUrl + '/shop/';
|
|
|
|
const SHOP_CART_PAGE = baseUrl + '/cart/';
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
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")]' );
|
|
|
|
},
|
|
|
|
|
|
|
|
removeFromCart: async ( productTitle ) => {
|
|
|
|
const cartItemXPath = getCartItemExpression( productTitle );
|
|
|
|
const removeItemXPath = cartItemXPath + '//' + getRemoveExpression();
|
|
|
|
|
|
|
|
const [ removeButton ] = await page.$x( removeItemXPath );
|
|
|
|
await removeButton.click();
|
|
|
|
},
|
|
|
|
|
|
|
|
goToCart: async () => {
|
|
|
|
await page.goto( SHOP_CART_PAGE, {
|
|
|
|
waitUntil: 'networkidle0',
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
goToShop: async () => {
|
|
|
|
await page.goto( SHOP_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 );
|
|
|
|
},
|
|
|
|
|
|
|
|
setCartQuantity: async ( productTitle, quantityValue ) => {
|
|
|
|
const cartItemXPath = getCartItemExpression( productTitle );
|
|
|
|
const quantityInputXPath = cartItemXPath + '//' + getQtyInputExpression();
|
|
|
|
|
|
|
|
const [ quantityInput ] = await page.$x( quantityInputXPath );
|
|
|
|
await quantityInput.focus();
|
|
|
|
await pressKeyWithModifier( 'primary', 'a' );
|
|
|
|
await quantityInput.type( quantityValue.toString() );
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-08-27 12:22:24 +00:00
|
|
|
const StoreOwnerFlow = {
|
2019-09-24 11:56:02 +00:00
|
|
|
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' ),
|
|
|
|
] );
|
|
|
|
},
|
|
|
|
|
2019-08-27 12:22:24 +00:00
|
|
|
openNewProduct: async () => {
|
|
|
|
await page.goto( WP_ADMIN_NEW_PRODUCT, {
|
|
|
|
waitUntil: 'networkidle0',
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-09-24 11:56:02 +00:00
|
|
|
export { CustomerFlow, StoreOwnerFlow };
|