fix(api): make Dialog.type() return string (#233)

This commit is contained in:
Yury Semikhatsky 2021-01-25 15:11:05 -08:00 committed by GitHub
parent 982cc6a3bc
commit e545829bf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 26 deletions

View File

@ -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();
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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());

View File

@ -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("");

View File

@ -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());