From cc5f47e276073ae9a8d7769e6dfb5bbcf4730465 Mon Sep 17 00:00:00 2001 From: najeeb1023 Date: Sun, 23 Jun 2024 16:15:03 +0200 Subject: [PATCH 1/3] selected random product size --- src/test/hooks/hooks.ts | 2 +- src/test/pages/UserShopping.ts | 14 +++++++++++--- src/test/resources/userShoppingPage.json | 4 ++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/test/hooks/hooks.ts b/src/test/hooks/hooks.ts index 88ee293..0ca080c 100644 --- a/src/test/hooks/hooks.ts +++ b/src/test/hooks/hooks.ts @@ -9,7 +9,7 @@ let context: BrowserContext; BeforeAll(async function () { pageFixture.logger = createCustomLogger(); - pageFixture.logger.info('----------------- New Test Run -----------------'); + pageFixture.logger.info('----------------- New Test Run -----------------' + "\n"); browser = await chromium.launch(config); }); diff --git a/src/test/pages/UserShopping.ts b/src/test/pages/UserShopping.ts index 667a12d..d09e0d1 100644 --- a/src/test/pages/UserShopping.ts +++ b/src/test/pages/UserShopping.ts @@ -42,6 +42,7 @@ import { Page, expect } from "@playwright/test"; getProductSizesAvailable:() => pageFixture.page.locator(getResource('getProductSizesAvailable').selectorValue), getCurrentSelectedColor:() => pageFixture.page.locator(getResource('getCurrentSelectedColor').selectorValue), getColorSwatches:() => pageFixture.page.locator(getResource('getColorSwatches').selectorValue), + getSelectedProductSize:() => pageFixture.page.locator(getResource('getSelectedProductSize').selectorValue) }; @@ -68,7 +69,7 @@ import { Page, expect } from "@playwright/test"; public async selectRandomProduct():Promise{ const getNumberOfProducts = await this.userShoppingLocators.productShown().count(); let ind: number = Math.floor(Math.random() * (getNumberOfProducts - 1))+ 1; - + if (ind == 0) { Math.floor(Math.random() * getNumberOfProducts); } else { @@ -105,9 +106,14 @@ import { Page, expect } from "@playwright/test"; console.log(''+i +")" + " " + text.trim()); }; } - } else { + } else { return this.selectRandomProduct(); - } + }; + const getSizes = await this.userShoppingLocators.getProductSizesAvailable().count(); + let ind: number = Math.floor(Math.random() * (getSizes - 1))+ 1; + const sizeEl = (pageFixture.page.locator(getResource('getProductSize').selectorValue.replace('FLAG', `${ind}`))); + await sizeEl.click(); + console.log("Size selected: "+await this.userShoppingLocators.getSelectedProductSize().textContent()); }; public async selectAndGetProductColors():Promise{ @@ -124,4 +130,6 @@ import { Page, expect } from "@playwright/test"; console.log(getColor); }; }; + + }; \ No newline at end of file diff --git a/src/test/resources/userShoppingPage.json b/src/test/resources/userShoppingPage.json index 832bea9..dad5034 100644 --- a/src/test/resources/userShoppingPage.json +++ b/src/test/resources/userShoppingPage.json @@ -38,6 +38,10 @@ "elementName": "getProductSize", "selectorValue": "//div[contains(@class,'swatch-option text')][FLAG]" }, + { + "elementName": "getSelectedProductSize", + "selectorValue": "//div[contains(@class,'swatch-attribute size')]//span[2]" + }, { "elementName": "getProductSizesAvailable", "selectorValue": "//div[contains(@class,'swatch-option text')]" From df3cf14206de23daf287b5326c10abaee2cfbb1a Mon Sep 17 00:00:00 2001 From: najeeb1023 Date: Sun, 23 Jun 2024 16:16:07 +0200 Subject: [PATCH 2/3] product size selection added, globalTimeout reduced to 15 seconds. --- playwright.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playwright.config.ts b/playwright.config.ts index 6843b24..18eefd5 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -4,7 +4,7 @@ import createCustomLogger from "./logger/logger"; const logger = createCustomLogger(); export const config: LaunchOptions = { - timeout: 600000, + timeout: 150000, headless: true, // args:['--window-position=-1070,-100'], //* customized for left-sided portrait monitor. slowMo: 300, From 417f183ad1397ced632aee4bc199ed06d0dd8bae Mon Sep 17 00:00:00 2001 From: najeeb1023 Date: Sun, 23 Jun 2024 17:04:08 +0200 Subject: [PATCH 3/3] feat: added colorization for fetched products and sizes. --- src/test/features/UserShopping.feature | 4 ++-- src/test/pages/UserShopping.ts | 28 ++++++++++++++---------- src/test/resources/userShoppingPage.json | 2 +- src/test/steps/userShopping.ts | 5 ++--- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/test/features/UserShopping.feature b/src/test/features/UserShopping.feature index 768e02c..4f3eaca 100644 --- a/src/test/features/UserShopping.feature +++ b/src/test/features/UserShopping.feature @@ -11,7 +11,7 @@ Feature: Verify that the user is able to purchase some item. Scenario: User shops for Men Jackets. When The user clicks on the "
" section and the user clicks on "" option. And The products are shown and user navigates to a product. - And The price, size of that product is shown. + And The details of the product are shown. Examples: | Section | Attire | @@ -21,7 +21,7 @@ Feature: Verify that the user is able to purchase some item. Scenario: User shops for Women Jackets. When The user clicks on the "
" section and the user clicks on "" option. And The products are shown and user navigates to a product. - And The price, size of that product is shown. + And The details of the product are shown. Examples: | Section | Attire | diff --git a/src/test/pages/UserShopping.ts b/src/test/pages/UserShopping.ts index d09e0d1..e6a9d0e 100644 --- a/src/test/pages/UserShopping.ts +++ b/src/test/pages/UserShopping.ts @@ -10,8 +10,8 @@ import { Page, expect } from "@playwright/test"; export class CategoryAndProductSelectionFacade{ private userShopping: UserShopping; - constructor(menSection: UserShopping){ - this.userShopping = menSection; + constructor(userShopping: UserShopping){ + this.userShopping = userShopping; }; public async productSelection(section: string, attire: string){ @@ -22,6 +22,11 @@ import { Page, expect } from "@playwright/test"; await this.userShopping.showItems(); await this.userShopping.selectRandomProduct(); }; + + public async showProductDetails(){ + await this.userShopping.getProductPriceAndSizes(); + await this.userShopping.selectAndGetProductColors() + }; }; export class UserShopping { @@ -30,7 +35,7 @@ import { Page, expect } from "@playwright/test"; }; userShoppingLocators = { - menSectionHeader:() => pageFixture.page.locator(getResource('menSectionBtn').selectorValue), + shoppingSectionHeader:() => pageFixture.page.locator(getResource('shoppingSectionHeader').selectorValue), attireSectionBtn:() => pageFixture.page.locator(getResource('attireSectionBtn').selectorValue), itemsShown:() => pageFixture.page.locator(getResource('itemsShown').selectorValue), attireSectionOptions:() => pageFixture.page.locator(getResource('attireSectionOptions').selectorValue), @@ -47,7 +52,7 @@ import { Page, expect } from "@playwright/test"; }; public async goSectionAndAttire(section: string, attire: string):Promise{ - const el = pageFixture.page.locator(getResource('menSectionBtn').selectorValue.replace('FLAG', section)); + const el = pageFixture.page.locator(getResource('shoppingSectionHeader').selectorValue.replace('FLAG', section)); await el.click(); for(let i=0;i<=0;i++){ const el = await pageFixture.page.locator(getResource('attireSectionBtn').selectorValue.replace('FLAG', attire)); @@ -57,11 +62,12 @@ import { Page, expect } from "@playwright/test"; public async showItems():Promise{ const getNumberOfProducts = await this.userShoppingLocators.productShown().count(); - process.stdout.write(' Products shown -> ' + getNumberOfProducts + '\n'); + console.log(' Products shown -> ' + getNumberOfProducts + '\n'); for(let i=1;i<=getNumberOfProducts;i++){ const getEl = await pageFixture.page.locator(getResource('itemsShown').selectorValue.replace('FLAG', i.toString())).allTextContents(); for (const text of getEl) { - console.log(''+i +")" + " " + text.trim()); + //console.log(''+i +")" + " " + '\u001b[34m',text.trim()); + console.log(''+i +")" + " " + '\x1b[36m%s\x1b[0m',text.trim()); }; }; }; @@ -79,7 +85,7 @@ import { Page, expect } from "@playwright/test"; const list = await this.userShoppingLocators.shoppingList().isVisible(); const listCount = await this.userShoppingLocators.shoppingList().count(); if (list == true){ - pageFixture.logger.error('User not navigated, retrying click'); + pageFixture.logger.warn('User not navigated, retrying click'); await pageFixture.page.waitForLoadState('networkidle'); for(let i=0;i{ const priceText = (await this.userShoppingLocators.productPrice().textContent()).trim(); if(await this.userShoppingLocators.productPrice().isVisible() == true){ - pageFixture.logger.error('Product price is not visible, attempting to click product again.') + pageFixture.logger.warn('Product price is not visible, attempting to click product again.') const regEx = /\$\d+\.\d{2}/; const matchPriceText = priceText.match(regEx); console.log("The price of the product -> "+matchPriceText[0]); const sizeText = (await this.userShoppingLocators.productSize().textContent()).trim(); const getSizes = await this.userShoppingLocators.getProductSizesAvailable().count(); - console.log(sizeText+'s' + ' available are: '); + console.log('\x1b[36m%s\x1b[0m',sizeText+'s' + ' available are: '); for(let i=1;i<=getSizes;i++){ const el = await pageFixture.page.locator(getResource('getProductSize').selectorValue.replace('FLAG', i.toString())).allTextContents(); for (const text of el) { - console.log(''+i +")" + " " + text.trim()); + console.log(''+i +")" + " " + '\x1b[36m%s\x1b[0m',text.trim()); }; } } else { @@ -127,7 +133,7 @@ import { Page, expect } from "@playwright/test"; await colorToBeSelect.click(); await this.userShoppingLocators.getCurrentSelectedColor().isVisible(); const getColor = await this.userShoppingLocators.getCurrentSelectedColor().textContent(); - console.log(getColor); + console.log('Selected color: ' +getColor); }; }; diff --git a/src/test/resources/userShoppingPage.json b/src/test/resources/userShoppingPage.json index dad5034..9da07d8 100644 --- a/src/test/resources/userShoppingPage.json +++ b/src/test/resources/userShoppingPage.json @@ -3,7 +3,7 @@ "webElements": [ { - "elementName": "menSectionBtn", + "elementName": "shoppingSectionHeader", "selectorValue": "//span[contains(.,'FLAG')]" }, { diff --git a/src/test/steps/userShopping.ts b/src/test/steps/userShopping.ts index a80014a..1937733 100644 --- a/src/test/steps/userShopping.ts +++ b/src/test/steps/userShopping.ts @@ -14,7 +14,6 @@ When("The products are shown and user navigates to a product.", async function ( await categoryAndProductSectionFacade.selectRandomItem(); }); -When("The price, size of that product is shown.", async function (){ - await userShopping.getProductPriceAndSizes(); - await userShopping.selectAndGetProductColors(); +When("The details of the product are shown.", async function (){ + await categoryAndProductSectionFacade.showProductDetails(); }); \ No newline at end of file