From 91e1347aab2212791b76861a30ccc8c2c39b3907 Mon Sep 17 00:00:00 2001 From: Jamel Noel Reid Date: Fri, 7 Oct 2022 15:11:49 -0500 Subject: [PATCH] Fix order email receiving test (#34972) * Added JS data file * Moved onboarding logic into utils folder * Used new onboarding methods and data file * Added changelog * Moved utils directory * Added api utils * Added customer details data * Used new api utils and data * Updated path * Added comment and removed log * Removed log * Added changelog --- .../changelog/add-pw-onboarding-utils | 4 - .../changelog/fix-order-email-receiving-test | 4 + .../tests/e2e-pw/test-data/data.js | 31 +++++- .../shopper/order-email-receiving.spec.js | 100 ++++++------------ plugins/woocommerce/tests/e2e-pw/utils/api.js | 94 ++++++++++++++++ .../woocommerce/tests/e2e-pw/utils/index.js | 2 + 6 files changed, 161 insertions(+), 74 deletions(-) delete mode 100644 plugins/woocommerce/changelog/add-pw-onboarding-utils create mode 100644 plugins/woocommerce/changelog/fix-order-email-receiving-test create mode 100644 plugins/woocommerce/tests/e2e-pw/utils/api.js diff --git a/plugins/woocommerce/changelog/add-pw-onboarding-utils b/plugins/woocommerce/changelog/add-pw-onboarding-utils deleted file mode 100644 index d2e1bda19e7..00000000000 --- a/plugins/woocommerce/changelog/add-pw-onboarding-utils +++ /dev/null @@ -1,4 +0,0 @@ -Significance: patch -Type: dev - -Moved onboarding methods into utils method for the sake of reusability diff --git a/plugins/woocommerce/changelog/fix-order-email-receiving-test b/plugins/woocommerce/changelog/fix-order-email-receiving-test new file mode 100644 index 00000000000..347398caa9d --- /dev/null +++ b/plugins/woocommerce/changelog/fix-order-email-receiving-test @@ -0,0 +1,4 @@ +Significance: patch +Type: dev + +set the store country in the test step diff --git a/plugins/woocommerce/tests/e2e-pw/test-data/data.js b/plugins/woocommerce/tests/e2e-pw/test-data/data.js index 96ff3ea0517..d30acf7d538 100644 --- a/plugins/woocommerce/tests/e2e-pw/test-data/data.js +++ b/plugins/woocommerce/tests/e2e-pw/test-data/data.js @@ -10,7 +10,8 @@ const storeDetails = { city: 'San Francisco', zip: '94107', email: adminEmail, - country: 'United States (US) — California', // corresponding to the text value of the option + country: 'United States (US) — California', // corresponding to the text value of the option, + countryCode: 'US:CA', }, expectedIndustries: 8, // There are 8 checkboxes on the page (in the US), adjust this constant if we change that industries: { @@ -28,7 +29,8 @@ const storeDetails = { city: 'Valletta', zip: 'VT 1011', email: adminEmail, - country: 'Malta', // corresponding to the text value of the option + country: 'Malta', // corresponding to the text value of the option, + countryCode: 'MT', }, expectedIndustries: 7, // There are 7 checkboxes on the page (in Malta), adjust this constant if we change that industries: { @@ -41,6 +43,31 @@ const storeDetails = { }, }; +const customerDetails = { + us: { + first_name: 'Maggie', + last_name: 'Simpson', + address: '123 Evergreen Terrace', + city: 'Springfield', + country: 'US', + state: 'OR', + zip: '97403', + phone: '555 555-5555', + email: 'customer@example.com', + }, + malta: { + first_name: 'Maggie', + last_name: 'Simpson', + address: '123 Evergreen Terrace', + city: 'Valletta', + country: 'MT', + zip: 'VT 1011', + phone: '555 555-5555', + email: 'vt-customer@example.com', + }, +}; + module.exports = { storeDetails, + customerDetails, }; 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 150fc8eeaf1..b31fd8da00d 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 @@ -1,56 +1,24 @@ const { test, expect } = require( '@playwright/test' ); -const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; +const { customerDetails, storeDetails } = require( '../../test-data/data' ); +const { api } = require( '../../utils' ); let productId, orderId; -const productName = 'Order email product'; + +const product = { + name: 'Order email product', + type: 'simple', + price: '42.77', +}; + const customerEmail = 'order-email-test@example.com'; const storeName = 'WooCommerce Core E2E Test Suite'; test.describe( 'Shopper Order Email Receiving', () => { test.use( { storageState: process.env.ADMINSTATE } ); - test.beforeAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); - // ensure store address is US - await api.post( 'settings/general/batch', { - update: [ - { - id: 'woocommerce_store_address', - value: 'addr 1', - }, - { - id: 'woocommerce_store_city', - value: 'San Francisco', - }, - { - id: 'woocommerce_default_country', - value: 'US:CA', - }, - { - id: 'woocommerce_store_postcode', - value: '94107', - }, - ], - } ); - // add product - await api - .post( 'products', { - name: productName, - type: 'simple', - regular_price: '42.77', - } ) - .then( ( response ) => { - productId = response.data.id; - } ); - // enable COD payment - await api.put( 'payment_gateways/cod', { - enabled: true, - } ); + test.beforeAll( async () => { + productId = await api.create.product( product ); + await api.update.enableCashOnDelivery(); } ); test.beforeEach( async ( { page } ) => { @@ -67,42 +35,38 @@ test.describe( 'Shopper Order Email Receiving', () => { } } ); - test.afterAll( async ( { baseURL } ) => { - const api = new wcApi( { - url: baseURL, - consumerKey: process.env.CONSUMER_KEY, - consumerSecret: process.env.CONSUMER_SECRET, - version: 'wc/v3', - } ); - await api.delete( `products/${ productId }`, { - force: true, - } ); + test.afterAll( async () => { + await api.deletePost.product( productId ); if ( orderId ) { - await api.delete( `orders/${ orderId }`, { - force: true, - } ); + await api.deletePost.order( orderId ); } - await api.put( 'payment_gateways/cod', { - enabled: false, - } ); + await api.update.disableCashOnDelivery(); } ); test( 'should receive order email after purchasing an item', async ( { page, } ) => { + // 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 page.goto( '/checkout/' ); - await page.fill( '#billing_first_name', 'Maggie' ); - await page.fill( '#billing_last_name', 'Simpson' ); - await page.fill( '#billing_address_1', '123 Evergreen Terrace' ); - await page.fill( '#billing_city', 'Springfield' ); - await page.selectOption( '#billing_country', 'US' ); - await page.selectOption( '#billing_state', 'OR' ); - await page.fill( '#billing_postcode', '97403' ); - await page.fill( '#billing_phone', '555 555-5555' ); + await page.fill( '#billing_first_name', customerDetails.us.first_name ); + await page.fill( '#billing_last_name', customerDetails.us.last_name ); + await page.fill( '#billing_address_1', customerDetails.us.address ); + await page.fill( '#billing_city', customerDetails.us.city ); + await page.selectOption( + '#billing_country', + customerDetails.us.country + ); + + await page.selectOption( '#billing_state', customerDetails.us.state ); + + await page.fill( '#billing_postcode', customerDetails.us.zip ); + await page.fill( '#billing_phone', customerDetails.us.phone ); await page.fill( '#billing_email', customerEmail ); await page.click( 'text=Place order' ); diff --git a/plugins/woocommerce/tests/e2e-pw/utils/api.js b/plugins/woocommerce/tests/e2e-pw/utils/api.js new file mode 100644 index 00000000000..1fe7c992b80 --- /dev/null +++ b/plugins/woocommerce/tests/e2e-pw/utils/api.js @@ -0,0 +1,94 @@ +const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default; +const config = require( '../playwright.config' ); + +let api; + +// Ensure that global-setup.js runs before creating api client +if ( process.env.CONSUMER_KEY && process.env.CONSUMER_SECRET ) { + api = new wcApi( { + url: config.use.baseURL, + consumerKey: process.env.CONSUMER_KEY, + consumerSecret: process.env.CONSUMER_SECRET, + version: 'wc/v3', + } ); +} + +const update = { + storeDetails: async ( store ) => { + // ensure store address is US + const res = await api.post( 'settings/general/batch', { + update: [ + { + id: 'woocommerce_store_address', + value: store.address, + }, + { + id: 'woocommerce_store_city', + value: store.city, + }, + { + id: 'woocommerce_default_country', + value: store.countryCode, + }, + { + id: 'woocommerce_store_postcode', + value: store.zip, + }, + ], + } ); + }, + enableCashOnDelivery: async () => { + await api.put( 'payment_gateways/cod', { + enabled: true, + } ); + }, + disableCashOnDelivery: async () => { + await api.put( 'payment_gateways/cod', { + enabled: false, + } ); + }, +}; + +const get = { + defaultCountry: async () => { + const response = await api.get( + 'settings/general/woocommerce_default_country' + ); + + const code = response.data.default; + + return code; + }, +}; + +const create = { + product: async ( product ) => { + const response = await api.post( 'products', { + name: product.name, + type: product.type, + regular_price: product.price, + } ); + + return response.data.id; + }, +}; + +const deletePost = { + product: async ( id ) => { + await api.delete( `products/${ id }`, { + force: true, + } ); + }, + order: async ( id ) => { + await api.delete( `orders/${ id }`, { + force: true, + } ); + }, +}; + +module.exports = { + update, + get, + create, + deletePost, +}; diff --git a/plugins/woocommerce/tests/e2e-pw/utils/index.js b/plugins/woocommerce/tests/e2e-pw/utils/index.js index 259a0a30175..ff02a21ec22 100644 --- a/plugins/woocommerce/tests/e2e-pw/utils/index.js +++ b/plugins/woocommerce/tests/e2e-pw/utils/index.js @@ -1,5 +1,7 @@ const onboarding = require( './onboarding' ); +const api = require( './api' ); module.exports = { onboarding, + api, };