Skip to content

Commit 4977e2f

Browse files
committed
Updated dependencies to Playwright 1.57.0 and JUnit 6.0.1
1 parent eb534a2 commit 4977e2f

File tree

3 files changed

+76
-24
lines changed

3 files changed

+76
-24
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818
<dependency>
1919
<groupId>com.microsoft.playwright</groupId>
2020
<artifactId>playwright</artifactId>
21-
<version>1.47.0</version>
21+
<version>1.57.0</version>
2222
<scope>test</scope>
2323
</dependency>
2424
<dependency>
2525
<groupId>org.junit.jupiter</groupId>
2626
<artifactId>junit-jupiter</artifactId>
27-
<version>5.11.1</version>
27+
<version>6.0.1</version>
2828
<scope>test</scope>
2929
</dependency>
3030
<dependency>
3131
<groupId>org.assertj</groupId>
3232
<artifactId>assertj-core</artifactId>
33-
<version>3.26.3</version>
33+
<version>3.27.6</version>
3434
</dependency>
3535
<dependency>
3636
<groupId>com.deque.html.axe-core</groupId>

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

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

33
import com.google.gson.Gson;
4+
import com.google.gson.JsonArray;
45
import com.google.gson.JsonObject;
56
import com.microsoft.playwright.*;
6-
import com.microsoft.playwright.options.AriaRole;
7-
import com.microsoft.playwright.options.RequestOptions;
87
import org.assertj.core.api.Assertions;
98
import org.junit.jupiter.api.*;
109
import org.junit.jupiter.api.parallel.Execution;
1110
import org.junit.jupiter.api.parallel.ExecutionMode;
11+
import org.junit.jupiter.params.ParameterizedTest;
12+
import org.junit.jupiter.params.provider.MethodSource;
13+
1214

1315
import java.util.Arrays;
16+
import java.util.HashMap;
17+
import java.util.stream.Stream;
1418

1519
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
1620

@@ -37,6 +41,10 @@ static void setUpBrowser() {
3741
void setUp() {
3842
browserContext = browser.newContext();
3943
page = browserContext.newPage();
44+
45+
page.navigate("https://practicesoftwaretesting.com");
46+
page.getByPlaceholder("Search").waitFor();
47+
4048
}
4149

4250
@AfterEach
@@ -50,16 +58,6 @@ static void tearDown() {
5058
playwright.close();
5159
}
5260

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-
6361
@DisplayName("Playwright allows us to mock out API responses")
6462
@Nested
6563
class MockingAPIResponses {
@@ -73,9 +71,9 @@ void whenASingleItemIsFound() {
7371
.setStatus(200))
7472
);
7573

76-
page.navigate("https://practicesoftwaretesting.com");
77-
page.getByPlaceholder("Search").fill("pliers");
78-
page.getByPlaceholder("Search").press("Enter");
74+
var searchBox = page.getByPlaceholder("Search");
75+
searchBox.fill("pliers");
76+
searchBox.press("Enter");
7977

8078
assertThat(page.getByTestId("product-name")).hasCount(1);
8179
assertThat(page.getByTestId("product-name")
@@ -91,13 +89,67 @@ void whenNoItemsAreFound() {
9189
.setBody(MockSearchResponses.RESPONSE_WITH_NO_ENTRIES)
9290
.setStatus(200))
9391
);
94-
95-
page.navigate("https://practicesoftwaretesting.com");
96-
page.getByPlaceholder("Search").fill("pliers");
97-
page.getByPlaceholder("Search").press("Enter");
92+
var searchBox = page.getByPlaceholder("Search");
93+
searchBox.fill("pliers");
94+
searchBox.press("Enter");
9895

9996
assertThat(page.getByTestId("product-name")).isHidden();
10097
assertThat(page.getByTestId("search_completed")).hasText("There are no products found.");
10198
}
10299
}
100+
101+
@Nested
102+
class MakingAPICalls {
103+
104+
record Product(String name, Double price) {}
105+
106+
private static APIRequestContext requestContext;
107+
108+
@BeforeAll
109+
public static void setupRequestContext() {
110+
requestContext = playwright.request().newContext(
111+
new APIRequest.NewContextOptions()
112+
.setBaseURL("https://api.practicesoftwaretesting.com")
113+
.setExtraHTTPHeaders(new HashMap<>() {{
114+
put("Accept", "application/json");
115+
}})
116+
);
117+
}
118+
119+
@DisplayName("Check presence of known products")
120+
@ParameterizedTest(name = "Checking product {0}")
121+
@MethodSource("products")
122+
void checkKnownProduct(Product product) {
123+
page.fill("[placeholder='Search']", product.name);
124+
page.click("button:has-text('Search')");
125+
126+
// Check that the product appears with the correct name and price
127+
128+
Locator productCard = page.locator(".card")
129+
.filter(
130+
new Locator.FilterOptions()
131+
.setHasText(product.name)
132+
.setHasText(Double.toString(product.price))
133+
);
134+
assertThat(productCard).isVisible();
135+
}
136+
137+
static Stream<Product> products() {
138+
APIResponse response = requestContext.get("/products?page=2");
139+
Assertions.assertThat(response.status()).isEqualTo(200);
140+
141+
JsonObject jsonObject = new Gson().fromJson(response.text(), JsonObject.class);
142+
JsonArray data = jsonObject.getAsJsonArray("data");
143+
144+
return data.asList().stream()
145+
.map(jsonElement -> {
146+
JsonObject productJson = jsonElement.getAsJsonObject();
147+
return new Product(
148+
productJson.get("name").getAsString(),
149+
productJson.get("price").getAsDouble()
150+
);
151+
});
152+
}
153+
154+
}
103155
}

src/test/resources/junit-platform.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Enable parallel execution
22
junit.jupiter.execution.parallel.enabled = true
33

4-
# Configure the parallel mode (methods, classes, or both)
5-
junit.jupiter.execution.parallel.mode.default = classes
4+
# Configure the parallel mode (concurrent or same_thread)
5+
junit.jupiter.execution.parallel.mode.default = same_thread
66
junit.jupiter.execution.parallel.mode.classes.default = concurrent
77

88
# Set the parallelism (number of threads)

0 commit comments

Comments
 (0)