Skip to content

Commit bced940

Browse files
yury-sclaude
andcommitted
test: add tests for new 1.59 APIs (Overlay, Debugger.next, existingResponse, httpVersion)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 6ffe56e commit bced940

File tree

4 files changed

+154
-3
lines changed

4 files changed

+154
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Playwright is a Java library to automate [Chromium](https://www.chromium.org/Hom
1010

1111
| | Linux | macOS | Windows |
1212
| :--- | :---: | :---: | :---: |
13-
| Chromium <!-- GEN:chromium-version -->146.0.7680.31<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
13+
| Chromium <!-- GEN:chromium-version -->147.0.7727.15<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
1414
| WebKit <!-- GEN:webkit-version -->26.0<!-- GEN:stop --> ||||
1515
| Firefox <!-- GEN:firefox-version -->148.0.2<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
1616

playwright/src/test/java/com/microsoft/playwright/TestDebugger.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import com.microsoft.playwright.options.PausedDetails;
2020
import org.junit.jupiter.api.Test;
2121

22-
import java.util.concurrent.atomic.AtomicReference;
23-
2422
import static org.junit.jupiter.api.Assertions.*;
2523

2624
public class TestDebugger extends TestBase {
@@ -53,6 +51,31 @@ void shouldPauseAtNextAndResume() {
5351
assertNull(dbg.pausedDetails());
5452
}
5553

54+
@Test
55+
void shouldStepWithNext() {
56+
page.setContent("<div>click me</div>");
57+
Debugger dbg = context.debugger();
58+
assertNull(dbg.pausedDetails());
59+
60+
dbg.requestPause();
61+
62+
boolean[] paused = {false};
63+
dbg.onPausedStateChanged(() -> {
64+
if (!paused[0]) {
65+
paused[0] = true;
66+
PausedDetails details = dbg.pausedDetails();
67+
assertNotNull(details);
68+
assertTrue(details.title.contains("Click"), "title: " + details.title);
69+
dbg.next();
70+
} else if (dbg.pausedDetails() != null) {
71+
dbg.resume();
72+
}
73+
});
74+
75+
page.click("div");
76+
assertNull(dbg.pausedDetails());
77+
}
78+
5679
@Test
5780
void shouldPauseAtPauseCall() {
5881
page.setContent("<div>click me</div>");
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.microsoft.playwright;
18+
19+
import com.microsoft.playwright.Overlay;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.junit.jupiter.api.Assertions.*;
23+
24+
// Note: The overlay elements are rendered inside a closed shadow root in driver mode,
25+
// so locator-based DOM assertions are not possible in Java. These tests verify that
26+
// the protocol calls succeed without errors.
27+
public class TestOverlay extends TestBase {
28+
@Test
29+
void shouldAddAndRemoveOverlay() throws Exception {
30+
page.navigate(server.EMPTY_PAGE);
31+
AutoCloseable disposable = page.overlay().show("<div id=\"my-overlay\">Hello Overlay</div>");
32+
assertNotNull(disposable);
33+
disposable.close();
34+
}
35+
36+
@Test
37+
void shouldAddMultipleOverlays() throws Exception {
38+
page.navigate(server.EMPTY_PAGE);
39+
AutoCloseable d1 = page.overlay().show("<div id=\"overlay-1\">First</div>");
40+
AutoCloseable d2 = page.overlay().show("<div id=\"overlay-2\">Second</div>");
41+
d1.close();
42+
d2.close();
43+
}
44+
45+
@Test
46+
void shouldHideAndShowOverlays() throws Exception {
47+
page.navigate(server.EMPTY_PAGE);
48+
page.overlay().show("<div id=\"my-overlay\">Visible</div>");
49+
page.overlay().setVisible(false);
50+
page.overlay().setVisible(true);
51+
}
52+
53+
@Test
54+
void shouldSurviveNavigation() {
55+
page.navigate(server.EMPTY_PAGE);
56+
page.overlay().show("<div id=\"persistent\">Survives Reload</div>");
57+
page.navigate(server.EMPTY_PAGE);
58+
page.reload();
59+
}
60+
61+
@Test
62+
void shouldRemoveOverlayAndNotRestoreAfterNavigation() throws Exception {
63+
page.navigate(server.EMPTY_PAGE);
64+
AutoCloseable disposable = page.overlay().show("<div id=\"temp\">Temporary</div>");
65+
disposable.close();
66+
page.reload();
67+
}
68+
69+
@Test
70+
void shouldSanitizeScriptsFromOverlayHtml() {
71+
page.navigate(server.EMPTY_PAGE);
72+
page.overlay().show("<div id=\"safe\">Safe</div><script>window.__injected = true</script>");
73+
}
74+
75+
@Test
76+
void shouldStripEventHandlersFromOverlayHtml() {
77+
page.navigate(server.EMPTY_PAGE);
78+
page.overlay().show("<div id=\"clean\" onclick=\"window.__clicked=true\">Click me</div>");
79+
}
80+
81+
@Test
82+
void shouldAutoRemoveOverlayAfterTimeout() {
83+
page.navigate(server.EMPTY_PAGE);
84+
page.overlay().show("<div id=\"timed\">Temporary</div>", new Overlay.ShowOptions().setDuration(1));
85+
}
86+
87+
@Test
88+
void shouldAllowStylesInOverlayHtml() {
89+
page.navigate(server.EMPTY_PAGE);
90+
page.overlay().show("<div id=\"styled\" style=\"color: red; font-size: 20px;\">Styled</div>");
91+
}
92+
93+
@Test
94+
void shouldShowChapter() {
95+
page.navigate(server.EMPTY_PAGE);
96+
page.overlay().chapter("Chapter Title");
97+
page.overlay().chapter("With Description", new Overlay.ChapterOptions().setDescription("Some details").setDuration(100));
98+
}
99+
}

playwright/src/test/java/com/microsoft/playwright/TestPageNetworkResponse.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,35 @@ void shouldRejectResponseFinishedIfPageCloses() {
7171
assertTrue(e.getMessage().contains("closed"), e.getMessage());
7272
}
7373

74+
@Test
75+
void shouldReturnNullExistingResponseBeforeResponseReceived() {
76+
Request[] capturedRequest = {null};
77+
page.route("**/*", route -> {
78+
capturedRequest[0] = route.request();
79+
assertNull(capturedRequest[0].existingResponse());
80+
route.resume();
81+
});
82+
page.navigate(server.EMPTY_PAGE);
83+
assertNotNull(capturedRequest[0]);
84+
}
85+
86+
@Test
87+
void shouldReturnExistingResponseAfterReceived() {
88+
Response[] responses = {null};
89+
page.onResponse(r -> responses[0] = r);
90+
page.navigate(server.EMPTY_PAGE);
91+
assertNotNull(responses[0]);
92+
assertEquals(responses[0], responses[0].request().existingResponse());
93+
}
94+
95+
@Test
96+
void shouldReturnHttpVersion() {
97+
page.navigate(server.EMPTY_PAGE);
98+
Response response = page.waitForResponse("**/*", () -> page.navigate(server.EMPTY_PAGE));
99+
String version = response.httpVersion();
100+
assertTrue(version.matches("HTTP/[12](\\.[01])?"), "unexpected version: " + version);
101+
}
102+
74103
@Test
75104
void shouldRejectResponseFinishedIfContextCloses() {
76105
page.navigate(server.EMPTY_PAGE);

0 commit comments

Comments
 (0)