Skip to content

Commit 4d29422

Browse files
committed
Sample code
1 parent 1e23c02 commit 4d29422

File tree

2 files changed

+89
-30
lines changed

2 files changed

+89
-30
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.serenitydojo.playwright;
2+
3+
public class MockSearchResponses {
4+
public static final String RESPONSE_WITH_A_SINGLE_ENTRY = """
5+
{
6+
"current_page": 1,
7+
"data": [
8+
{
9+
"id": "01JBSC2JBTD1HY15BZQR9RMBB8",
10+
"name": "Super Pliers",
11+
"description": "A really good pair of pliers",
12+
"price": 14.15,
13+
"is_location_offer": false,
14+
"is_rental": false,
15+
"in_stock": true,
16+
"product_image": {
17+
"id": "01JBSC2JBJ445KGXKVSE1VDE69",
18+
"by_name": "Helinton Fantin",
19+
"by_url": "https://unsplash.com/@fantin",
20+
"source_name": "Unsplash",
21+
"source_url": "https://unsplash.com/photos/W8BNwvOvW4M",
22+
"file_name": "pliers01.avif",
23+
"title": "Super pliers"
24+
}
25+
}
26+
],
27+
"from": 1,
28+
"last_page": 1,
29+
"per_page": 9,
30+
"to": 1,
31+
"total": 1
32+
}
33+
""";
34+
35+
public static final String RESPONSE_WITH_NO_ENTRIES = """
36+
{
37+
"current_page": 1,
38+
"data": [],
39+
"from": 1,
40+
"last_page": 1,
41+
"per_page": 9,
42+
"to": 1,
43+
"total": 0
44+
}
45+
""";
46+
47+
}
Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package com.serenitydojo.playwright;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonObject;
35
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;
58
import org.assertj.core.api.Assertions;
69
import org.junit.jupiter.api.*;
710
import org.junit.jupiter.api.parallel.Execution;
811
import org.junit.jupiter.api.parallel.ExecutionMode;
912

1013
import java.util.Arrays;
11-
import java.util.Comparator;
12-
import java.util.List;
1314

1415
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
1516

1617
@Execution(ExecutionMode.SAME_THREAD)
17-
public class PlaywrightWaitsTest {
18+
public class PlaywrightRestAPITest {
1819

1920
protected static Playwright playwright;
2021
protected static Browser browser;
@@ -51,41 +52,52 @@ static void tearDown() {
5152

5253
@BeforeEach
5354
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+
);
5460
page.navigate("https://practicesoftwaretesting.com");
55-
page.waitForCondition(() -> page.getByTestId("product-name").count() > 0);
5661
}
5762

58-
@DisplayName("Playwright waits automatically for elements by default")
63+
@DisplayName("Playwright allows us to mock out API responses")
5964
@Nested
60-
class AutoWaits {
65+
class MockingAPIResponses {
6166

6267
@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();
7284
}
7385

7486
@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.");
88101
}
89-
90102
}
91103
}

0 commit comments

Comments
 (0)