From 504df1ee44bc659f4b089050c8b778d27e60115c Mon Sep 17 00:00:00 2001 From: Alex Stefan Date: Fri, 26 Jan 2024 22:21:52 +0200 Subject: [PATCH 1/4] hotSellersSection first test rework --- pages/home.page.js | 3 ++- tests/homePage/HotSellersSection.spec.js | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pages/home.page.js b/pages/home.page.js index b6f0938..076ce42 100644 --- a/pages/home.page.js +++ b/pages/home.page.js @@ -6,7 +6,8 @@ export class HomePage { } // locators - whatIsNewBTN = () => this.page.locator("#ui-id-3") + whatIsNewBTN = () => this.page.locator("#ui-id-3"); + hotSellersProducts = () => this.page.locator(".product-item"); async navigateToHomePage() { await this.page.goto(this.homePageUrl); diff --git a/tests/homePage/HotSellersSection.spec.js b/tests/homePage/HotSellersSection.spec.js index 80ac7ff..32c719b 100644 --- a/tests/homePage/HotSellersSection.spec.js +++ b/tests/homePage/HotSellersSection.spec.js @@ -1,9 +1,10 @@ import { test, expect } from "@playwright/test"; +import { HomePage } from '../../pages/home.page'; test("Six items displayed at hot sellers", async ({ page }) => { - await page.goto("https://magento.softwaretestingboard.com/"); - const productItems = await page.$$(".product-item"); - expect(productItems).toHaveLength(6); + const homePage = new HomePage(page) + await homePage.navigateToHomePage(); + expect(homePage.hotSellersProducts()).toHaveCount(6); }); test("Item redirects correctly on home page", async ({ page }) => { From 680a8365ca51417595a30b6eb8d08eb364b12ea6 Mon Sep 17 00:00:00 2001 From: Alex Stefan Date: Fri, 26 Jan 2024 23:49:00 +0200 Subject: [PATCH 2/4] added product.details.js and reworked 2nd test from hotSellerSection --- pages/product.details.js | 10 ++++++ tests/homePage/HotSellersSection.spec.js | 40 +++++++++--------------- 2 files changed, 25 insertions(+), 25 deletions(-) create mode 100644 pages/product.details.js diff --git a/pages/product.details.js b/pages/product.details.js new file mode 100644 index 0000000..04d3eab --- /dev/null +++ b/pages/product.details.js @@ -0,0 +1,10 @@ +export class ProductDetails { + + constructor(page) { + this.page = page; + } + + // locators + productName = () => this.page.locator(".base"); + } + \ No newline at end of file diff --git a/tests/homePage/HotSellersSection.spec.js b/tests/homePage/HotSellersSection.spec.js index 32c719b..ae660f0 100644 --- a/tests/homePage/HotSellersSection.spec.js +++ b/tests/homePage/HotSellersSection.spec.js @@ -1,35 +1,25 @@ import { test, expect } from "@playwright/test"; -import { HomePage } from '../../pages/home.page'; +import { HomePage } from "../../pages/home.page"; +import { ProductDetails } from "../../pages/product.details"; test("Six items displayed at hot sellers", async ({ page }) => { - const homePage = new HomePage(page) + const homePage = new HomePage(page); await homePage.navigateToHomePage(); expect(homePage.hotSellersProducts()).toHaveCount(6); }); test("Item redirects correctly on home page", async ({ page }) => { - await page.goto("https://magento.softwaretestingboard.com/"); + const homePage = new HomePage(page); + const productDetails = new ProductDetails(page); - const productItems = await page.$$(".product-item"); - const priceItems = await page.locator(".price").allTextContents(); - const nameItems = await page.locator(".product-item-link").allInnerTexts(); - - for (let item in productItems) { - // Re-fetch the elements inside the loop to ensure they are still attached to the DOM - const updatedProductItems = await page.$$(".product-item"); - - // Go to item`s page - await updatedProductItems[item].click(); - - // Get the name of the item - const nameItemsPage = await page.locator("span.base").innerText(); - - // Get the price of the item - const priceItemPage = await page.locator(".product-info-main .price"); - - expect(nameItemsPage).toContain(nameItems[item]); - expect(priceItemPage).toHaveText(priceItems[item]); - - await page.goto("https://magento.softwaretestingboard.com/"); - } + await homePage.navigateToHomePage(); + const randomNumber = Math.floor(Math.random() * 6); + const productName = ( + await homePage + .hotSellersProducts() + .locator(`nth=${randomNumber}`) + .innerText() + ).split("\n")[0]; + await homePage.hotSellersProducts().locator(`nth=${randomNumber}`).click(); + expect(productName).toContain(await productDetails.productName().innerText()); }); From db6072765c4a47a79547e11d9976f43c8e91090f Mon Sep 17 00:00:00 2001 From: Alex Stefan Date: Sat, 27 Jan 2024 23:42:41 +0200 Subject: [PATCH 3/4] tests rework, added utils.js + product.details, update home.page --- helpers/utils.js | 11 +++++++++++ pages/home.page.js | 21 ++++++++++++++++++--- pages/product.details.js | 16 +++++++++------- tests/homePage/HotSellersSection.spec.js | 22 ++++++++++++---------- 4 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 helpers/utils.js diff --git a/helpers/utils.js b/helpers/utils.js new file mode 100644 index 0000000..8c2153f --- /dev/null +++ b/helpers/utils.js @@ -0,0 +1,11 @@ +import { HomePage } from "../pages/home.page"; +/** + * Navigates to the home page and generates a random number between 0 and max (exclusive). + * @param {Object} homePage - The object representing the home page. + * @param {number} max - The upper limit for the random number. + * @returns {Promise} - A promise that resolves to a random number. + */ +export async function navigateToHomePageGenerateRandomNumber(homePage, max) { + await homePage.navigateToHomePage(); + return Math.floor(Math.random() * max); +} diff --git a/pages/home.page.js b/pages/home.page.js index 076ce42..9b17de1 100644 --- a/pages/home.page.js +++ b/pages/home.page.js @@ -1,8 +1,7 @@ export class HomePage { - constructor(page) { this.page = page; - this.homePageUrl = 'https://magento.softwaretestingboard.com/'; + this.homePageUrl = "https://magento.softwaretestingboard.com/"; } // locators @@ -12,5 +11,21 @@ export class HomePage { async navigateToHomePage() { await this.page.goto(this.homePageUrl); } -} + async getNumberOfHotSellers() { + return await this.hotSellersProducts().count(); + } + + async getNameOfHotSeller(hotSellerIndex) { + const productName = ( + await this.hotSellersProducts() + .locator(`nth=${hotSellerIndex}`) + .innerText() + ).split("\n")[0]; + return productName; + } + + async clickOnHotSellerProduct(indexOfSelector) { + await this.hotSellersProducts().locator(`nth=${indexOfSelector}`).click(); + } +} diff --git a/pages/product.details.js b/pages/product.details.js index 04d3eab..ffe174a 100644 --- a/pages/product.details.js +++ b/pages/product.details.js @@ -1,10 +1,12 @@ export class ProductDetails { + constructor(page) { + this.page = page; + } + + // locators + productName = () => this.page.locator(".base"); - constructor(page) { - this.page = page; - } - - // locators - productName = () => this.page.locator(".base"); + async getProductName() { + return await this.productName().innerText(); } - \ No newline at end of file +} diff --git a/tests/homePage/HotSellersSection.spec.js b/tests/homePage/HotSellersSection.spec.js index ae660f0..65250fb 100644 --- a/tests/homePage/HotSellersSection.spec.js +++ b/tests/homePage/HotSellersSection.spec.js @@ -1,11 +1,12 @@ import { test, expect } from "@playwright/test"; import { HomePage } from "../../pages/home.page"; import { ProductDetails } from "../../pages/product.details"; +import { navigateToHomePageGenerateRandomNumber } from "../../helpers/utils"; test("Six items displayed at hot sellers", async ({ page }) => { const homePage = new HomePage(page); await homePage.navigateToHomePage(); - expect(homePage.hotSellersProducts()).toHaveCount(6); + await expect(homePage.hotSellersProducts()).toHaveCount(6); }); test("Item redirects correctly on home page", async ({ page }) => { @@ -13,13 +14,14 @@ test("Item redirects correctly on home page", async ({ page }) => { const productDetails = new ProductDetails(page); await homePage.navigateToHomePage(); - const randomNumber = Math.floor(Math.random() * 6); - const productName = ( - await homePage - .hotSellersProducts() - .locator(`nth=${randomNumber}`) - .innerText() - ).split("\n")[0]; - await homePage.hotSellersProducts().locator(`nth=${randomNumber}`).click(); - expect(productName).toContain(await productDetails.productName().innerText()); + + const randomProductIndex = await navigateToHomePageGenerateRandomNumber( + homePage, + await homePage.getNumberOfHotSellers() + ); + + const productName = await homePage.getNameOfHotSeller(randomProductIndex); + await homePage.clickOnHotSellerProduct(randomProductIndex); + const productName2 = await productDetails.getProductName(); + expect(productName).toContain(productName2); }); From fa02234c4ba4183879adccb17a911add49f78d37 Mon Sep 17 00:00:00 2001 From: Alex Stefan Date: Sat, 10 Feb 2024 22:08:26 +0200 Subject: [PATCH 4/4] updated utils.js and HotSellersSection 2nd test --- helpers/utils.js | 3 +-- tests/homePage/HotSellersSection.spec.js | 10 ++++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/helpers/utils.js b/helpers/utils.js index 8c2153f..98524c3 100644 --- a/helpers/utils.js +++ b/helpers/utils.js @@ -5,7 +5,6 @@ import { HomePage } from "../pages/home.page"; * @param {number} max - The upper limit for the random number. * @returns {Promise} - A promise that resolves to a random number. */ -export async function navigateToHomePageGenerateRandomNumber(homePage, max) { - await homePage.navigateToHomePage(); +export async function getRandomNumber(max) { return Math.floor(Math.random() * max); } diff --git a/tests/homePage/HotSellersSection.spec.js b/tests/homePage/HotSellersSection.spec.js index 65250fb..af630d1 100644 --- a/tests/homePage/HotSellersSection.spec.js +++ b/tests/homePage/HotSellersSection.spec.js @@ -1,7 +1,7 @@ import { test, expect } from "@playwright/test"; import { HomePage } from "../../pages/home.page"; import { ProductDetails } from "../../pages/product.details"; -import { navigateToHomePageGenerateRandomNumber } from "../../helpers/utils"; +import { getRandomNumber } from "../../helpers/utils"; test("Six items displayed at hot sellers", async ({ page }) => { const homePage = new HomePage(page); @@ -14,14 +14,12 @@ test("Item redirects correctly on home page", async ({ page }) => { const productDetails = new ProductDetails(page); await homePage.navigateToHomePage(); - - const randomProductIndex = await navigateToHomePageGenerateRandomNumber( - homePage, + const randomNumber = await getRandomNumber( await homePage.getNumberOfHotSellers() ); - const productName = await homePage.getNameOfHotSeller(randomProductIndex); - await homePage.clickOnHotSellerProduct(randomProductIndex); + const productName = await homePage.getNameOfHotSeller(randomNumber); + await homePage.clickOnHotSellerProduct(randomNumber); const productName2 = await productDetails.getProductName(); expect(productName).toContain(productName2); });