mirror of
https://github.com/microsoft/playwright-java.git
synced 2025-12-29 02:40:42 +00:00
fix(api): make Dialog.type() return string (#233)
This commit is contained in:
parent
982cc6a3bc
commit
e545829bf5
@ -22,8 +22,6 @@ import java.util.*;
|
||||
* {@code Dialog} objects are dispatched by page via the [{@code event: Page.dialog}] event.
|
||||
*/
|
||||
public interface Dialog {
|
||||
enum Type { ALERT, BEFOREUNLOAD, CONFIRM, PROMPT }
|
||||
|
||||
default void accept() {
|
||||
accept(null);
|
||||
}
|
||||
@ -48,6 +46,6 @@ public interface Dialog {
|
||||
/**
|
||||
* Returns dialog's type, can be one of {@code alert}, {@code beforeunload}, {@code confirm} or {@code prompt}.
|
||||
*/
|
||||
Type type();
|
||||
String type();
|
||||
}
|
||||
|
||||
|
||||
@ -52,13 +52,7 @@ class DialogImpl extends ChannelOwner implements Dialog {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type type() {
|
||||
switch (initializer.get("type").getAsString()) {
|
||||
case "alert": return Type.ALERT;
|
||||
case "beforeunload": return Type.BEFOREUNLOAD;
|
||||
case "confirm": return Type.CONFIRM;
|
||||
case "prompt": return Type.PROMPT;
|
||||
default: throw new PlaywrightException("Unexpected dialog type: " + initializer.get("type").getAsString());
|
||||
}
|
||||
public String type() {
|
||||
return initializer.get("type").getAsString();
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,17 +19,17 @@ package com.microsoft.playwright;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.DisabledIf;
|
||||
|
||||
import static com.microsoft.playwright.Dialog.Type.ALERT;
|
||||
import static com.microsoft.playwright.Dialog.Type.PROMPT;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestDialog extends TestBase {
|
||||
|
||||
@Test
|
||||
void shouldFire() {
|
||||
page.onDialog(dialog -> {
|
||||
assertEquals(ALERT, dialog.type());
|
||||
assertEquals("alert", dialog.type());
|
||||
assertEquals( "", dialog.defaultValue());
|
||||
assertEquals( "yo", dialog.message());
|
||||
dialog.accept();
|
||||
@ -40,7 +40,7 @@ public class TestDialog extends TestBase {
|
||||
@Test
|
||||
void shouldAllowAcceptingPrompts() {
|
||||
page.onDialog(dialog -> {
|
||||
assertEquals(PROMPT, dialog.type());
|
||||
assertEquals("prompt", dialog.type());
|
||||
assertEquals("yes.", dialog.defaultValue());
|
||||
assertEquals("question?", dialog.message());
|
||||
dialog.accept("answer!");
|
||||
@ -85,11 +85,16 @@ public class TestDialog extends TestBase {
|
||||
void shouldBeAbleToCloseContextWithOpenAlert() {
|
||||
BrowserContext context = browser.newContext();
|
||||
Page page = context.newPage();
|
||||
// const alertPromise = page.futureEvent("dialog");
|
||||
boolean[] didShowDialog = {false};
|
||||
page.onDialog(dialog -> didShowDialog[0] = true);
|
||||
page.evaluate("() => {\n" +
|
||||
" setTimeout(() => alert('hello'), 0);\n" +
|
||||
"}");
|
||||
// alertPromise;
|
||||
Instant start = Instant.now();
|
||||
while (!didShowDialog[0]) {
|
||||
page.waitForTimeout(100);
|
||||
assertTrue(Duration.between(start, Instant.now()).getSeconds() < 30, "Timed out");
|
||||
}
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ public class TestPageBasic extends TestBase {
|
||||
boolean[] didShowDialog = {false};
|
||||
newPage.onDialog(dialog -> {
|
||||
didShowDialog[0] = true;
|
||||
assertEquals(Dialog.Type.BEFOREUNLOAD, dialog.type());
|
||||
assertEquals("beforeunload", dialog.type());
|
||||
assertEquals("", dialog.defaultValue());
|
||||
if (isChromium()) {
|
||||
assertEquals("", dialog.message());
|
||||
|
||||
@ -1112,11 +1112,6 @@ class Interface extends TypeDefinition {
|
||||
|
||||
private void writeSharedTypes(List<String> output, String offset) {
|
||||
switch (jsonName) {
|
||||
case "Dialog": {
|
||||
output.add(offset + "enum Type { ALERT, BEFOREUNLOAD, CONFIRM, PROMPT }");
|
||||
output.add("");
|
||||
break;
|
||||
}
|
||||
case "Mouse": {
|
||||
output.add(offset + "enum Button { LEFT, MIDDLE, RIGHT }");
|
||||
output.add("");
|
||||
|
||||
@ -152,7 +152,6 @@ class Types {
|
||||
add("WebSocket.waitForEvent.optionsOrPredicate", "Function|Object", "String");
|
||||
|
||||
// Return structures
|
||||
add("Dialog.type", "string", "Type", new Empty());
|
||||
add("ConsoleMessage.location", "Object", "Location");
|
||||
add("ElementHandle.boundingBox", "Object|null", "BoundingBox", new Empty());
|
||||
add("Accessibility.snapshot", "Object|null", "AccessibilityNode", new Empty());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user