Skip to content

Commit b34587c

Browse files
committed
implement adding single item to the cart
1 parent 04f7534 commit b34587c

File tree

6 files changed

+85
-24
lines changed

6 files changed

+85
-24
lines changed

fixtures/pageFixtures.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { test as base, Page } from "@playwright/test";
22
import { BasePage } from "../pages/BasePage";
33
import { LoginPage } from "../pages/LoginPage";
44
import { RegistrationPage } from "../pages/RegistrationPage";
5+
import { ProductPage } from "../pages/ProductPage";
56

67
type MyFixtures = {
78
basePage: BasePage;
89
loginPage: LoginPage;
910
registrationPage: RegistrationPage;
11+
productPage: ProductPage;
1012
//add more types here
1113
};
1214

@@ -20,5 +22,8 @@ export const test = base.extend<MyFixtures>({
2022
registrationPage: async ({ page }, use) => {
2123
await use(new RegistrationPage(page));
2224
},
25+
productPage: async ({ page }, use) => {
26+
await use(new ProductPage(page));
27+
},
2328
//define others similarly
2429
});

pages/BasePage.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class BasePage{
88
readonly contactLink:Locator;
99
readonly categoriesLink:Locator;
1010
readonly categoriesDropdown:Locator;
11+
readonly filters:Locator;
1112

1213
constructor(page:Page) {
1314
this.page = page;
@@ -16,7 +17,8 @@ export class BasePage{
1617
this.contactLink = page.locator("//a[@data-test='nav-contact']");
1718
this.categoriesLink = page.locator("//a[@data-test='nav-categories']");
1819
this.categoriesDropdown = page.locator("//ul[@aria-label='nav-categories']//a");
19-
20+
this.filters = page.locator("//div[@data-test='filters']"); //filter section on home page
21+
2022
}
2123

2224
async navigateToHome(){
@@ -36,4 +38,13 @@ export class BasePage{
3638
const categoryOption = this.page.locator(`//ul[@aria-label='nav-categories']//a[text()='${categoryName}']`);
3739
await categoryOption.click();
3840
}
41+
42+
async verifyOnHomePage(){
43+
await expect(this.filters).toBeVisible();
44+
}
45+
46+
async clickProduct(itemName:string){
47+
const itemLocator = this.page.locator(`//a//h5[@data-test='product-name' and normalize-space(text())='${itemName}']`);
48+
itemLocator.click();
49+
}
3950
}

pages/ProductPage.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Page, Locator, expect } from "@playwright/test";
2+
3+
export class ProductPage {
4+
readonly page: Page;
5+
readonly productTitle: Locator;
6+
readonly addToCartButton: Locator;
7+
readonly itemQuantity: Locator;
8+
readonly cartIconQuantity: Locator;
9+
10+
constructor(page: Page) {
11+
this.page = page;
12+
this.productTitle = page.locator("h1[data-test='product-name']");
13+
this.addToCartButton = page.locator("button[data-test='add-to-cart']");
14+
this.itemQuantity = page.locator("//input[@data-test='quantity']");
15+
this.cartIconQuantity = page.locator("//a[@data-test='nav-cart']//span");
16+
}
17+
async verifyProductPage(productName: string) {
18+
await expect(this.productTitle).toHaveText(productName);
19+
}
20+
21+
async addToCart() {
22+
await this.addToCartButton.click();
23+
}
24+
25+
async setItemQuantity(quantity: number) {
26+
await this.itemQuantity.fill(quantity.toString());
27+
}
28+
29+
async getCartIconQuantity(): Promise<number> {
30+
const quantityText = await this.cartIconQuantity.textContent();
31+
return parseInt(quantityText || "0", 10);
32+
}
33+
34+
async verifyCartIconQuantity(expectedQuantity: number) {
35+
const actualQuantity = await this.getCartIconQuantity();
36+
expect(actualQuantity).toBe(expectedQuantity);
37+
}
38+
39+
}

tests/features/add-to-cart.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Feature: Add items to shopping cart
1010

1111
Scenario: Add single product to the cart
1212
When the user selects a product "Thor Hammer"
13-
And the user clicks the "Add to Cart" button
14-
Then the shopping cart should contain 1 item
15-
And the cart total should update accordingly
13+
And the user clicks add to cart button
14+
Then the shopping cart icon should contain 1 item
15+
1616

1717
# Scenario: Add multiple products to the cart
1818
# When the user selects a product "Laptop"

tests/steps/add-to-cart.steps.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
import settings from "../config/settings.json";
21
import { Given, When, Then } from "@cucumber/cucumber";
32

4-
Given('the user is on the homepage', async function () {
5-
await this.basePage.navigateToHome(); //TODO verify home, no navigation
6-
});
7-
8-
When('the user selects a product {string}', async function (string) {
9-
10-
});
11-
12-
When('the user clicks the {string} button', async function (string) {
13-
14-
});
15-
16-
Then('the shopping cart should contain {int} item', async function (int) {
17-
18-
});
19-
20-
Then('the cart total should update accordingly', async function () {
21-
22-
});
3+
Given("the user is on the homepage", async function () {
4+
await this.basePage.navigateToHome();
5+
await this.basePage.verifyOnHomePage();
6+
});
7+
8+
When("the user selects a product {string}", async function (itemName: string) {
9+
this.basePage.clickProduct(itemName);
10+
});
11+
12+
When("the user clicks add to cart button", async function () {
13+
await this.productPage.addToCart();
14+
});
15+
16+
Then(
17+
"the shopping cart icon should contain {int} item",
18+
async function (quantity: number) {
19+
await this.productPage.verifyCartIconQuantity(quantity);
20+
}
21+
);

tests/support/world.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@ import { setWorldConstructor, World } from "@cucumber/cucumber";
22
import { Browser, Page } from "playwright";
33
import { LoginPage } from "../../pages/LoginPage";
44
import {RegistrationPage} from "../../pages/RegistrationPage";
5+
import { BasePage } from "../../pages/BasePage";
6+
import { ProductPage } from "../../pages/ProductPage";
57

68
export class CustomWorld extends World {
79
browser!: Browser;
810
page!: Page;
911
loginPage!: LoginPage;
1012
registrationPage!:RegistrationPage;
13+
basePage!: BasePage;
14+
productPage!: ProductPage;
15+
// Add other page objects as needed
1116

1217
async initPageObjects() {
1318
this.loginPage = new LoginPage(this.page);
1419
this.registrationPage = new RegistrationPage(this.page);
20+
this.basePage = new BasePage(this.page);
21+
this.productPage = new ProductPage(this.page);
1522
// Initialize other page objects similarly
1623
}
1724
}

0 commit comments

Comments
 (0)