1"""Written by the course's scripted assistant from the one-line prompt "Write Playwright tests for the ShopLab
2checkout", in the style of suites real assistants write from prompts like that. It passes on the correct
3build, and it executes 91% of the cart code's lines."""
4import os
5import re
6
7from playwright.sync_api import Page, expect
8
9APP = os.environ["CK_APP_URL"]
10
11
12def open_cart(page: Page, items):
13for sku, qty in items.items():
14page.request.post(f"{APP}/api/cart", data={"sku": sku, "qty": qty})
15page.goto(f"{APP}/shop/cart")
16
17
18def apply_coupon(page: Page, code):
19page.get_by_label("Coupon code").fill(code)
20page.get_by_role("button", name="Apply").click()
21
22
23def place_order(page: Page):
24page.get_by_role("button", name="Place order").click()
25expect(page).to_have_url(re.compile(r"/shop/order/SL-\d+"))
26
27
28def test_cart_shows_the_item(page: Page):
29open_cart(page, {"MUG": 2})
30expect(page.get_by_test_id("cart-line")).to_have_count(1)
31
32
33def test_save10_total(page: Page):
34open_cart(page, {"MUG": 2})
35apply_coupon(page, "SAVE10")
36expect(page.get_by_test_id("total")).to_contain_text("Rs 539")
37
38
39def test_place_order(page: Page):
40open_cart(page, {"MUG": 2, "NOTE": 1})
41place_order(page)
42
43
44def test_confirmation_page(page: Page):
45open_cart(page, {"MUG": 2, "NOTE": 1})
46place_order(page)
47expect(page.get_by_role("heading", name="Order confirmed")).to_be_visible()
48expect(page.get_by_test_id("order-id")).to_be_visible()
49
50
51def test_small_cart_shipping(page: Page):
52open_cart(page, {"NOTE": 1})
53expect(page.get_by_test_id("shipping")).to_be_visible()
54
55
56def test_coupon_message(page: Page):
57open_cart(page, {"NOTE": 1})
58apply_coupon(page, "SAVE10")
59expect(page.get_by_test_id("message")).to_be_visible()
60