Skip to content

Commit d827f7f

Browse files
committed
Improved the reliability of the API tests
1 parent a1d0d7d commit d827f7f

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static void setUpBrowser() {
2727
playwright = Playwright.create();
2828
browser = playwright.chromium().launch(
2929
new BrowserType.LaunchOptions()
30-
.setHeadless(false)
30+
.setHeadless(true)
3131
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
3232
);
3333
playwright.selectors().setTestIdAttribute("data-test");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class MyOptions implements OptionsFactory {
1818
@Override
1919
public Options getOptions() {
2020
return new Options()
21-
// .setHeadless(false)
21+
// .setHeadless(true)
2222
.setLaunchOptions(
2323
new BrowserType.LaunchOptions()
2424
.setArgs(Arrays.asList("--no-sandbox","--disable-gpu"))

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package com.serenitydojo.playwright;
22

3-
import com.google.gson.Gson;
4-
import com.google.gson.JsonObject;
53
import com.microsoft.playwright.*;
6-
import com.microsoft.playwright.options.AriaRole;
7-
import com.microsoft.playwright.options.RequestOptions;
8-
import org.assertj.core.api.Assertions;
94
import org.junit.jupiter.api.*;
105
import org.junit.jupiter.api.parallel.Execution;
116
import org.junit.jupiter.api.parallel.ExecutionMode;
@@ -28,7 +23,7 @@ static void setUpBrowser() {
2823
playwright = Playwright.create();
2924
playwright.selectors().setTestIdAttribute("data-test");
3025
browser = playwright.chromium().launch(
31-
new BrowserType.LaunchOptions().setHeadless(true)
26+
new BrowserType.LaunchOptions().setHeadless(false)
3227
.setArgs(Arrays.asList("--no-sandbox", "--disable-extensions", "--disable-gpu"))
3328
);
3429
}
@@ -37,6 +32,10 @@ static void setUpBrowser() {
3732
void setUp() {
3833
browserContext = browser.newContext();
3934
page = browserContext.newPage();
35+
36+
page.navigate("https://practicesoftwaretesting.com");
37+
page.getByTestId("product-name").waitFor();
38+
4039
}
4140

4241
@AfterEach
@@ -50,16 +49,6 @@ static void tearDown() {
5049
playwright.close();
5150
}
5251

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-
6352
@DisplayName("Playwright allows us to mock out API responses")
6453
@Nested
6554
class MockingAPIResponses {
@@ -73,9 +62,13 @@ void whenASingleItemIsFound() {
7362
.setStatus(200))
7463
);
7564

76-
page.navigate("https://practicesoftwaretesting.com");
77-
page.getByPlaceholder("Search").fill("pliers");
78-
page.getByPlaceholder("Search").press("Enter");
65+
var searchBox = page.getByPlaceholder("Search");
66+
searchBox.waitFor(); // Wait for element to be ready
67+
searchBox.fill("pliers");
68+
searchBox.press("Enter");
69+
70+
page.waitForResponse("**/products/search?q=pliers", () -> {
71+
});
7972

8073
assertThat(page.getByTestId("product-name")).hasCount(1);
8174
assertThat(page.getByTestId("product-name")
@@ -91,10 +84,13 @@ void whenNoItemsAreFound() {
9184
.setBody(MockSearchResponses.RESPONSE_WITH_NO_ENTRIES)
9285
.setStatus(200))
9386
);
87+
var searchBox = page.getByPlaceholder("Search");
88+
searchBox.waitFor(); // Wait for element to be ready
89+
searchBox.fill("pliers");
90+
searchBox.press("Enter");
9491

95-
page.navigate("https://practicesoftwaretesting.com");
96-
page.getByPlaceholder("Search").fill("pliers");
97-
page.getByPlaceholder("Search").press("Enter");
92+
page.waitForResponse("**/products/search?q=pliers", () -> {
93+
});
9894

9995
assertThat(page.getByTestId("product-name")).isHidden();
10096
assertThat(page.getByTestId("search_completed")).hasText("There are no products found.");

0 commit comments

Comments
 (0)