Skip to content

Commit 32409d8

Browse files
committed
test(e2e): add cart tests
1 parent 73b7435 commit 32409d8

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

e2e/tests/public/cart.spec.ts

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
Test List
3+
- login from the sign in page redirects you page to the cart
4+
*/
5+
import { test, expect } from "../../index"
6+
import { compareFloats, getFloatValue } from "../../utils"
7+
8+
test.describe("Cart tests", async () => {
9+
test("Ensure adding multiple items from a product page adjusts the cart accordingly", async ({
10+
page,
11+
cartPage,
12+
productPage,
13+
storePage,
14+
}) => {
15+
// Assuming we have access to our page objects here
16+
const cartDropdown = cartPage.cartDropdown
17+
18+
await test.step("Navigate to the product page", async () => {
19+
await storePage.goto()
20+
const product = await storePage.getProduct("Sweatshirt")
21+
await product.locator.click()
22+
await productPage.container.waitFor({ state: "visible" })
23+
})
24+
25+
await test.step("Add the small size to the cart and verify the data", async () => {
26+
await productPage.selectOption("S")
27+
await productPage.addProductButton.click()
28+
await expect(cartDropdown.navCartLink).toContainText("(1)")
29+
const cartItem = await cartDropdown.getCartItem("Sweatshirt", "S")
30+
await expect(cartItem.locator).toBeVisible()
31+
await expect(cartItem.variant).toContainText("S")
32+
await expect(cartItem.quantity).toContainText("1")
33+
await cartDropdown.goToCartButton.click()
34+
await cartDropdown.close()
35+
await cartPage.container.waitFor({ state: "visible" })
36+
const productInCart = await cartPage.getProduct("Sweatshirt", "S")
37+
await expect(productInCart.productRow).toBeVisible()
38+
await expect(productInCart.quantitySelect).toHaveValue("1")
39+
await page.goBack()
40+
await productPage.container.waitFor({ state: "visible" })
41+
})
42+
43+
await test.step("Add the small size to the cart again and verify the data", async () => {
44+
await productPage.selectOption("S")
45+
await productPage.addProductButton.click()
46+
await expect(cartDropdown.navCartLink).toContainText("(2)")
47+
const cartItem = await cartDropdown.getCartItem("Sweatshirt", "S")
48+
await expect(cartItem.locator).toBeVisible()
49+
await expect(cartItem.variant).toContainText("S")
50+
await expect(cartItem.quantity).toContainText("2")
51+
await cartDropdown.goToCartButton.click()
52+
await cartDropdown.close()
53+
await cartPage.container.waitFor({ state: "visible" })
54+
const productInCart = await cartPage.getProduct("Sweatshirt", "S")
55+
await expect(productInCart.productRow).toBeVisible()
56+
await expect(productInCart.quantitySelect).toHaveValue("2")
57+
await page.goBack()
58+
await productPage.container.waitFor({ state: "visible" })
59+
})
60+
61+
await test.step("Add the medium size to the cart and verify the data", async () => {
62+
await productPage.selectOption("M")
63+
await productPage.addProductButton.click()
64+
await expect(cartDropdown.navCartLink).toContainText("(3)")
65+
const mediumCartItem = await cartDropdown.getCartItem("Sweatshirt", "M")
66+
await expect(mediumCartItem.locator).toBeVisible()
67+
await expect(mediumCartItem.variant).toContainText("M")
68+
await expect(mediumCartItem.quantity).toContainText("1")
69+
await cartDropdown.goToCartButton.click()
70+
await cartDropdown.close()
71+
await cartPage.container.waitFor({ state: "visible" })
72+
const mediumProductInCart = await cartPage.getProduct("Sweatshirt", "M")
73+
await expect(mediumProductInCart.productRow).toBeVisible()
74+
await expect(mediumProductInCart.quantitySelect).toHaveValue("1")
75+
const smallProductInCart = await cartPage.getProduct("Sweatshirt", "S")
76+
await expect(smallProductInCart.productRow).toBeVisible()
77+
await expect(smallProductInCart.quantitySelect).toHaveValue("2")
78+
})
79+
})
80+
81+
test("Ensure adding two products into the cart and verify the quantities", async ({
82+
cartPage,
83+
productPage,
84+
storePage,
85+
}) => {
86+
const cartDropdown = cartPage.cartDropdown
87+
88+
await test.step("Navigate to the product page - go to the store page and click on the Sweatshirt product", async () => {
89+
await storePage.goto()
90+
const product = await storePage.getProduct("Sweatshirt")
91+
await product.locator.click()
92+
await productPage.container.waitFor({ state: "visible" })
93+
})
94+
95+
await test.step("Add the small sweatshirt to the cart", async () => {
96+
await productPage.selectOption("S")
97+
await productPage.addProductButton.click()
98+
await expect(cartDropdown.navCartLink).toContainText("(1)")
99+
const sweatshirtItem = await cartDropdown.getCartItem("Sweatshirt", "S")
100+
await expect(sweatshirtItem.locator).toBeVisible()
101+
await expect(sweatshirtItem.variant).toHaveText("Variant: S")
102+
await expect(sweatshirtItem.quantity).toContainText("1")
103+
await cartDropdown.close()
104+
})
105+
106+
await test.step("Navigate to another product - Sweatpants", async () => {
107+
await storePage.goto()
108+
const product = await storePage.getProduct("Sweatpants")
109+
await product.locator.click()
110+
await productPage.container.waitFor({ state: "visible" })
111+
})
112+
113+
await test.step("Add the small sweatpants to the cart", async () => {
114+
await productPage.selectOption("S")
115+
await productPage.addProductButton.click()
116+
await expect(cartDropdown.navCartLink).toContainText("(2)")
117+
const sweatpantsItem = await cartDropdown.getCartItem("Sweatpants", "S")
118+
await expect(sweatpantsItem.locator).toBeVisible()
119+
await expect(sweatpantsItem.variant).toHaveText("Variant: S")
120+
await expect(sweatpantsItem.quantity).toContainText("1")
121+
const sweatshirtItem = await cartDropdown.getCartItem("Sweatshirt", "S")
122+
await expect(sweatshirtItem.locator).toBeVisible()
123+
await expect(sweatshirtItem.quantity).toContainText("1")
124+
await cartDropdown.goToCartButton.click()
125+
await cartDropdown.close()
126+
await cartPage.container.waitFor({ state: "visible" })
127+
})
128+
129+
await test.step("Verify the quantities in the cart", async () => {
130+
const sweatpantsProduct = await cartPage.getProduct("Sweatpants", "S")
131+
await expect(sweatpantsProduct.productRow).toBeVisible()
132+
await expect(sweatpantsProduct.quantitySelect).toHaveValue("1")
133+
const sweatshirtProduct = await cartPage.getProduct("Sweatshirt", "S")
134+
await expect(sweatshirtProduct.productRow).toBeVisible()
135+
await expect(sweatshirtProduct.quantitySelect).toHaveValue("1")
136+
})
137+
})
138+
139+
test("Verify the prices carries over to checkout", async ({
140+
cartPage,
141+
productPage,
142+
storePage,
143+
}) => {
144+
await test.step("Navigate to the product page - go to the store page and click on the Hoodie product", async () => {
145+
await storePage.goto()
146+
const product = await storePage.getProduct("Hoodie")
147+
await product.locator.click()
148+
await productPage.container.waitFor({ state: "visible" })
149+
})
150+
151+
let hoodieSmallPrice = 0
152+
let hoodieMediumPrice = 0
153+
await test.step("Add the hoodie to the cart", async () => {
154+
await productPage.selectOption("S")
155+
hoodieSmallPrice = getFloatValue(
156+
(await productPage.productPrice.getAttribute("data-value")) || "0"
157+
)
158+
await productPage.clickAddProduct()
159+
await productPage.cartDropdown.close()
160+
await productPage.selectOption("M")
161+
hoodieMediumPrice = getFloatValue(
162+
(await productPage.productPrice.getAttribute("data-value")) || "0"
163+
)
164+
await productPage.clickAddProduct()
165+
166+
await productPage.cartDropdown.close()
167+
})
168+
169+
await test.step("Navigate to another product - Longsleeve", async () => {
170+
await storePage.goto()
171+
const product = await storePage.getProduct("Longsleeve")
172+
await product.locator.click()
173+
await productPage.container.waitFor({ state: "visible" })
174+
})
175+
176+
let longsleeveSmallPrice = 0
177+
await test.step("Add the small longsleeve to the cart", async () => {
178+
await productPage.selectOption("S")
179+
longsleeveSmallPrice = getFloatValue(
180+
(await productPage.productPrice.getAttribute("data-value")) || "0"
181+
)
182+
await productPage.clickAddProduct()
183+
await productPage.cartDropdown.close()
184+
await productPage.selectOption("S")
185+
await productPage.clickAddProduct()
186+
await productPage.selectOption("S")
187+
await productPage.clickAddProduct()
188+
await productPage.cartDropdown.goToCartButton.click()
189+
await productPage.cartDropdown.close()
190+
await cartPage.container.waitFor({ state: "visible" })
191+
})
192+
193+
await test.step("Verify the price in the cart is the expected value", async () => {
194+
const total = getFloatValue(
195+
(await cartPage.cartSubtotal.getAttribute("data-value")) || "0"
196+
)
197+
const calculatedTotal =
198+
3 * longsleeveSmallPrice + hoodieSmallPrice + hoodieMediumPrice
199+
expect(compareFloats(total, calculatedTotal)).toBe(0)
200+
})
201+
})
202+
})

e2e/utils/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function getFloatValue(s: string) {
2+
return parseFloat(parseFloat(s).toFixed(2))
3+
}
4+
5+
export function compareFloats(f1: number, f2: number) {
6+
const diff = f1 - f2
7+
if (Math.abs(diff) < 0.01) {
8+
return 0
9+
} else if (diff < 0) {
10+
return -1
11+
} else {
12+
return 1
13+
}
14+
}

0 commit comments

Comments
 (0)