Skip to content

Commit 077e8f6

Browse files
authored
fix: correctly initialize page.isClosed() (#163)
1 parent 190bcbd commit 077e8f6

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class PageImpl extends ChannelOwner implements Page {
5555
browserContext = (BrowserContextImpl) parent;
5656
mainFrame = connection.getExistingObject(initializer.getAsJsonObject("mainFrame").get("guid").getAsString());
5757
mainFrame.page = this;
58+
isClosed = initializer.get("isClosed").getAsBoolean();
5859
keyboard = new KeyboardImpl(this);
5960
mouse = new MouseImpl(this);
6061
touchscreen = new TouchscreenImpl(this);
@@ -212,6 +213,9 @@ public void removeListener(EventType type, Listener<EventType> listener) {
212213

213214
@Override
214215
public void close(CloseOptions options) {
216+
if (isClosed) {
217+
return;
218+
}
215219
JsonObject params = options == null ? new JsonObject() : gson().toJsonTree(options).getAsJsonObject();
216220
try {
217221
sendMessage("close", params);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 org.junit.jupiter.api.Test;
20+
21+
import java.io.OutputStreamWriter;
22+
23+
import static com.microsoft.playwright.BrowserContext.EventType.PAGE;
24+
import static org.junit.jupiter.api.Assertions.assertNotNull;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
27+
public class TestBrowserContextBasic extends TestBase {
28+
@Test
29+
void shouldNotReportFramelessPagesOnError() {
30+
BrowserContext context = browser.newContext();
31+
Page page = context.newPage();
32+
server.setRoute("/empty.html", exchange -> {
33+
exchange.sendResponseHeaders(200, 0);
34+
try (OutputStreamWriter writer = new OutputStreamWriter(exchange.getResponseBody())) {
35+
writer.write("<a href='" + server.EMPTY_PAGE + "' target='_blank'>Click me</a>");
36+
}
37+
});
38+
Page[] popup = {null};
39+
context.addListener(PAGE, event -> popup[0] = (Page) event.data());
40+
page.navigate(server.EMPTY_PAGE);
41+
page.click("'Click me'");
42+
context.close();
43+
if (popup[0] != null) {
44+
// This races on Firefox :/
45+
assertTrue(popup[0].isClosed());
46+
assertNotNull(popup[0].mainFrame());
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)