From ee4f8698da56bebf1f3a705ab7dcf4b27d31f75b Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 3 Feb 2021 13:18:01 -0800 Subject: [PATCH] fix: auto dismiss dialogs if there are no listeners (#252) --- .../microsoft/playwright/impl/PageImpl.java | 9 +- .../microsoft/playwright/TestPageDialog.java | 126 ++++++++++++++++++ 2 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 playwright/src/test/java/com/microsoft/playwright/TestPageDialog.java 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 37e5f1c6..dc5b753d 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java @@ -110,10 +110,11 @@ public class PageImpl extends ChannelOwner implements Page { if ("dialog".equals(event)) { String guid = params.getAsJsonObject("dialog").get("guid").getAsString(); DialogImpl dialog = connection.getExistingObject(guid); - // If no action taken the dialog will stay open and execution will hang. We - // could automatically dismiss the dialog if there are no listeners but we want - // the behavior to match upstream one. - listeners.notify(EventType.DIALOG, dialog); + if (listeners.hasListeners(EventType.DIALOG)) { + listeners.notify(EventType.DIALOG, dialog); + } else { + dialog.dismiss(); + } } else if ("popup".equals(event)) { String guid = params.getAsJsonObject("page").get("guid").getAsString(); PageImpl popup = connection.getExistingObject(guid); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageDialog.java b/playwright/src/test/java/com/microsoft/playwright/TestPageDialog.java new file mode 100644 index 00000000..db688e3b --- /dev/null +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageDialog.java @@ -0,0 +1,126 @@ +/* + * 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 static org.junit.jupiter.api.Assertions.*; + +public class TestPageDialog extends TestBase { + @Test + void shouldFire() { + page.onDialog(dialog -> { + assertEquals("alert", dialog.type()); + assertEquals("", dialog.defaultValue()); + assertEquals("yo", dialog.message()); + dialog.accept(); + }); + page.evaluate("alert('yo')"); + } + + @Test + void shouldAllowAcceptingPrompts() { + page.onDialog(dialog -> { + assertEquals("prompt", dialog.type()); + assertEquals("yes.", dialog.defaultValue()); + assertEquals("question?", dialog.message()); + dialog.accept("answer!"); + }); + Object result = page.evaluate("prompt('question?', 'yes.')"); + assertEquals("answer!", result); + } + + @Test + void shouldDismissThePrompt() { + page.onDialog(dialog -> { + dialog.dismiss(); + }); + Object result = page.evaluate("prompt('question?')"); + assertNull(result); + } + + @Test + void shouldAcceptTheConfirmPrompt() { + page.onDialog(dialog -> { + dialog.accept(); + }); + Object result = page.evaluate("confirm('boolean?')"); + assertEquals(true, result); + } + + @Test + void shouldDismissTheConfirmPrompt() { + page.onDialog(dialog -> { + dialog.dismiss(); + }); + Object result = page.evaluate("() => confirm('boolean?')"); + assertEquals(false, result); + } + + @Test + void shouldBeAbleToCloseContextWithOpenAlert() { + Page page = context.newPage(); + boolean[] didShowDialog = {false}; + page.onDialog(dialog -> didShowDialog[0] = true); + page.evaluate("() => setTimeout(() => alert('hello'), 0)"); + while (!didShowDialog[0]) { + page.waitForTimeout(100); + } + } + + @Test + void shouldHandleMultipleAlerts() { + page.onDialog(dialog -> { + dialog.accept(); + }); + page.setContent("

Hello World

\n" + + "\n"); + assertEquals("Hello World", page.textContent("p")); + } + + @Test + void shouldHandleMultipleConfirms() { + page.onDialog(dialog -> { + dialog.accept(); + }); + page.setContent("

Hello World

\n" + + "\n"); + assertEquals("Hello World", page.textContent("p")); + } + + @Test + void shouldAutoDismissThePromptWithoutListeners() { + Object result = page.evaluate("() => prompt('question?')"); + assertNull(result); + } + + @Test + void shouldAutoDismissTheAlertWithoutListeners() { + page.setContent("
Click me
"); + page.click("div"); + assertEquals(true, page.evaluate("window._clicked")); + } + +}