Skip to content

Commit b010d3e

Browse files
authored
fix: make sure page.goto navigates away from pages with beforeunload (microsoft#898)
1 parent 6cb70f3 commit b010d3e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

playwright/_impl/_page.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from_nullable_channel,
3838
)
3939
from playwright._impl._console_message import ConsoleMessage
40+
from playwright._impl._dialog import Dialog
4041
from playwright._impl._download import Download
4142
from playwright._impl._element_handle import ElementHandle
4243
from playwright._impl._event_context_manager import EventContextManagerImpl
@@ -242,11 +243,14 @@ def _on_crash(self) -> None:
242243
self.emit(Page.Events.Crash)
243244

244245
def _on_dialog(self, params: Any) -> None:
245-
dialog = from_channel(params["dialog"])
246+
dialog = cast(Dialog, from_channel(params["dialog"]))
246247
if self.listeners(Page.Events.Dialog):
247248
self.emit(Page.Events.Dialog, dialog)
248249
else:
249-
asyncio.create_task(dialog.dismiss())
250+
if dialog.type == "beforeunload":
251+
asyncio.create_task(dialog.accept())
252+
else:
253+
asyncio.create_task(dialog.dismiss())
250254

251255
def _on_download(self, params: Any) -> None:
252256
url = params["url"]

tests/async/test_page.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ async def test_close_should_not_run_beforeunload_by_default(context, server):
7575
await page.close()
7676

7777

78+
async def test_should_be_able_to_navigate_away_from_page_with_before_unload(
79+
server: Server, page: Page
80+
):
81+
await page.goto(server.PREFIX + "/beforeunload.html")
82+
# We have to interact with a page so that 'beforeunload' handlers
83+
# fire.
84+
await page.click("body")
85+
await page.goto(server.EMPTY_PAGE)
86+
87+
7888
async def test_close_should_set_the_page_close_state(context):
7989
page = await context.new_page()
8090
assert page.is_closed() is False

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy