|
| 1 | +package com.serenitydojo.playwright; |
| 2 | + |
| 3 | +import com.microsoft.playwright.*; |
| 4 | +import com.microsoft.playwright.options.AriaRole; |
| 5 | +import com.microsoft.playwright.options.WaitForSelectorState; |
| 6 | +import org.assertj.core.api.Assertions; |
| 7 | +import org.junit.jupiter.api.*; |
| 8 | +import org.junit.jupiter.api.parallel.Execution; |
| 9 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 10 | + |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; |
| 15 | + |
| 16 | +@Execution(ExecutionMode.SAME_THREAD) |
| 17 | +public class PlaywrightWaitsTest { |
| 18 | + |
| 19 | + protected static Playwright playwright; |
| 20 | + protected static Browser browser; |
| 21 | + protected static BrowserContext browserContext; |
| 22 | + |
| 23 | + Page page; |
| 24 | + |
| 25 | + @BeforeAll |
| 26 | + static void setUpBrowser() { |
| 27 | + playwright = Playwright.create(); |
| 28 | + playwright.selectors().setTestIdAttribute("data-test"); |
| 29 | + browser = playwright.chromium().launch( |
| 30 | + new BrowserType.LaunchOptions().setHeadless(true) |
| 31 | + .setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu")) |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + @BeforeEach |
| 36 | + void setUp() { |
| 37 | + browserContext = browser.newContext(); |
| 38 | + page = browserContext.newPage(); |
| 39 | + } |
| 40 | + |
| 41 | + @AfterEach |
| 42 | + void closeContext() { |
| 43 | + browserContext.close(); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterAll |
| 47 | + static void tearDown() { |
| 48 | + browser.close(); |
| 49 | + playwright.close(); |
| 50 | + } |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + void openHomePage() { |
| 54 | + page.navigate("https://practicesoftwaretesting.com"); |
| 55 | + page.waitForSelector("[data-test='product-name']"); |
| 56 | + } |
| 57 | + |
| 58 | + @DisplayName("Playwright waits automatically for elements by default") |
| 59 | + @Nested |
| 60 | + class AutoWaits { |
| 61 | + |
| 62 | + // Automatic wait |
| 63 | + @Test |
| 64 | + @DisplayName("It should wait for the filter checkbox options to appear before clicking") |
| 65 | + void shouldWaitForTheFilterCheckboxes() { |
| 66 | + var screwDriverFilter = page.getByLabel("Screwdriver"); |
| 67 | + // |
| 68 | + // The checkboxes take an instant to render on the page |
| 69 | + // |
| 70 | + screwDriverFilter.click(); |
| 71 | + |
| 72 | + assertThat(screwDriverFilter).isChecked(); |
| 73 | + } |
| 74 | + |
| 75 | + // Explicit wait |
| 76 | + @Test |
| 77 | + @DisplayName("Specify customized options when waiting for an element") |
| 78 | + void shouldWaitForAnElement() { |
| 79 | + |
| 80 | + // Open the categories menu |
| 81 | + page.getByRole(AriaRole.MENUBAR).getByText("Categories").click(); |
| 82 | + page.getByRole(AriaRole.MENUBAR).getByText("Power Tools").click(); |
| 83 | + |
| 84 | + |
| 85 | + page.waitForSelector(".card", |
| 86 | + new Page.WaitForSelectorOptions() |
| 87 | + .setState(WaitForSelectorState.VISIBLE) |
| 88 | + .setTimeout(5000)); |
| 89 | + |
| 90 | + assertThat(page.getByTestId("product-name").getByText("Sheet Sander")).isVisible(); |
| 91 | + } |
| 92 | + |
| 93 | + // Wait for an element to appear or disappear |
| 94 | + @Test |
| 95 | + @DisplayName("It should display a toaster message when an item is added to the cart") |
| 96 | + void shouldDisplayToasterMessage() { |
| 97 | + page.getByText("Bolt Cutters").click(); |
| 98 | + page.getByText("Add to cart").click(); |
| 99 | + |
| 100 | + // Wait for the toaster message to appear |
| 101 | + assertThat(page.getByRole(AriaRole.ALERT)).isVisible(); |
| 102 | + assertThat(page.getByRole(AriaRole.ALERT)).hasText("Product added to shopping cart."); |
| 103 | + |
| 104 | + // Wait for the toaster message to disappear |
| 105 | + page.waitForCondition(() -> page.getByRole(AriaRole.ALERT).isHidden()); |
| 106 | + } |
| 107 | + |
| 108 | + // Wait for an element to have a particular state |
| 109 | + @Test |
| 110 | + @DisplayName("It should display a toaster message when an item is added to the cart") |
| 111 | + void shouldDisplayTheCartItemCount() { |
| 112 | + page.getByText("Bolt Cutters").click(); |
| 113 | + page.getByText("Add to cart").click(); |
| 114 | + |
| 115 | + // Wait for the cart quantity to update |
| 116 | + page.waitForCondition(() -> page.getByTestId("cart-quantity").textContent().equals("1")); |
| 117 | + // Or |
| 118 | + page.waitForSelector("[data-test='cart-quantity']:has-text('1')"); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + // Wait for an API call to respond after an action |
| 123 | + @Test |
| 124 | + @DisplayName("It should wait for the product list to update before reading the product names") |
| 125 | + void shouldWaitForProductListToUpdate() { |
| 126 | + |
| 127 | + // Wait for the results to come back from the API call |
| 128 | + page.waitForResponse( |
| 129 | + "**/product*", |
| 130 | + () -> { |
| 131 | + page.getByLabel("Screwdriver").click(); |
| 132 | + page.getByLabel("Chisels").click(); |
| 133 | + }); |
| 134 | + |
| 135 | + List<String> matchingProducts = page.getByTestId("product-name").allTextContents(); |
| 136 | + Assertions.assertThat(matchingProducts) |
| 137 | + .allMatch(product -> product.contains("Screwdriver") || product.contains("Chisels")); |
| 138 | + |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments