|
| 1 | +package com.serenitydojo.playwright.toolshop.e2e; |
| 2 | + |
| 3 | +import com.microsoft.playwright.Locator; |
| 4 | +import com.microsoft.playwright.Page; |
| 5 | +import com.microsoft.playwright.options.AriaRole; |
| 6 | +import com.serenitydojo.playwright.toolshop.fixtures.PlaywrightTestCase; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import static org.assertj.core.api.Assertions.assertThat; |
| 12 | + |
| 13 | +class PurchaseAProductTest extends PlaywrightTestCase { |
| 14 | + |
| 15 | + record CartItem(String productTitle, int quantity, double amount, double total) {} |
| 16 | + |
| 17 | + // Customers should be able to add items to the shopping cart |
| 18 | + @Test |
| 19 | + void addToShoppingCart() { |
| 20 | + page.navigate("https://practicesoftwaretesting.com"); |
| 21 | + // Search for claw hammer |
| 22 | + page.getByPlaceholder("Search").fill("claw hammer"); |
| 23 | + page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Search")).click(); |
| 24 | + |
| 25 | + // View product details |
| 26 | + page.getByTestId("product-name") |
| 27 | + .filter(new Locator.FilterOptions().setHasText("Claw Hammer with Shock Reduction Grip")) |
| 28 | + .click(); |
| 29 | + double price = Double.parseDouble(page.getByTestId("unit-price").textContent()); |
| 30 | + |
| 31 | + // Add two claw hammers to the cart |
| 32 | + page.getByTestId("increase-quantity").click(); |
| 33 | + page.getByTestId("add-to-cart").click(); |
| 34 | + page.getByRole(AriaRole.ALERT).click(); |
| 35 | + page.getByTestId("nav-cart").click(); |
| 36 | + |
| 37 | + // Wait for the cart rows to appear |
| 38 | + page.locator("app-cart tbody tr").waitFor(); |
| 39 | + |
| 40 | + // Check that the cart contains the right elements |
| 41 | + List<CartItem> displayedCartItems = page.locator("app-cart tbody tr") |
| 42 | + .all() |
| 43 | + .stream() |
| 44 | + .map( |
| 45 | + row -> { |
| 46 | + String productTitle = stripWhiteSpace(row.getByTestId("product-title").textContent()); |
| 47 | + int quantity = Integer.parseInt(row.getByTestId("product-quantity").inputValue()); |
| 48 | + double amount = Double.parseDouble(priceFrom(row.getByTestId("product-price").textContent())); |
| 49 | + double total = Double.parseDouble(priceFrom(row.getByTestId("line-price").textContent())); |
| 50 | + return new CartItem(productTitle, quantity, amount, total); |
| 51 | + } |
| 52 | + ).toList(); |
| 53 | + |
| 54 | + System.out.println(displayedCartItems); |
| 55 | + // Check that the total price is correct |
| 56 | + |
| 57 | + assertThat(displayedCartItems).hasSize(1); |
| 58 | + assertThat(displayedCartItems.get(0)) |
| 59 | + .isEqualTo(new CartItem("Claw Hammer with Shock Reduction Grip",2,13.41,26.82)); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private String stripWhiteSpace(String text) { |
| 64 | + return text.strip().replace("\u00A0",""); |
| 65 | + } |
| 66 | + |
| 67 | + private String priceFrom(String value) { |
| 68 | + return value.replace("$", "").replace(",", "."); |
| 69 | + } |
| 70 | +} |
0 commit comments