Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions helpers/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotations says you take two params, but function takes only one

* @param {number} max - The upper limit for the random number.
* @returns {Promise<number>} - A promise that resolves to a random number.
*/
export async function getRandomNumber(max) {
return Math.floor(Math.random() * max);
}
24 changes: 20 additions & 4 deletions pages/home.page.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
export class HomePage {

constructor(page) {
this.page = page;
this.homePageUrl = 'https://magento.softwaretestingboard.com/';
this.homePageUrl = "https://magento.softwaretestingboard.com/";
}

// 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);
}
}

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();
}
}
12 changes: 12 additions & 0 deletions pages/product.details.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class ProductDetails {
constructor(page) {
this.page = page;
}

// locators
productName = () => this.page.locator(".base");

async getProductName() {
return await this.productName().innerText();
}
}
45 changes: 18 additions & 27 deletions tests/homePage/HotSellersSection.spec.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
import { test, expect } from "@playwright/test";
import { HomePage } from "../../pages/home.page";
import { ProductDetails } from "../../pages/product.details";
import { getRandomNumber } from "../../helpers/utils";

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();
await expect(homePage.hotSellersProducts()).toHaveCount(6);
});

test("Item redirects correctly on home page", async ({ page }) => {
await page.goto("https://magento.softwaretestingboard.com/");

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/");
}
const homePage = new HomePage(page);
const productDetails = new ProductDetails(page);

await homePage.navigateToHomePage();
const randomNumber = await getRandomNumber(
await homePage.getNumberOfHotSellers()
);

const productName = await homePage.getNameOfHotSeller(randomNumber);
await homePage.clickOnHotSellerProduct(randomNumber);
const productName2 = await productDetails.getProductName();
expect(productName).toContain(productName2);
});