Skip to content

Commit 1ca022e

Browse files
committed
Improved the reliability of the API tests
1 parent 327f38d commit 1ca022e

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
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: 31 additions & 28 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;
@@ -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
@@ -57,40 +56,44 @@ class MockingAPIResponses {
5756
@Test
5857
@DisplayName("When a search returns a single product")
5958
void whenASingleItemIsFound() {
60-
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-
);
59+
page.route("**/products/search?q=pliers",
60+
route -> route.fulfill(new Route.FulfillOptions()
61+
.setBody(MockSearchResponses.RESPONSE_WITH_A_SINGLE_ENTRY)
62+
.setStatus(200))
63+
);
64+
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", () -> {
6871
});
69-
page.navigate("https://practicesoftwaretesting.com");
70-
page.getByPlaceholder("Search").fill("Pliers");
71-
page.getByPlaceholder("Search").press("Enter");
7272

7373
assertThat(page.getByTestId("product-name")).hasCount(1);
74-
assertThat(page.getByTestId("product-name")).hasText("Super Pliers");
74+
assertThat(page.getByTestId("product-name")
75+
.filter(new Locator.FilterOptions().setHasText("Super Pliers")))
76+
.isVisible();
7577
}
7678

7779
@Test
7880
@DisplayName("When a search returns no products")
7981
void whenNoItemsAreFound() {
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-
);
82+
page.route("**/products/search?q=pliers",
83+
route -> route.fulfill(new Route.FulfillOptions()
84+
.setBody(MockSearchResponses.RESPONSE_WITH_NO_ENTRIES)
85+
.setStatus(200))
86+
);
87+
var searchBox = page.getByPlaceholder("Search");
88+
searchBox.waitFor(); // Wait for element to be ready
89+
searchBox.fill("pliers");
90+
searchBox.press("Enter");
91+
92+
page.waitForResponse("**/products/search?q=pliers", () -> {
8693
});
87-
page.navigate("https://practicesoftwaretesting.com");
88-
page.getByPlaceholder("Search").fill("Pliers");
89-
page.getByPlaceholder("Search").press("Enter");
9094

91-
assertThat(page.getByTestId("product-name")).hasCount(0);
95+
assertThat(page.getByTestId("product-name")).isHidden();
9296
assertThat(page.getByTestId("search_completed")).hasText("There are no products found.");
93-
9497
}
9598
}
9699
}

0 commit comments

Comments
 (0)