diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java index e4b47740..b9b0c51c 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java @@ -55,6 +55,7 @@ public class PageImpl extends ChannelOwner implements Page { browserContext = (BrowserContextImpl) parent; mainFrame = connection.getExistingObject(initializer.getAsJsonObject("mainFrame").get("guid").getAsString()); mainFrame.page = this; + isClosed = initializer.get("isClosed").getAsBoolean(); keyboard = new KeyboardImpl(this); mouse = new MouseImpl(this); touchscreen = new TouchscreenImpl(this); @@ -212,6 +213,9 @@ public class PageImpl extends ChannelOwner implements Page { @Override public void close(CloseOptions options) { + if (isClosed) { + return; + } JsonObject params = options == null ? new JsonObject() : gson().toJsonTree(options).getAsJsonObject(); try { sendMessage("close", params); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextBasic.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextBasic.java new file mode 100644 index 00000000..a0aa451c --- /dev/null +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextBasic.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.microsoft.playwright; + +import org.junit.jupiter.api.Test; + +import java.io.OutputStreamWriter; + +import static com.microsoft.playwright.BrowserContext.EventType.PAGE; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TestBrowserContextBasic extends TestBase { + @Test + void shouldNotReportFramelessPagesOnError() { + BrowserContext context = browser.newContext(); + Page page = context.newPage(); + server.setRoute("/empty.html", exchange -> { + exchange.sendResponseHeaders(200, 0); + try (OutputStreamWriter writer = new OutputStreamWriter(exchange.getResponseBody())) { + writer.write("Click me"); + } + }); + Page[] popup = {null}; + context.addListener(PAGE, event -> popup[0] = (Page) event.data()); + page.navigate(server.EMPTY_PAGE); + page.click("'Click me'"); + context.close(); + if (popup[0] != null) { + // This races on Firefox :/ + assertTrue(popup[0].isClosed()); + assertNotNull(popup[0].mainFrame()); + } + } +}