|
| 1 | +package com.serenitydojo.playwright; |
| 2 | + |
| 3 | +import com.microsoft.playwright.*; |
| 4 | +import com.microsoft.playwright.assertions.PlaywrightAssertions; |
| 5 | +import com.microsoft.playwright.options.AriaRole; |
| 6 | +import com.microsoft.playwright.options.LoadState; |
| 7 | +import com.microsoft.playwright.options.SelectOption; |
| 8 | +import org.assertj.core.api.Assertions; |
| 9 | +import org.junit.jupiter.api.*; |
| 10 | + |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | +import java.util.regex.Pattern; |
| 14 | + |
| 15 | +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; |
| 16 | + |
| 17 | +public class AddingItemsToTheCartTest { |
| 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 | + browser = playwright.chromium().launch( |
| 29 | + new BrowserType.LaunchOptions() |
| 30 | + .setHeadless(false) |
| 31 | + .setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu")) |
| 32 | + ); |
| 33 | + playwright.selectors().setTestIdAttribute("data-test"); |
| 34 | + } |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setUp() { |
| 38 | + browserContext = browser.newContext(); |
| 39 | + page = browserContext.newPage(); |
| 40 | + } |
| 41 | + |
| 42 | + @AfterEach |
| 43 | + void closeContext() { |
| 44 | + browserContext.close(); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterAll |
| 48 | + static void tearDown() { |
| 49 | + browser.close(); |
| 50 | + playwright.close(); |
| 51 | + } |
| 52 | + |
| 53 | + @DisplayName("Search for pliers") |
| 54 | + @Test |
| 55 | + void searchForPliers() { |
| 56 | + page.onConsoleMessage(msg -> System.out.println(msg.text())); |
| 57 | + |
| 58 | + page.navigate("https://practicesoftwaretesting.com"); |
| 59 | + page.getByPlaceholder("Search").fill("Pliers"); |
| 60 | + page.getByPlaceholder("Search").press("Enter"); |
| 61 | + |
| 62 | + page.waitForLoadState(); |
| 63 | + page.waitForCondition( () -> page.getByTestId("product-name").count() > 0); |
| 64 | + |
| 65 | + List<String> products = page.getByTestId("product-name").allTextContents(); |
| 66 | + Assertions.assertThat(products.get(0)).containsIgnoringCase("Pliers"); |
| 67 | + |
| 68 | + assertThat(page.locator(".card")).hasCount(4); |
| 69 | + |
| 70 | + List<String> productNames = page.getByTestId("product-name").allTextContents(); |
| 71 | + Assertions.assertThat(productNames).allMatch(name -> name.contains("Pliers")); |
| 72 | + |
| 73 | + Locator outOfStockItem = page.locator(".card") |
| 74 | + .filter(new Locator.FilterOptions().setHasText("Out of stock")) |
| 75 | + .getByTestId("product-name"); |
| 76 | + |
| 77 | + assertThat(outOfStockItem).hasCount(1); |
| 78 | + assertThat(outOfStockItem).hasText("Long Nose Pliers"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @DisplayName("Chaining assertions on product search results with AssertJ") |
| 83 | + void chainedAssertionsOnProductSearchResults() { |
| 84 | + page.navigate("https://practicesoftwaretesting.com"); |
| 85 | + |
| 86 | + // Using AssertJ assertions to chain multiple checks on product search results |
| 87 | + Locator searchField = page.getByPlaceholder("Search"); |
| 88 | + searchField.fill("Pliers"); |
| 89 | + searchField.press("Enter"); |
| 90 | + |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | +} |
0 commit comments