From 0420dd812aa3aa85f8f8e3a302762ebb9af53aac Mon Sep 17 00:00:00 2001 From: shashwata Date: Wed, 14 Feb 2024 00:56:18 +0600 Subject: [PATCH] update: update product addon tests --- tests/pw/tests/e2e/productAddons.spec.ts | 32 +++++++++++++++----- tests/pw/utils/apiEndPoints.ts | 6 ++-- tests/pw/utils/apiUtils.ts | 38 ++++++++++++++++++++++++ tests/pw/utils/dbUtils.ts | 8 +++++ tests/pw/utils/payloads.ts | 12 ++++++-- 5 files changed, 82 insertions(+), 14 deletions(-) diff --git a/tests/pw/tests/e2e/productAddons.spec.ts b/tests/pw/tests/e2e/productAddons.spec.ts index aceb86b332..9fae12df14 100644 --- a/tests/pw/tests/e2e/productAddons.spec.ts +++ b/tests/pw/tests/e2e/productAddons.spec.ts @@ -1,40 +1,56 @@ -import { test, Page } from '@playwright/test'; +import { test, request, Page } from '@playwright/test'; import { ProductAddonsPage } from '@pages/productAddonsPage'; +import { ApiUtils } from '@utils/apiUtils'; import { data } from '@utils/testData'; +import { payloads } from '@utils/payloads'; +import { dbUtils } from '@utils/dbUtils'; + +const { VENDOR_ID } = process.env; test.describe('Product addon functionality test', () => { let vendor: ProductAddonsPage; let vPage: Page; + let addonId: string; let addonName: string; let addonFieldTitle: string; + let apiUtils: ApiUtils; + + async function createVendorProductAddon(): Promise<[string, string, string]> { + const [, addonId, addonName, addonFieldTitle] = await apiUtils.createProductAddon(payloads.createProductAddons(), payloads.adminAuth); + await dbUtils.updateCell(addonId, VENDOR_ID); + console.log(addonId, addonName, addonFieldTitle); + return [addonId, addonName, addonFieldTitle]; + } test.beforeAll(async ({ browser }) => { const vendorContext = await browser.newContext(data.auth.vendorAuth); vPage = await vendorContext.newPage(); vendor = new ProductAddonsPage(vPage); - addonName = data.vendor.addon.randomName(); - addonFieldTitle = data.vendor.addon.randomTitle(); - await vendor.addAddon({ ...data.vendor.addon, name: addonName, titleRequired: addonFieldTitle }); + apiUtils = new ApiUtils(await request.newContext()); + [addonId, addonName, addonFieldTitle] = await createVendorProductAddon(); }); test.afterAll(async () => { + await apiUtils.deleteAllProductAddons(payloads.adminAuth); await vPage.close(); + await apiUtils.dispose(); }); - test('vendor product addons menu page is rendering properly @pro @explo', async () => { + test('vendor product addons menu page is rendering properly @pro @exp @v', async () => { await vendor.vendorProductAddonsSettingsRenderProperly(); }); - test('vendor can add addons @pro', async () => { + test('vendor can add addons @pro @v', async () => { await vendor.addAddon({ ...data.vendor.addon, name: data.vendor.addon.randomName() }); }); - test('vendor can edit addon @pro', async () => { + test('vendor can edit addon @pro @v', async () => { await vendor.editAddon({ ...data.vendor.addon, name: addonName, titleRequired: addonFieldTitle }); }); - test('vendor can delete addon @pro', async () => { + test('vendor can delete addon @pro @v', async () => { + const [, addonName] = await createVendorProductAddon(); await vendor.deleteAddon({ ...data.vendor.addon, name: addonName }); }); }); diff --git a/tests/pw/utils/apiEndPoints.ts b/tests/pw/utils/apiEndPoints.ts index 625582a260..dd09c12769 100644 --- a/tests/pw/utils/apiEndPoints.ts +++ b/tests/pw/utils/apiEndPoints.ts @@ -538,10 +538,10 @@ export const endPoints = { productAddons: { getAllProductAddons: `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons`, - getSingleProductAddon: (productId: string) => `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons${productId}`, + getSingleProductAddon: (productId: string) => `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons/${productId}`, createProductAddon: `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons`, - updateProductAddon: (productId: string) => `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons${productId}`, - deleteProductAddon: (productId: string) => `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons${productId}`, + updateProductAddon: (productId: string) => `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons/${productId}`, + deleteProductAddon: (productId: string) => `${SERVER_URL}/wc-product-add-ons/v1/product-add-ons/${productId}`, }, }, diff --git a/tests/pw/utils/apiUtils.ts b/tests/pw/utils/apiUtils.ts index 8b32729c9a..4438648e8e 100644 --- a/tests/pw/utils/apiUtils.ts +++ b/tests/pw/utils/apiUtils.ts @@ -1789,4 +1789,42 @@ export class ApiUtils { return orderDetails; } + + /** + * woocommerce product addon api methods + */ + + // get all product addons + async getAllProductAddons(auth?: auth): Promise { + const [, responseBody] = await this.get(endPoints.wc.productAddons.getAllProductAddons, { headers: auth }); + return responseBody; + } + + // create product addon + async createProductAddon(payload: object, auth?: auth): Promise<[responseBody, string, string, string]> { + const [, responseBody] = await this.post(endPoints.wc.productAddons.createProductAddon, { data: payload, headers: auth }); + const productAddonId = String(responseBody?.id); + const addonName = responseBody.name; + const addonFieldTitle = responseBody.fields[0].name; + return [responseBody, productAddonId, addonName, addonFieldTitle]; + } + + // delete product addon + async deleteProductAddon(productAddonId: string, auth?: auth): Promise { + const [, responseBody] = await this.delete(endPoints.wc.productAddons.deleteProductAddon(productAddonId), { headers: auth }); + return responseBody; + } + + // delete all product addons + async deleteAllProductAddons(auth?: auth): Promise { + const allProductAddons = await this.getAllProductAddons(auth); + if (!allProductAddons?.length) { + console.log('No product addon exists'); + return; + } + const allProductAddonIds = allProductAddons.map((o: { id: unknown }) => o.id); + for (const productAddonId of allProductAddonIds) { + await this.deleteProductAddon(productAddonId, auth); + } + } } diff --git a/tests/pw/utils/dbUtils.ts b/tests/pw/utils/dbUtils.ts index 7f851fd852..804f8f3fd5 100644 --- a/tests/pw/utils/dbUtils.ts +++ b/tests/pw/utils/dbUtils.ts @@ -123,4 +123,12 @@ export const dbUtils = { // console.log(res); return [res, refundId]; }, + + // update cell + async updateCell(id: any, value: any): Promise { + const queryUpdate = `UPDATE ${dbPrefix}_posts SET post_author = '${value}' WHERE ID = '${id}';`; + const res = await dbUtils.dbQuery(queryUpdate); + // console.log(res); + return res; + }, }; diff --git a/tests/pw/utils/payloads.ts b/tests/pw/utils/payloads.ts index b68e8c55b9..1cb7576bfe 100644 --- a/tests/pw/utils/payloads.ts +++ b/tests/pw/utils/payloads.ts @@ -484,8 +484,8 @@ export const payloads = { }), createProductAddons: () => ({ - name: 'Test Addons Group_1' + faker.string.nanoid(10), - priority: 1, + name: 'Test Addons Group_' + faker.string.nanoid(10), + priority: 10, restrict_to_categories: [], fields: [ { @@ -507,7 +507,13 @@ export const payloads = { options: [ { label: 'Option 1', - price: '30', + price: '10', + image: '', + price_type: 'flat_fee', + }, + { + label: 'Option 2', + price: '20', image: '', price_type: 'flat_fee', },