|
1 | 1 | package com.serenitydojo.playwright; |
2 | 2 |
|
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonObject; |
3 | 5 | import com.microsoft.playwright.*; |
4 | | -import com.microsoft.playwright.options.LoadState; |
| 6 | +import com.microsoft.playwright.options.AriaRole; |
| 7 | +import com.microsoft.playwright.options.RequestOptions; |
5 | 8 | import org.assertj.core.api.Assertions; |
6 | 9 | import org.junit.jupiter.api.*; |
7 | 10 | import org.junit.jupiter.api.parallel.Execution; |
8 | 11 | import org.junit.jupiter.api.parallel.ExecutionMode; |
9 | 12 |
|
10 | 13 | import java.util.Arrays; |
11 | | -import java.util.Comparator; |
12 | | -import java.util.List; |
13 | 14 |
|
14 | 15 | import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; |
15 | 16 |
|
16 | 17 | @Execution(ExecutionMode.SAME_THREAD) |
17 | | -public class PlaywrightWaitsTest { |
| 18 | +public class PlaywrightRestAPITest { |
18 | 19 |
|
19 | 20 | protected static Playwright playwright; |
20 | 21 | protected static Browser browser; |
@@ -51,41 +52,52 @@ static void tearDown() { |
51 | 52 |
|
52 | 53 | @BeforeEach |
53 | 54 | void openHomePage() { |
| 55 | + page.route("**/products/search?q=pliers", |
| 56 | + route -> route.fulfill(new Route.FulfillOptions() |
| 57 | + .setBody("{\"message\": \"Internal Server Error\"}") |
| 58 | + .setStatus(404)) |
| 59 | + ); |
54 | 60 | page.navigate("https://practicesoftwaretesting.com"); |
55 | | - page.waitForCondition(() -> page.getByTestId("product-name").count() > 0); |
56 | 61 | } |
57 | 62 |
|
58 | | - @DisplayName("Playwright waits automatically for elements by default") |
| 63 | + @DisplayName("Playwright allows us to mock out API responses") |
59 | 64 | @Nested |
60 | | - class AutoWaits { |
| 65 | + class MockingAPIResponses { |
61 | 66 |
|
62 | 67 | @Test |
63 | | - @DisplayName("It should wait for the filter checkbox options to appear before clicking") |
64 | | - void shouldWaitForTheFilterCheckboxes() { |
65 | | - var screwDriverFilter = page.getByLabel("Screwdriver"); |
66 | | - // |
67 | | - // The checkboxes take an instant to render on the page |
68 | | - // |
69 | | - screwDriverFilter.click(); |
70 | | - |
71 | | - assertThat(screwDriverFilter).isChecked(); |
| 68 | + @DisplayName("When a search returns a single product") |
| 69 | + void whenASingleItemIsFound() { |
| 70 | + page.route("**/products/search?q=pliers", |
| 71 | + route -> route.fulfill(new Route.FulfillOptions() |
| 72 | + .setBody(MockSearchResponses.RESPONSE_WITH_A_SINGLE_ENTRY) |
| 73 | + .setStatus(200)) |
| 74 | + ); |
| 75 | + |
| 76 | + page.navigate("https://practicesoftwaretesting.com"); |
| 77 | + page.getByPlaceholder("Search").fill("pliers"); |
| 78 | + page.getByPlaceholder("Search").press("Enter"); |
| 79 | + |
| 80 | + assertThat(page.getByTestId("product-name")).hasCount(1); |
| 81 | + assertThat(page.getByTestId("product-name") |
| 82 | + .filter(new Locator.FilterOptions().setHasText("Super Pliers"))) |
| 83 | + .isVisible(); |
72 | 84 | } |
73 | 85 |
|
74 | 86 | @Test |
75 | | - @DisplayName("It should wait for the product list to update before reading the product names") |
76 | | - void shouldWaitForProductListToUpdate() { |
77 | | - |
78 | | - page.getByLabel("Screwdriver").click(); |
79 | | - page.getByLabel("Chisels").click(); |
80 | | - |
81 | | - page.waitForLoadState(LoadState.LOAD); |
82 | | - |
83 | | - List<String> matchingProducts = page.getByTestId("product-name").allTextContents(); |
84 | | - |
85 | | - Assertions.assertThat(matchingProducts) |
86 | | - .allMatch( product -> product.contains("Screwdriver") || product.contains("Chisels")); |
87 | | - |
| 87 | + @DisplayName("When a search returns no products") |
| 88 | + void whenNoItemsAreFound() { |
| 89 | + page.route("**/products/search?q=pliers", |
| 90 | + route -> route.fulfill(new Route.FulfillOptions() |
| 91 | + .setBody(MockSearchResponses.RESPONSE_WITH_NO_ENTRIES) |
| 92 | + .setStatus(200)) |
| 93 | + ); |
| 94 | + |
| 95 | + page.navigate("https://practicesoftwaretesting.com"); |
| 96 | + page.getByPlaceholder("Search").fill("pliers"); |
| 97 | + page.getByPlaceholder("Search").press("Enter"); |
| 98 | + |
| 99 | + assertThat(page.getByTestId("product-name")).isHidden(); |
| 100 | + assertThat(page.getByTestId("search_completed")).hasText("There are no products found."); |
88 | 101 | } |
89 | | - |
90 | 102 | } |
91 | 103 | } |
0 commit comments