A typical e-commerce checkout test can use expectations
at every important business step.
from playwright.sync_api import expect
def test_checkout(page):
# Open application
page.goto("https://shop.example.com")
# Verify home page
expect(page).to_have_title(
"Online Shopping"
)
# Search product
page.get_by_placeholder(
"Search products"
).fill("Laptop")
page.get_by_role(
"button",
name="Search"
).click()
# Verify search results
expect(
page.get_by_text("Laptop")
).to_be_visible()
# Select product
page.get_by_text(
"Laptop Pro"
).click()
# Verify product page
expect(
page.get_by_role(
"heading",
name="Laptop Pro"
)
).to_be_visible()
# Add product
page.get_by_role(
"button",
name="Add to Cart"
).click()
# Verify cart
expect(
page.get_by_text("Added to cart")
).to_be_visible()
# Checkout
page.get_by_role(
"link",
name="Checkout"
).click()
# Verify checkout page
expect(page).to_have_url(
"https://shop.example.com/checkout"
)
# Place order
page.get_by_role(
"button",
name="Place Order"
).click()
# Final business validation
expect(
page.get_by_text(
"Order placed successfully"
)
).to_be_visible()
expect(
page.get_by_test_id("order-status")
).to_have_text("Confirmed")
Key idea:
A good Playwright test does not merely perform actions.
It performs an action and then verifies the expected
business outcome.
Action → Expectation → Action → Expectation