Skip to content

Commit 327f38d

Browse files
committed
Sample code
1 parent 4d29422 commit 327f38d

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

src/test/java/com/serenitydojo/playwright/MockSearchResponses.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@ public class MockSearchResponses {
4444
}
4545
""";
4646

47+
48+
4749
}

src/test/java/com/serenitydojo/playwright/PlaywrightRestAPITest.java

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static void setUpBrowser() {
2828
playwright = Playwright.create();
2929
playwright.selectors().setTestIdAttribute("data-test");
3030
browser = playwright.chromium().launch(
31-
new BrowserType.LaunchOptions().setHeadless(true)
31+
new BrowserType.LaunchOptions().setHeadless(false)
3232
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
3333
);
3434
}
@@ -50,54 +50,47 @@ static void tearDown() {
5050
playwright.close();
5151
}
5252

53-
@BeforeEach
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-
);
60-
page.navigate("https://practicesoftwaretesting.com");
61-
}
62-
6353
@DisplayName("Playwright allows us to mock out API responses")
6454
@Nested
6555
class MockingAPIResponses {
6656

6757
@Test
6858
@DisplayName("When a search returns a single product")
6959
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-
);
7560

61+
// /products/search?q=Pliers
62+
page.route("**/products/search?q=Pliers", route -> {
63+
route.fulfill(
64+
new Route.FulfillOptions()
65+
.setBody(MockSearchResponses.RESPONSE_WITH_A_SINGLE_ENTRY)
66+
.setStatus(200)
67+
);
68+
});
7669
page.navigate("https://practicesoftwaretesting.com");
77-
page.getByPlaceholder("Search").fill("pliers");
70+
page.getByPlaceholder("Search").fill("Pliers");
7871
page.getByPlaceholder("Search").press("Enter");
7972

8073
assertThat(page.getByTestId("product-name")).hasCount(1);
81-
assertThat(page.getByTestId("product-name")
82-
.filter(new Locator.FilterOptions().setHasText("Super Pliers")))
83-
.isVisible();
74+
assertThat(page.getByTestId("product-name")).hasText("Super Pliers");
8475
}
8576

8677
@Test
8778
@DisplayName("When a search returns no products")
8879
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-
80+
page.route("**/products/search?q=Pliers", route -> {
81+
route.fulfill(
82+
new Route.FulfillOptions()
83+
.setBody(MockSearchResponses.RESPONSE_WITH_NO_ENTRIES)
84+
.setStatus(200)
85+
);
86+
});
9587
page.navigate("https://practicesoftwaretesting.com");
96-
page.getByPlaceholder("Search").fill("pliers");
88+
page.getByPlaceholder("Search").fill("Pliers");
9789
page.getByPlaceholder("Search").press("Enter");
9890

99-
assertThat(page.getByTestId("product-name")).isHidden();
91+
assertThat(page.getByTestId("product-name")).hasCount(0);
10092
assertThat(page.getByTestId("search_completed")).hasText("There are no products found.");
93+
10194
}
10295
}
10396
}

0 commit comments

Comments
 (0)