Skip to content

Commit ab8219d

Browse files
committed
fix: refactor cart operations and improve assertions in add to cart tests
1 parent 2620d16 commit ab8219d

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

tests_e2e/tests/test_e2e_category_page_add_product_to_cart.py

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,39 +42,40 @@ def test_e2e_category_add_product_to_cart_with_add_to_cart_button(
4242
category_page = CategoryPage(config, page, category["seoInfos"][0]["semanticUrl"])
4343
category_page.navigate()
4444

45-
cart = cart_operations.get_cart(
46-
store_id=config["STORE_ID"],
47-
user_id=user["id"],
48-
currency_code="USD",
49-
culture_name="en-US",
50-
)
51-
5245
product_card = category_page.get_product_card_by_sku(product["code"])
5346
product_card.add_to_cart_component.quantity_input.fill(quantity_to_add)
5447
product_card.add_to_cart_component.add_to_cart_text_button.click()
5548

56-
expect(product_card.count_in_cart_label).to_be_visible(), "Count in cart label is not visible"
57-
expect(product_card.count_in_cart_label).to_have_text(
58-
quantity_to_add
59-
), "Count in cart label is not equal to product quantity to add"
49+
expect(product_card.count_in_cart_label, "Count in cart label is not visible").to_be_visible()
50+
expect(
51+
product_card.count_in_cart_label, "Count in cart label is not equal to product quantity to add"
52+
).to_have_text(quantity_to_add)
6053

6154
cart_page = CartPage(config, page)
6255
cart_page.navigate()
6356
page.wait_for_load_state("networkidle")
6457

65-
line_item = cart_page.get_line_item_by_sku(product["code"])
58+
cart = cart_operations.get_cart(
59+
store_id=config["STORE_ID"],
60+
user_id=user["id"],
61+
currency_code="USD",
62+
culture_name="en-US",
63+
)
6664

67-
assert line_item.sku == product["code"], f"Line item sku is not equal to product sku: {product["code"]}"
68-
assert (
69-
line_item.add_to_cart_component.quantity_input.input_value() == quantity_to_add
70-
), f"Line item quantity is not equal to product quantity to add: {quantity_to_add}"
65+
try:
66+
line_item = cart_page.get_line_item_by_sku(product["code"])
7167

72-
cart_operations.remove_cart(
73-
payload={
74-
"cartId": cart["id"],
75-
"userId": user["id"],
76-
}
77-
)
68+
assert line_item.sku == product["code"], f"Line item sku is not equal to product sku: {product['code']}"
69+
assert (
70+
line_item.add_to_cart_component.quantity_input.input_value() == quantity_to_add
71+
), f"Line item quantity is not equal to product quantity to add: {quantity_to_add}"
72+
finally:
73+
cart_operations.remove_cart(
74+
payload={
75+
"cartId": cart["id"],
76+
"userId": user["id"],
77+
}
78+
)
7879

7980

8081
@pytest.mark.e2e
@@ -113,13 +114,14 @@ def test_e2e_category_add_product_to_cart_with_quantity_stepper(
113114
product_card.quantity_stepper_component.increment_button.click()
114115
product_card.quantity_stepper_component.increment_button.click()
115116

116-
expect(product_card.quantity_stepper_component.quantity_input).to_have_value(
117-
str(quantity_to_add)
118-
), f"Quantity input is not equal to {quantity_to_add}"
119-
expect(product_card.count_in_cart_label).to_be_visible(), "Count in cart label is not visible"
120-
expect(product_card.count_in_cart_label).to_have_text(
121-
str(quantity_to_add)
122-
), "Count in cart label is not equal to product quantity to add"
117+
expect(
118+
product_card.quantity_stepper_component.quantity_input,
119+
f"Quantity input is not equal to {quantity_to_add}",
120+
).to_have_value(str(quantity_to_add))
121+
expect(product_card.count_in_cart_label, "Count in cart label is not visible").to_be_visible()
122+
expect(
123+
product_card.count_in_cart_label, "Count in cart label is not equal to product quantity to add"
124+
).to_have_text(str(quantity_to_add))
123125

124126
cart_page = CartPage(config, page)
125127
cart_page.navigate()
@@ -132,16 +134,18 @@ def test_e2e_category_add_product_to_cart_with_quantity_stepper(
132134
culture_name="en-US",
133135
)
134136

135-
line_item = cart_page.get_line_item_by_sku(product["code"])
136-
137-
assert line_item.sku == product["code"], f"Line item sku is not equal to product sku: {product["code"]}"
138-
expect(line_item.quantity_stepper_component.quantity_input).to_have_value(
139-
str(quantity_to_add)
140-
), f"Line item quantity is not equal to product quantity to add: {quantity_to_add}"
141-
142-
cart_operations.remove_cart(
143-
payload={
144-
"cartId": cart["id"],
145-
"userId": user["id"],
146-
}
147-
)
137+
try:
138+
line_item = cart_page.get_line_item_by_sku(product["code"])
139+
140+
assert line_item.sku == product["code"], f"Line item sku is not equal to product sku: {product['code']}"
141+
expect(
142+
line_item.quantity_stepper_component.quantity_input,
143+
f"Line item quantity is not equal to product quantity to add: {quantity_to_add}",
144+
).to_have_value(str(quantity_to_add))
145+
finally:
146+
cart_operations.remove_cart(
147+
payload={
148+
"cartId": cart["id"],
149+
"userId": user["id"],
150+
}
151+
)

0 commit comments

Comments
 (0)