|
| 1 | +const { test, expect } = require( '@playwright/test' ); |
| 2 | + |
| 3 | +const addProductToCart = async ( page, productSlug = 'beanie' ) => { |
| 4 | + // Add a product to the cart |
| 5 | + await page.goto( `/product/${ productSlug }` ); |
| 6 | + |
| 7 | + const addToCartButton = await page.getByRole( 'button', { |
| 8 | + name: 'Add to cart', |
| 9 | + } ); |
| 10 | + await expect( addToCartButton ).toBeEnabled(); |
| 11 | + await addToCartButton.dispatchEvent( 'click' ); |
| 12 | + |
| 13 | + // Wait for the cart update to complete - look for success message or cart count update |
| 14 | + await expect( |
| 15 | + page.getByText( 'has been added to your cart' ) |
| 16 | + ).toBeVisible(); |
| 17 | +}; |
| 18 | + |
| 19 | +const testLink = async ( page, navigateTo, isBlockPage = false ) => { |
| 20 | + await page.goto( navigateTo ); |
| 21 | + |
| 22 | + let frameLocator; |
| 23 | + if ( isBlockPage ) { |
| 24 | + frameLocator = await page.frameLocator( |
| 25 | + '#express-payment-method-express_checkout_element_link iframe[name^="__privateStripeFrame"]' |
| 26 | + ); |
| 27 | + } else { |
| 28 | + frameLocator = await page.frameLocator( |
| 29 | + '#wc-stripe-express-checkout-element-link iframe[name^="__privateStripeFrame"]' |
| 30 | + ); |
| 31 | + } |
| 32 | + const linkButton = await frameLocator.getByRole( 'button', { |
| 33 | + name: 'Pay with Link', |
| 34 | + } ); |
| 35 | + await expect( linkButton ).toBeEnabled(); |
| 36 | + |
| 37 | + const context = await page.context(); |
| 38 | + const [ popup ] = await Promise.all( [ |
| 39 | + context.waitForEvent( 'page' ), |
| 40 | + linkButton.dispatchEvent( 'click' ), |
| 41 | + ] ); |
| 42 | + |
| 43 | + // Check that the payment modal gets loaded. |
| 44 | + await popup.waitForLoadState(); |
| 45 | + |
| 46 | + // Back in the main window, check that Link's "Continue payment" button is visible. |
| 47 | + const continuePaymentButton = await page.getByRole( 'button', { |
| 48 | + name: 'Continue payment', |
| 49 | + } ); |
| 50 | + await expect( continuePaymentButton ).toBeVisible(); |
| 51 | +}; |
| 52 | + |
| 53 | +test.describe( 'customer can use Link express checkout', () => { |
| 54 | + test( 'inside the product page', async ( { page } ) => |
| 55 | + await testLink( page, '/product/beanie' ) ); |
| 56 | + |
| 57 | + test( 'inside the cart page (classic)', async ( { page } ) => { |
| 58 | + await addProductToCart( page ); |
| 59 | + await testLink( page, '/cart-shortcode', false ); |
| 60 | + } ); |
| 61 | + |
| 62 | + test( 'inside the checkout page (classic)', async ( { page } ) => { |
| 63 | + await addProductToCart( page ); |
| 64 | + await testLink( page, '/checkout-shortcode', false ); |
| 65 | + } ); |
| 66 | + |
| 67 | + test( 'inside the cart page (block)', async ( { page } ) => { |
| 68 | + await addProductToCart( page ); |
| 69 | + await testLink( page, '/cart', true ); |
| 70 | + } ); |
| 71 | + |
| 72 | + test( 'inside the checkout page (block)', async ( { page } ) => { |
| 73 | + await addProductToCart( page ); |
| 74 | + await testLink( page, '/checkout', true ); |
| 75 | + } ); |
| 76 | +} ); |
0 commit comments