diff --git a/plugins/woocommerce/changelog/e2e-fix-cleanup-for-created-pages b/plugins/woocommerce/changelog/e2e-fix-cleanup-for-created-pages new file mode 100644 index 0000000000000..5baab77e106fc --- /dev/null +++ b/plugins/woocommerce/changelog/e2e-fix-cleanup-for-created-pages @@ -0,0 +1,4 @@ +Significance: patch +Type: dev + +E2E tests: fix cleanup of created test pages and migrate to using fixtures diff --git a/plugins/woocommerce/tests/e2e-pw/fixtures/fixtures.js b/plugins/woocommerce/tests/e2e-pw/fixtures/fixtures.js index 5d8448b03f819..a0af6568d0389 100644 --- a/plugins/woocommerce/tests/e2e-pw/fixtures/fixtures.js +++ b/plugins/woocommerce/tests/e2e-pw/fixtures/fixtures.js @@ -1,6 +1,7 @@ const base = require( '@playwright/test' ); const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; const { admin } = require( '../test-data/data' ); +const { random } = require( '../utils/helpers' ); exports.test = base.test.extend( { api: async ( { baseURL }, use ) => { @@ -43,6 +44,35 @@ exports.test = base.test.extend( { await use( wpApi ); }, + + testPageTitlePrefix: [ '', { option: true } ], + + testPage: async ( { wpApi, testPageTitlePrefix }, use ) => { + const pageTitle = `${ testPageTitlePrefix } Page ${ random() }`; + const pageSlug = pageTitle.replace( / /gi, '-' ).toLowerCase(); + + await use( { title: pageTitle, slug: pageSlug } ); + + // Cleanup + const pages = await wpApi.get( + `/wp-json/wp/v2/pages?slug=${ pageSlug }`, + { + data: { + _fields: [ 'id' ], + }, + failOnStatusCode: false, + } + ); + + for ( const page of await pages.json() ) { + console.log( `Deleting page ${ page.id }` ); + await wpApi.delete( `/wp-json/wp/v2/pages/${ page.id }`, { + data: { + force: true, + }, + } ); + } + }, } ); exports.expect = base.expect; diff --git a/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-cart-block.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-cart-block.spec.js index 388daaa1862db..a30e9d5aafbc8 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-cart-block.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-cart-block.spec.js @@ -1,43 +1,33 @@ -const { test, expect } = require( '@playwright/test' ); +const { test: baseTest, expect } = require( '../../fixtures/fixtures' ); const { goToPageEditor, fillPageTitle, insertBlock, transformIntoBlocks, + publishPage, } = require( '../../utils/editor' ); -const uuid = require( 'uuid' ); -const transformedCartBlockTitle = `Transformed Cart ${ uuid.v1() }`; -const transformedCartBlockSlug = transformedCartBlockTitle - .replace( / /gi, '-' ) - .toLowerCase(); - -test.describe( 'Transform Classic Cart To Cart Block', () => { - test.use( { storageState: process.env.ADMINSTATE } ); +baseTest.describe( 'Transform Classic Cart To Cart Block', () => { + const test = baseTest.extend( { + storageState: process.env.ADMINSTATE, + testPageTitlePrefix: 'Transformed cart', + } ); - test( 'can transform classic cart to cart block', async ( { page } ) => { + test( 'can transform classic cart to cart block', async ( { + page, + testPage, + } ) => { await goToPageEditor( { page } ); - await fillPageTitle( page, transformedCartBlockTitle ); + await fillPageTitle( page, testPage.title ); await insertBlock( page, 'Classic Cart' ); await transformIntoBlocks( page ); - - // save and publish the page - await page - .getByRole( 'button', { name: 'Publish', exact: true } ) - .click(); - await page - .getByRole( 'region', { name: 'Editor publish' } ) - .getByRole( 'button', { name: 'Publish', exact: true } ) - .click(); - await expect( - page.getByText( `${ transformedCartBlockTitle } is now live.` ) - ).toBeVisible(); + await publishPage( page, testPage.title ); // go to frontend to verify transformed cart block - await page.goto( transformedCartBlockSlug ); + await page.goto( testPage.slug ); await expect( - page.getByRole( 'heading', { name: transformedCartBlockTitle } ) + page.getByRole( 'heading', { name: testPage.title } ) ).toBeVisible(); await expect( page.getByRole( 'heading', { diff --git a/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-checkout-block.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-checkout-block.spec.js index e774da780f428..1bf283240830a 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-checkout-block.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-checkout-block.spec.js @@ -1,34 +1,25 @@ -const { test, expect } = require( '@playwright/test' ); +const { test: baseTest, expect } = require( '../../fixtures/fixtures' ); const { goToPageEditor, getCanvas, fillPageTitle, insertBlock, transformIntoBlocks, + publishPage, } = require( '../../utils/editor' ); -const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; -const uuid = require( 'uuid' ); - -const transformedCheckoutBlockTitle = `Transformed Checkout ${ uuid.v1() }`; -const transformedCheckoutBlockSlug = transformedCheckoutBlockTitle - .replace( / /gi, '-' ) - .toLowerCase(); const simpleProductName = 'Very Simple Product'; const singleProductPrice = '999.00'; let productId, shippingZoneId; -test.describe( 'Transform Classic Checkout To Checkout Block', () => { - test.use( { storageState: process.env.ADMINSTATE } ); +baseTest.describe( 'Transform Classic Checkout To Checkout Block', () => { + const test = baseTest.extend( { + storageState: process.env.ADMINSTATE, + testPageTitlePrefix: 'Transformed checkout', + } ); - test.beforeAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.beforeAll( async ( { api } ) => { // enable COD await api.put( 'payment_gateways/cod', { enabled: true, @@ -56,13 +47,7 @@ test.describe( 'Transform Classic Checkout To Checkout Block', () => { } ); } ); - test.afterAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.afterAll( async ( { api } ) => { await api.delete( `products/${ productId }`, { force: true, } ); @@ -79,18 +64,12 @@ test.describe( 'Transform Classic Checkout To Checkout Block', () => { test( 'can transform classic checkout to checkout block', async ( { page, - baseURL, + api, + testPage, } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); - await goToPageEditor( { page } ); - await fillPageTitle( page, transformedCheckoutBlockTitle ); + await fillPageTitle( page, testPage.title ); await insertBlock( page, 'Classic Checkout' ); await transformIntoBlocks( page ); @@ -101,17 +80,7 @@ test.describe( 'Transform Classic Checkout To Checkout Block', () => { await canvas.getByLabel( 'Block: Terms and Conditions' ).click(); await page.getByLabel( 'Require checkbox' ).check(); - // save and publish the page - await page - .getByRole( 'button', { name: 'Publish', exact: true } ) - .click(); - await page - .getByRole( 'region', { name: 'Editor publish' } ) - .getByRole( 'button', { name: 'Publish', exact: true } ) - .click(); - await expect( - page.getByText( `${ transformedCheckoutBlockTitle } is now live.` ) - ).toBeVisible(); + await publishPage( page, testPage.title ); // add additional payment option after page creation await api.put( 'payment_gateways/bacs', { @@ -152,9 +121,9 @@ test.describe( 'Transform Classic Checkout To Checkout Block', () => { // go to frontend to verify transformed checkout block // before that add product to cart to be able to visit checkout page await page.goto( `/cart/?add-to-cart=${ productId }` ); - await page.goto( transformedCheckoutBlockSlug ); + await page.goto( testPage.slug ); await expect( - page.getByRole( 'heading', { name: transformedCheckoutBlockTitle } ) + page.getByRole( 'heading', { name: testPage.title } ) ).toBeVisible(); await expect( page diff --git a/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-page.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-page.spec.js index 24524528ce0a0..d634eca46e2dc 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-page.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-page.spec.js @@ -1,45 +1,21 @@ -const { test, expect, request } = require( '@playwright/test' ); -const { admin } = require( '../../test-data/data' ); +const { test: baseTest } = require( '../../fixtures/fixtures' ); const { goToPageEditor, fillPageTitle, getCanvas, + publishPage, } = require( '../../utils/editor' ); -const pageTitle = `Page-${ new Date().getTime().toString() }`; - -test.describe( 'Can create a new page', () => { - test.use( { storageState: process.env.ADMINSTATE } ); - - test.afterAll( async ( { baseURL } ) => { - const base64auth = Buffer.from( - `${ admin.username }:${ admin.password }` - ).toString( 'base64' ); - const wpApi = await request.newContext( { - baseURL: `${ baseURL }/wp-json/wp/v2/`, - extraHTTPHeaders: { - Authorization: `Basic ${ base64auth }`, - }, - } ); - - let response = await wpApi.get( `pages` ); - const allPages = await response.json(); - - await allPages.forEach( async ( page ) => { - if ( page.title.rendered === pageTitle ) { - response = await wpApi.delete( `pages/${ page.id }`, { - data: { - force: true, - }, - } ); - } - } ); +baseTest.describe( 'Can create a new page', () => { + const test = baseTest.extend( { + storageState: process.env.ADMINSTATE, } ); - test( 'can create new page', async ( { page } ) => { + // eslint-disable-next-line playwright/expect-expect + test( 'can create new page', async ( { page, testPage } ) => { await goToPageEditor( { page } ); - await fillPageTitle( page, pageTitle ); + await fillPageTitle( page, testPage.title ); const canvas = await getCanvas( page ); @@ -53,17 +29,6 @@ test.describe( 'Can create a new page', () => { } ) .fill( 'Test Page' ); - await page - .getByRole( 'button', { name: 'Publish', exact: true } ) - .click(); - - await page - .getByRole( 'region', { name: 'Editor publish' } ) - .getByRole( 'button', { name: 'Publish', exact: true } ) - .click(); - - await expect( - page.getByText( `${ pageTitle } is now live.` ) - ).toBeVisible(); + await publishPage( page, testPage.title ); } ); } ); diff --git a/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-woocommerce-blocks.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-woocommerce-blocks.spec.js index 80ffd6c5b094c..fc8f3a379a377 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-woocommerce-blocks.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-woocommerce-blocks.spec.js @@ -1,4 +1,4 @@ -const { test, expect } = require( '@playwright/test' ); +const { test: baseTest, expect } = require( '../../fixtures/fixtures' ); const { goToPageEditor, fillPageTitle, @@ -6,10 +6,6 @@ const { getCanvas, publishPage, } = require( '../../utils/editor' ); -const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; -const uuid = require( 'uuid' ); - -const allWooBlocksPageTitle = `Insert All Woo Blocks ${ uuid.v1() }`; const simpleProductName = 'Simplest Product'; const singleProductPrice = '555.00'; @@ -93,16 +89,13 @@ const blocks = [ let productId, shippingZoneId, productTagId, attributeId, productCategoryId; -test.describe( 'Insert All WooCommerce Blocks Into Page', () => { - test.use( { storageState: process.env.ADMINSTATE } ); +baseTest.describe( 'Add WooCommerce Blocks Into Page', () => { + const test = baseTest.extend( { + storageState: process.env.ADMINSTATE, + testPageTitlePrefix: 'Woocommerce Blocks', + } ); - test.beforeAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.beforeAll( async ( { api } ) => { // add product attribute await api .post( 'products/attributes', { @@ -172,13 +165,7 @@ test.describe( 'Insert All WooCommerce Blocks Into Page', () => { } ); } ); - test.afterAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.afterAll( async ( { api } ) => { await api.delete( `products/${ productId }`, { force: true, } ); @@ -196,10 +183,13 @@ test.describe( 'Insert All WooCommerce Blocks Into Page', () => { } ); } ); - test( `can insert all WooCommerce blocks into page`, async ( { page } ) => { + test( `can insert all WooCommerce blocks into page`, async ( { + page, + testPage, + } ) => { await goToPageEditor( { page } ); - await fillPageTitle( page, allWooBlocksPageTitle ); + await fillPageTitle( page, testPage.title ); for ( let i = 0; i < blocks.length; i++ ) { await test.step( `Insert ${ blocks[ i ].name } block`, async () => { @@ -227,7 +217,7 @@ test.describe( 'Insert All WooCommerce Blocks Into Page', () => { } ); } - await publishPage( page, allWooBlocksPageTitle ); + await publishPage( page, testPage.title ); // check all blocks inside the page after publishing // except the product price due to invisibility and false-positive diff --git a/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-woocommerce-patterns.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-woocommerce-patterns.spec.js index de1c66fdc6a15..d2b9699c52553 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-woocommerce-patterns.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-woocommerce-patterns.spec.js @@ -1,4 +1,4 @@ -const { test, expect } = require( '@playwright/test' ); +const { test: baseTest, expect } = require( '../../fixtures/fixtures' ); const { goToPageEditor, fillPageTitle, @@ -6,12 +6,6 @@ const { getCanvas, publishPage, } = require( '../../utils/editor' ); -const uuid = require( 'uuid' ); - -const wooPatternsPageTitle = `Insert Woo Patterns ${ uuid.v1() }`; -const wooPatternsPageSlug = wooPatternsPageTitle - .replace( / /gi, '-' ) - .toLowerCase(); // some WooCommerce Patterns to use const wooPatterns = [ @@ -33,12 +27,18 @@ const wooPatterns = [ }, ]; -test.describe( 'Insert WooCommerce Patterns Into Page', () => { - test.use( { storageState: process.env.ADMINSTATE } ); +baseTest.describe( 'Add WooCommerce Patterns Into Page', () => { + const test = baseTest.extend( { + storageState: process.env.ADMINSTATE, + testPageTitlePrefix: 'Woocommerce Patterns', + } ); - test( 'can insert WooCommerce patterns into page', async ( { page } ) => { + test( 'can insert WooCommerce patterns into page', async ( { + page, + testPage, + } ) => { await goToPageEditor( { page } ); - await fillPageTitle( page, wooPatternsPageTitle ); + await fillPageTitle( page, testPage.title ); for ( let i = 0; i < wooPatterns.length; i++ ) { await test.step( `Insert ${ wooPatterns[ i ].name } pattern`, async () => { @@ -59,7 +59,7 @@ test.describe( 'Insert WooCommerce Patterns Into Page', () => { } ); } - await publishPage( page, wooPatternsPageTitle ); + await publishPage( page, testPage.title ); // check again added patterns after publishing const canvas = await getCanvas( page ); @@ -72,9 +72,9 @@ test.describe( 'Insert WooCommerce Patterns Into Page', () => { } // go to the frontend page to verify patterns - await page.goto( wooPatternsPageSlug ); + await page.goto( testPage.slug ); await expect( - page.getByRole( 'heading', { name: wooPatternsPageTitle } ) + page.getByRole( 'heading', { name: testPage.title } ) ).toBeVisible(); // check some elements from added patterns diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-block-calculate-shipping.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-block-calculate-shipping.spec.js index 2aff68231393f..adac3c135853b 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-block-calculate-shipping.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-block-calculate-shipping.spec.js @@ -1,4 +1,4 @@ -const { test, expect } = require( '@playwright/test' ); +const { test: baseTest, expect } = require( '../../fixtures/fixtures' ); const { goToPageEditor, fillPageTitle, @@ -6,8 +6,6 @@ const { publishPage, } = require( '../../utils/editor' ); const { addAProductToCart } = require( '../../utils/cart' ); -const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; -const uuid = require( 'uuid' ); const firstProductName = 'First Product'; const firstProductPrice = '10.00'; @@ -15,28 +13,28 @@ const secondProductName = 'Second Product'; const secondProductPrice = '20.00'; const firstProductWithFlatRate = +firstProductPrice + 5; -const cartBlockPageTitle = `Cart Block ${ uuid.v1() }`; -const cartBlockPageSlug = cartBlockPageTitle - .replace( / /gi, '-' ) - .toLowerCase(); - const shippingZoneNameES = 'Netherlands Free Shipping'; const shippingCountryNL = 'NL'; const shippingZoneNamePT = 'Portugal Flat Local'; const shippingCountryPT = 'PT'; -test.describe( 'Cart Block Calculate Shipping', () => { - test.use( { storageState: process.env.ADMINSTATE } ); - let product1Id, product2Id, shippingZoneNLId, shippingZonePTId; +baseTest.describe( 'Cart Block Calculate Shipping', () => { + const test = baseTest.extend( { + storageState: process.env.ADMINSTATE, + testPageTitlePrefix: 'Cart Block', + cartBlockPage: async ( { page, testPage }, use ) => { + await goToPageEditor( { page } ); + await fillPageTitle( page, testPage.title ); + await insertBlockByShortcut( page, '/cart' ); + await publishPage( page, testPage.title ); + + await use( testPage ); + }, + } ); - test.beforeAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + let product1Id, product2Id, shippingZoneNLId, shippingZonePTId; + test.beforeAll( async ( { api } ) => { // make sure the currency is USD await api.put( 'settings/general/woocommerce_currency', { value: 'USD', @@ -117,13 +115,7 @@ test.describe( 'Cart Block Calculate Shipping', () => { } ); } ); - test.afterAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.afterAll( async ( { api } ) => { await api.post( 'products/batch', { delete: [ product1Id, product2Id ], } ); @@ -135,22 +127,15 @@ test.describe( 'Cart Block Calculate Shipping', () => { } ); } ); - // eslint-disable-next-line playwright/expect-expect,jest/expect-expect - test( 'create Cart Block page', async ( { page } ) => { - await goToPageEditor( { page } ); - await fillPageTitle( page, cartBlockPageTitle ); - await insertBlockByShortcut( page, '/cart' ); - await publishPage( page, cartBlockPageTitle ); - } ); - test( 'allows customer to calculate Free Shipping in cart block if in Netherlands', async ( { page, context, + cartBlockPage, } ) => { await context.clearCookies(); await addAProductToCart( page, product1Id ); - await page.goto( cartBlockPageSlug ); + await page.goto( cartBlockPage.slug ); // Set shipping country to Netherlands await page.getByLabel( 'Add an address for shipping' ).click(); @@ -172,11 +157,12 @@ test.describe( 'Cart Block Calculate Shipping', () => { test( 'allows customer to calculate Flat rate and Local pickup in cart block if in Portugal', async ( { page, context, + cartBlockPage, } ) => { await context.clearCookies(); await addAProductToCart( page, product1Id ); - await page.goto( cartBlockPageSlug ); + await page.goto( cartBlockPage.slug ); // Set shipping country to Portugal await page.getByLabel( 'Add an address for shipping' ).click(); @@ -207,11 +193,12 @@ test.describe( 'Cart Block Calculate Shipping', () => { test( 'should show correct total cart block price after updating quantity', async ( { page, context, + cartBlockPage, } ) => { await context.clearCookies(); await addAProductToCart( page, product1Id ); - await page.goto( cartBlockPageSlug ); + await page.goto( cartBlockPage.slug ); // Set shipping country to Portugal await page.getByLabel( 'Add an address for shipping' ).click(); @@ -236,12 +223,13 @@ test.describe( 'Cart Block Calculate Shipping', () => { test( 'should show correct total cart block price with 2 different products and flat rate/local pickup', async ( { page, context, + cartBlockPage, } ) => { await context.clearCookies(); await addAProductToCart( page, product1Id ); await addAProductToCart( page, product2Id ); - await page.goto( cartBlockPageSlug ); + await page.goto( cartBlockPage.slug ); // Set shipping country to Portugal await page.getByLabel( 'Add an address for shipping' ).click(); diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-block-coupons.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-block-coupons.spec.js index c732290bcbf86..a1ec1228bb31b 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-block-coupons.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-block-coupons.spec.js @@ -1,4 +1,4 @@ -const { test, expect } = require( '@playwright/test' ); +const { test: baseTest, expect } = require( '../../fixtures/fixtures' ); const { goToPageEditor, fillPageTitle, @@ -6,8 +6,6 @@ const { publishPage, } = require( '../../utils/editor' ); const { addAProductToCart } = require( '../../utils/cart' ); -const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; -const uuid = require( 'uuid' ); const simpleProductName = 'Cart Coupons Product'; const singleProductFullPrice = '110.00'; @@ -34,24 +32,33 @@ const customerBilling = { email: 'john.doe.merchant.test@example.com', }; -const cartBlockPageTitle = `Cart Block Coupons ${ uuid.v1() }`; -const cartBlockPageSlug = cartBlockPageTitle - .replace( / /gi, '-' ) - .toLowerCase(); - let productId, orderId, limitedCouponId; -test.describe( 'Cart Block Applying Coupons', () => { - test.use( { storageState: process.env.ADMINSTATE } ); +baseTest.describe( 'Cart Block Applying Coupons', () => { + const test = baseTest.extend( { + storageState: process.env.ADMINSTATE, + testPageTitlePrefix: 'Cart Block', + page: async ( { context, page, testPage }, use ) => { + await goToPageEditor( { page } ); + await fillPageTitle( page, testPage.title ); + await insertBlockByShortcut( page, '/cart' ); + await publishPage( page, testPage.title ); + + await context.clearCookies(); + + await addAProductToCart( page, productId ); + await page.goto( testPage.slug ); + await expect( + page.getByRole( 'heading', { name: testPage.title } ) + ).toBeVisible(); + + await use( page ); + }, + } ); + const couponBatchId = []; - test.beforeAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.beforeAll( async ( { api } ) => { // make sure the currency is USD await api.put( 'settings/general/woocommerce_currency', { value: 'USD', @@ -105,13 +112,7 @@ test.describe( 'Cart Block Applying Coupons', () => { } ); } ); - test.afterAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.afterAll( async ( { api } ) => { await api.post( 'products/batch', { delete: [ productId ], } ); @@ -123,29 +124,11 @@ test.describe( 'Cart Block Applying Coupons', () => { } ); } ); - // eslint-disable-next-line playwright/expect-expect,jest/expect-expect - test( 'can create Cart Block page', async ( { page } ) => { - await goToPageEditor( { page } ); - await fillPageTitle( page, cartBlockPageTitle ); - await insertBlockByShortcut( page, '/cart' ); - await publishPage( page, cartBlockPageTitle ); - } ); - test( 'allows cart block to apply coupon of any type', async ( { page, - context, } ) => { - await context.clearCookies(); - const totals = [ '$50.00', '$27.50', '$45.00' ]; - await addAProductToCart( page, productId ); - await page.goto( cartBlockPageSlug ); - - await expect( - page.getByRole( 'heading', { name: cartBlockPageTitle } ) - ).toBeVisible(); - // apply all coupon types for ( let i = 0; i < coupons.length; i++ ) { await page.getByRole( 'button', { name: 'Add a coupon' } ).click(); @@ -178,23 +161,11 @@ test.describe( 'Cart Block Applying Coupons', () => { } } ); - test( 'allows cart block to apply multiple coupons', async ( { - page, - context, - } ) => { - await context.clearCookies(); - + test( 'allows cart block to apply multiple coupons', async ( { page } ) => { const totals = [ '$50.00', '$22.50', '$12.50' ]; const totalsReverse = [ '$17.50', '$45.00', '$55.00' ]; const discounts = [ '-$5.00', '-$32.50', '-$42.50' ]; - await addAProductToCart( page, productId ); - await page.goto( cartBlockPageSlug ); - - await expect( - page.getByRole( 'heading', { name: cartBlockPageTitle } ) - ).toBeVisible(); - // add all coupons and verify prices for ( let i = 0; i < coupons.length; i++ ) { await page.getByRole( 'button', { name: 'Add a coupon' } ).click(); @@ -235,17 +206,7 @@ test.describe( 'Cart Block Applying Coupons', () => { test( 'prevents cart block applying same coupon twice', async ( { page, - context, } ) => { - await context.clearCookies(); - - await addAProductToCart( page, productId ); - await page.goto( cartBlockPageSlug ); - - await expect( - page.getByRole( 'heading', { name: cartBlockPageTitle } ) - ).toBeVisible(); - // try to add two same coupons and verify the error message await page.getByRole( 'button', { name: 'Add a coupon' } ).click(); await page @@ -275,17 +236,7 @@ test.describe( 'Cart Block Applying Coupons', () => { test( 'prevents cart block applying coupon with usage limit', async ( { page, - context, } ) => { - await context.clearCookies(); - - await addAProductToCart( page, productId ); - await page.goto( cartBlockPageSlug ); - - await expect( - page.getByRole( 'heading', { name: cartBlockPageTitle } ) - ).toBeVisible(); - // add coupon with usage limit await page.getByRole( 'button', { name: 'Add a coupon' } ).click(); await page diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-block.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-block.spec.js index b9dc68d9cc9a8..c08a4cbba6607 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-block.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-block.spec.js @@ -1,4 +1,4 @@ -const { test, expect } = require( '@playwright/test' ); +const { test: baseTest, expect } = require( '../../fixtures/fixtures' ); const { goToPageEditor, fillPageTitle, @@ -6,8 +6,6 @@ const { publishPage, } = require( '../../utils/editor' ); const { addAProductToCart } = require( '../../utils/cart' ); -const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; -const uuid = require( 'uuid' ); const simpleProductName = 'Single Simple Product'; const simpleProductDesc = 'Lorem ipsum dolor sit amet.'; @@ -21,23 +19,15 @@ const singleProductWithCrossSellProducts = +firstCrossSellProductPrice + +secondCrossSellProductPrice; -const cartBlockPageTitle = `Cart Block ${ uuid.v1() }`; -const cartBlockPageSlug = cartBlockPageTitle - .replace( / /gi, '-' ) - .toLowerCase(); - let product1Id, product2Id, product3Id; -test.describe( 'Cart Block page', () => { - test.use( { storageState: process.env.ADMINSTATE } ); +baseTest.describe( 'Cart Block page', () => { + const test = baseTest.extend( { + storageState: process.env.ADMINSTATE, + testPageTitlePrefix: 'Cart Block', + } ); - test.beforeAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.beforeAll( async ( { api } ) => { // make sure the currency is USD await api.put( 'settings/general/woocommerce_currency', { value: 'USD', @@ -77,13 +67,7 @@ test.describe( 'Cart Block page', () => { } ); } ); - test.afterAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.afterAll( async ( { api } ) => { await api.post( 'products/batch', { delete: [ product1Id, product2Id, product3Id ], } ); @@ -91,16 +75,17 @@ test.describe( 'Cart Block page', () => { test( 'can see empty cart, add and remove simple & cross sell product, increase to max quantity', async ( { page, + testPage, } ) => { await goToPageEditor( { page } ); - await fillPageTitle( page, cartBlockPageTitle ); + await fillPageTitle( page, testPage.title ); await insertBlockByShortcut( page, '/cart' ); - await publishPage( page, cartBlockPageTitle ); + await publishPage( page, testPage.title ); // go to the page to test empty cart block - await page.goto( cartBlockPageSlug ); + await page.goto( testPage.slug ); await expect( - page.getByRole( 'heading', { name: cartBlockPageTitle } ) + page.getByRole( 'heading', { name: testPage.title } ) ).toBeVisible(); await expect( await page.getByText( 'Your cart is currently empty!' ).count() @@ -114,9 +99,9 @@ test.describe( 'Cart Block page', () => { ).toBeVisible(); await addAProductToCart( page, product1Id ); - await page.goto( cartBlockPageSlug ); + await page.goto( testPage.slug ); await expect( - page.getByRole( 'heading', { name: cartBlockPageTitle } ) + page.getByRole( 'heading', { name: testPage.title } ) ).toBeVisible(); await expect( page.getByRole( 'link', { name: simpleProductName, exact: true } ) @@ -162,9 +147,9 @@ test.describe( 'Cart Block page', () => { .getByText( `${ simpleProductName } Cross-Sell 2` ) ).toBeVisible(); - await page.goto( cartBlockPageSlug ); + await page.goto( testPage.slug ); await expect( - page.getByRole( 'heading', { name: cartBlockPageTitle } ) + page.getByRole( 'heading', { name: testPage.title } ) ).toBeVisible(); await expect( page.getByRole( 'heading', { name: 'You may be interested in…' } ) diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-checkout-block-calculate-tax.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-checkout-block-calculate-tax.spec.js index 70e17f9c43f84..bfcc372b4728b 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-checkout-block-calculate-tax.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-checkout-block-calculate-tax.spec.js @@ -7,18 +7,18 @@ const { publishPage, } = require( '../../utils/editor' ); const { addAProductToCart } = require( '../../utils/cart' ); -const uuid = require( 'uuid' ); +const { random } = require( '../../utils/helpers' ); const productName = 'First Product Cart Block Taxing'; const productPrice = '100.00'; const messyProductPrice = '13.47'; const secondProductName = 'Second Product Cart Block Taxing'; -const cartBlockPageTitle = `Cart Block ${ uuid.v1() }`; +const cartBlockPageTitle = `Cart Block ${ random() }`; const cartBlockPageSlug = cartBlockPageTitle .replace( / /gi, '-' ) .toLowerCase(); -const checkoutBlockPageTitle = `Checkout Block ${ uuid.v1() }`; +const checkoutBlockPageTitle = `Checkout Block ${ random() }`; const checkoutBlockPageSlug = checkoutBlockPageTitle .replace( / /gi, '-' ) .toLowerCase(); diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-checkout-calculate-tax.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-checkout-calculate-tax.spec.js index 44590aef0e677..146b414fc816e 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-checkout-calculate-tax.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/cart-checkout-calculate-tax.spec.js @@ -2,12 +2,12 @@ const { test, expect } = require( '@playwright/test' ); const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; const { customer } = require( '../../test-data/data' ); const { addAProductToCart } = require( '../../utils/cart' ); -const uuid = require( 'uuid' ); +const { random } = require( '../../utils/helpers' ); -const productName = `Taxed products are awesome ${ uuid.v1() }`; +const productName = `Taxed products are awesome ${ random() }`; const productPrice = '200.00'; const messyProductPrice = '13.47'; -const secondProductName = `Other products are also awesome ${ uuid.v1() }`; +const secondProductName = `Other products are also awesome ${ random() }`; let productId, productId2, diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-block-coupons.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-block-coupons.spec.js index 92ca3c1b6737f..11d47c046a0ce 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-block-coupons.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-block-coupons.spec.js @@ -5,11 +5,10 @@ const { publishPage, } = require( '../../utils/editor' ); const { addAProductToCart } = require( '../../utils/cart' ); -const { test, expect } = require( '@playwright/test' ); -const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; -const uuid = require( 'uuid' ); +const { test: baseTest, expect } = require( '../../fixtures/fixtures' ); +const { random } = require( '../../utils/helpers' ); -const simpleProductName = `Checkout Coupons Product ${ uuid.v1() }`; +const simpleProductName = `Checkout Coupons Product ${ random() }`; const singleProductFullPrice = '110.00'; const singleProductSalePrice = '55.00'; const coupons = [ @@ -34,24 +33,33 @@ const customerBilling = { email: 'john.doe.merchant.test@example.com', }; -const checkoutBlockPageTitle = `Checkout Block ${ uuid.v1() }`; -const checkoutBlockPageSlug = checkoutBlockPageTitle - .replace( / /gi, '-' ) - .toLowerCase(); - let productId, orderId, limitedCouponId; -test.describe( 'Checkout Block Applying Coupons', () => { - test.use( { storageState: process.env.ADMINSTATE } ); +baseTest.describe( 'Checkout Block Applying Coupons', () => { + const test = baseTest.extend( { + storageState: process.env.ADMINSTATE, + testPageTitlePrefix: 'Checkout Block', + page: async ( { context, page, testPage }, use ) => { + await goToPageEditor( { page } ); + await fillPageTitle( page, testPage.title ); + await insertBlockByShortcut( page, '/checkout' ); + await publishPage( page, testPage.title ); + + await context.clearCookies(); + + await addAProductToCart( page, productId ); + await page.goto( testPage.slug ); + await expect( + page.getByRole( 'heading', { name: testPage.title } ) + ).toBeVisible(); + + await use( page ); + }, + } ); + const couponBatchId = []; - test.beforeAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.beforeAll( async ( { api } ) => { // make sure the currency is USD await api.put( 'settings/general/woocommerce_currency', { value: 'USD', @@ -105,13 +113,7 @@ test.describe( 'Checkout Block Applying Coupons', () => { } ); } ); - test.afterAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.afterAll( async ( { api } ) => { await api.post( 'products/batch', { delete: [ productId ], } ); @@ -123,28 +125,11 @@ test.describe( 'Checkout Block Applying Coupons', () => { } ); } ); - // eslint-disable-next-line playwright/expect-expect - test( 'can create checkout block page', async ( { page } ) => { - await goToPageEditor( { page } ); - await fillPageTitle( page, checkoutBlockPageTitle ); - await insertBlockByShortcut( page, '/checkout' ); - await publishPage( page, checkoutBlockPageTitle ); - } ); - test( 'allows checkout block to apply coupon of any type', async ( { page, - context, } ) => { - await context.clearCookies(); const totals = [ '$50.00', '$27.50', '$45.00' ]; - await addAProductToCart( page, productId ); - await page.goto( checkoutBlockPageSlug ); - - await expect( - page.getByRole( 'heading', { name: checkoutBlockPageTitle } ) - ).toBeVisible(); - // apply all coupon types for ( let i = 0; i < coupons.length; i++ ) { await page.getByRole( 'button', { name: 'Add a coupon' } ).click(); @@ -179,20 +164,11 @@ test.describe( 'Checkout Block Applying Coupons', () => { test( 'allows checkout block to apply multiple coupons', async ( { page, - context, } ) => { - await context.clearCookies(); const totals = [ '$50.00', '$22.50', '$12.50' ]; const totalsReverse = [ '$17.50', '$45.00', '$55.00' ]; const discounts = [ '-$5.00', '-$32.50', '-$42.50' ]; - await addAProductToCart( page, productId ); - await page.goto( checkoutBlockPageSlug ); - - await expect( - page.getByRole( 'heading', { name: checkoutBlockPageTitle } ) - ).toBeVisible(); - // add all coupons and verify prices for ( let i = 0; i < coupons.length; i++ ) { await page.getByRole( 'button', { name: 'Add a coupon' } ).click(); @@ -233,17 +209,7 @@ test.describe( 'Checkout Block Applying Coupons', () => { test( 'prevents checkout block applying same coupon twice', async ( { page, - context, } ) => { - await context.clearCookies(); - - await addAProductToCart( page, productId ); - await page.goto( checkoutBlockPageSlug ); - - await expect( - page.getByRole( 'heading', { name: checkoutBlockPageTitle } ) - ).toBeVisible(); - // try to add two same coupons and verify the error message await page.getByRole( 'button', { name: 'Add a coupon' } ).click(); await page @@ -273,17 +239,7 @@ test.describe( 'Checkout Block Applying Coupons', () => { test( 'prevents checkout block applying coupon with usage limit', async ( { page, - context, } ) => { - await context.clearCookies(); - - await addAProductToCart( page, productId ); - await page.goto( checkoutBlockPageSlug ); - - await expect( - page.getByRole( 'heading', { name: checkoutBlockPageTitle } ) - ).toBeVisible(); - // add coupon with usage limit await page.getByRole( 'button', { name: 'Add a coupon' } ).click(); await page diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-create-account.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-create-account.spec.js index acc21a056a45e..76f1d0aac162c 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-create-account.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-create-account.spec.js @@ -2,6 +2,7 @@ const { test, expect } = require( '@playwright/test' ); const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; const { admin } = require( '../../test-data/data' ); const { getOrderIdFromUrl } = require( '../../utils/order' ); +const { addAProductToCart } = require( '../../utils/cart' ); const billingEmail = 'marge-test-account@example.com'; @@ -132,8 +133,7 @@ test.describe( 'Shopper Checkout Create Account', () => { await context.clearCookies(); // all tests use the first product - await page.goto( `shop/?add-to-cart=${ productId }` ); - await page.waitForLoadState( 'networkidle' ); + await addAProductToCart( page, productId ); } ); test( 'can create an account during checkout', async ( { page } ) => { diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-login.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-login.spec.js index 9dd51ea473fbb..4595ad180e4d1 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-login.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout-login.spec.js @@ -1,5 +1,6 @@ const { test, expect } = require( '@playwright/test' ); const { getOrderIdFromUrl } = require( '../../utils/order' ); +const { addAProductToCart } = require( '../../utils/cart' ); const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; const customer = { @@ -112,8 +113,7 @@ test.describe( 'Shopper Checkout Login Account', () => { await context.clearCookies(); // all tests use the first product - await page.goto( `/shop/?add-to-cart=${ productId }` ); - await page.waitForLoadState( 'networkidle' ); + await addAProductToCart( page, productId ); } ); test( 'can login to an existing account during checkout', async ( { diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout.spec.js index dd76a3b442a9e..c08a622b73167 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/checkout.spec.js @@ -3,6 +3,7 @@ const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; const { admin, customer } = require( '../../test-data/data' ); const { setFilterValue, clearFilters } = require( '../../utils/filters' ); const { addProductsToCart } = require( '../../utils/pdp' ); +const { addAProductToCart } = require( '../../utils/cart' ); const { getOrderIdFromUrl } = require( '../../utils/order' ); const guestEmail = 'checkout-guest@example.com'; @@ -119,8 +120,7 @@ test.describe( 'Checkout page', () => { } ); test( 'should display cart items in order review', async ( { page } ) => { - await page.goto( `/shop/?add-to-cart=${ productId }` ); - await page.waitForLoadState( 'networkidle' ); + await addAProductToCart( page, productId ); await page.goto( '/checkout/' ); @@ -201,9 +201,7 @@ test.describe( 'Checkout page', () => { test( 'warn when customer is missing required details', async ( { page, } ) => { - await page.goto( `/shop/?add-to-cart=${ productId }`, { - waitUntil: 'networkidle', - } ); + await addAProductToCart( page, productId ); await page.goto( '/checkout/' ); @@ -450,7 +448,12 @@ test.describe( 'Checkout page', () => { .locator( 'input[name="password"]' ) .fill( customer.password ); await page.locator( 'text=Log In' ).click(); - await page.waitForLoadState( 'networkidle' ); + await expect( + page.getByText( + `Hello ${ customer.first_name } ${ customer.last_name }` + ) + ).toBeVisible(); + await addProductsToCart( page, simpleProductName, '2' ); await page.goto( '/checkout/' ); diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/mini-cart.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/mini-cart.spec.js index b79c132102fc0..e64fb431b5694 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/mini-cart.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/mini-cart.spec.js @@ -1,9 +1,9 @@ const { test, expect } = require( '@playwright/test' ); const { disableWelcomeModal } = require( '../../utils/editor' ); const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; -const uuid = require( 'uuid' ); +const { random } = require( '../../utils/helpers' ); -const miniCartPageTitle = `Mini Cart ${ uuid.v1() }`; +const miniCartPageTitle = `Mini Cart ${ random() }`; const miniCartPageSlug = miniCartPageTitle.replace( / /gi, '-' ).toLowerCase(); const miniCartButton = 'main .wc-block-mini-cart__button'; const miniCartBadge = 'main .wc-block-mini-cart__badge'; diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/order-email-receiving.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/order-email-receiving.spec.js index 627995bd610f3..dc6e325f7d36e 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/order-email-receiving.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/order-email-receiving.spec.js @@ -2,6 +2,7 @@ const { test, expect } = require( '@playwright/test' ); const { customer, storeDetails } = require( '../../test-data/data' ); const { api } = require( '../../utils' ); const { getOrderIdFromUrl } = require( '../../utils/order' ); +const { addAProductToCart } = require( '../../utils/cart' ); let productId, orderId, zoneId; @@ -68,8 +69,7 @@ test.describe( 'Shopper Order Email Receiving', () => { // ensure that the store's address is in the US await api.update.storeDetails( storeDetails.us.store ); - await page.goto( `/shop/?add-to-cart=${ productId }` ); - await page.waitForLoadState( 'networkidle' ); + await addAProductToCart( page, productId ); await page.goto( '/checkout/' ); diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/product-simple.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/product-simple.spec.js index 5c89f3b62267c..8d3d3ab9fd758 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/product-simple.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/product-simple.spec.js @@ -1,5 +1,3 @@ -/* eslint-disable playwright/no-networkidle */ -/* eslint-disable jest/valid-expect */ const { test, expect } = require( '@playwright/test' ); const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; diff --git a/plugins/woocommerce/tests/e2e-pw/tests/shopper/shop-products-filter-by-price.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/shopper/shop-products-filter-by-price.spec.js index 45ebec18182f4..65bee2982e19b 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/shopper/shop-products-filter-by-price.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/shopper/shop-products-filter-by-price.spec.js @@ -1,4 +1,4 @@ -const { test, expect } = require( '@playwright/test' ); +const { test: baseTest, expect } = require( '../../fixtures/fixtures' ); const { goToPageEditor, fillPageTitle, @@ -6,8 +6,6 @@ const { insertBlockByShortcut, publishPage, } = require( '../../utils/editor' ); -const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; -const uuid = require( 'uuid' ); const singleProductPrice1 = '10'; const singleProductPrice2 = '50'; @@ -15,23 +13,15 @@ const singleProductPrice3 = '200'; const simpleProductName = 'AAA Filter Products'; -const productsFilteringPageTitle = `Products Filtering ${ uuid.v1() }`; -const productsFilteringPageSlug = productsFilteringPageTitle - .replace( / /gi, '-' ) - .toLowerCase(); - let product1Id, product2Id, product3Id; -test.describe( 'Filter items in the shop by product price', () => { - test.use( { storageState: process.env.ADMINSTATE } ); +baseTest.describe( 'Filter items in the shop by product price', () => { + const test = baseTest.extend( { + storageState: process.env.ADMINSTATE, + testPageTitlePrefix: 'Products filter', + } ); - test.beforeAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.beforeAll( async ( { api } ) => { // add products await api .post( 'products', { @@ -62,13 +52,7 @@ test.describe( 'Filter items in the shop by product price', () => { } ); } ); - test.afterAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); + test.afterAll( async ( { api } ) => { await api.post( 'products/batch', { delete: [ product1Id, product2Id, product3Id ], } ); @@ -76,19 +60,20 @@ test.describe( 'Filter items in the shop by product price', () => { test( 'filter products by prices on the created page', async ( { page, + testPage, } ) => { const sortingProductsDropdown = '.wc-block-sort-select__select'; await goToPageEditor( { page } ); - await fillPageTitle( page, productsFilteringPageTitle ); + await fillPageTitle( page, testPage.title ); await insertBlockByShortcut( page, '/filter' ); await insertBlock( page, 'All Products' ); - await publishPage( page, productsFilteringPageTitle ); + await publishPage( page, testPage.title ); // go to the page to test filtering products by price - await page.goto( productsFilteringPageSlug ); + await page.goto( testPage.slug ); await expect( - page.getByRole( 'heading', { name: productsFilteringPageTitle } ) + page.getByRole( 'heading', { name: testPage.title } ) ).toBeVisible(); // The price filter input is initially enabled, but it becomes disabled diff --git a/plugins/woocommerce/tests/e2e-pw/utils/helpers.js b/plugins/woocommerce/tests/e2e-pw/utils/helpers.js new file mode 100644 index 0000000000000..721aa5064092b --- /dev/null +++ b/plugins/woocommerce/tests/e2e-pw/utils/helpers.js @@ -0,0 +1,9 @@ +const crypto = require( 'crypto' ); + +const random = ( size = 4 ) => { + return crypto.randomBytes( size ).toString( 'hex' ); +}; + +module.exports = { + random, +}; diff --git a/plugins/woocommerce/tests/e2e-pw/utils/index.js b/plugins/woocommerce/tests/e2e-pw/utils/index.js index 65b6f5d740a31..ed7823a5a54a7 100644 --- a/plugins/woocommerce/tests/e2e-pw/utils/index.js +++ b/plugins/woocommerce/tests/e2e-pw/utils/index.js @@ -5,6 +5,7 @@ const features = require( './features' ); const tours = require( './tours' ); const login = require( './login' ); const editor = require( './editor' ); +const helpers = require( './helpers' ); module.exports = { api, @@ -14,4 +15,5 @@ module.exports = { tours, login, editor, + helpers, };