chore: use enum for dialog type (#59)

This commit is contained in:
Yury Semikhatsky 2020-10-30 10:44:32 -07:00 committed by GitHub
parent ad51236002
commit 92ae6cae49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 8 deletions

View File

@ -713,6 +713,11 @@ class Interface extends TypeDefinition {
private void writeSharedTypes(List<String> output, String offset) { private void writeSharedTypes(List<String> output, String offset) {
switch (jsonName) { switch (jsonName) {
case "Dialog": {
output.add(offset + "enum Type { ALERT, BEFOREUNLOAD, CONFIRM, PROMPT }");
output.add("");
break;
}
case "Mouse": { case "Mouse": {
output.add(offset + "enum Button { LEFT, MIDDLE, RIGHT }"); output.add(offset + "enum Button { LEFT, MIDDLE, RIGHT }");
output.add(""); output.add("");

View File

@ -179,6 +179,7 @@ class Types {
add("Worker.evaluateHandle.pageFunction", "function|string", "String"); add("Worker.evaluateHandle.pageFunction", "function|string", "String");
// Return structures // Return structures
add("Dialog.type", "string", "Type", new Empty());
add("ConsoleMessage.location", "Object", "Location"); add("ConsoleMessage.location", "Object", "Location");
add("ElementHandle.boundingBox", "Promise<null|Object>", "BoundingBox", new Empty()); add("ElementHandle.boundingBox", "Promise<null|Object>", "BoundingBox", new Empty());

View File

@ -19,6 +19,8 @@ package com.microsoft.playwright;
import java.util.*; import java.util.*;
public interface Dialog { public interface Dialog {
enum Type { ALERT, BEFOREUNLOAD, CONFIRM, PROMPT }
default void accept() { default void accept() {
accept(null); accept(null);
} }
@ -26,6 +28,6 @@ public interface Dialog {
String defaultValue(); String defaultValue();
void dismiss(); void dismiss();
String message(); String message();
String type(); Type type();
} }

View File

@ -18,6 +18,7 @@ package com.microsoft.playwright.impl;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.microsoft.playwright.Dialog; import com.microsoft.playwright.Dialog;
import com.microsoft.playwright.PlaywrightException;
public class DialogImpl extends ChannelOwner implements Dialog { public class DialogImpl extends ChannelOwner implements Dialog {
private boolean handled; private boolean handled;
@ -50,10 +51,15 @@ public class DialogImpl extends ChannelOwner implements Dialog {
return initializer.get("message").getAsString(); return initializer.get("message").getAsString();
} }
// public enum Type { Alert, BeforeUnload, Confirm, Prompt }
@Override @Override
public String type() { public Type type() {
return initializer.get("type").getAsString(); 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());
}
} }
boolean isHandled() { boolean isHandled() {

View File

@ -18,6 +18,8 @@ package com.microsoft.playwright;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static com.microsoft.playwright.Dialog.Type.ALERT;
import static com.microsoft.playwright.Dialog.Type.PROMPT;
import static com.microsoft.playwright.Page.EventType.DIALOG; import static com.microsoft.playwright.Page.EventType.DIALOG;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
@ -28,7 +30,7 @@ public class TestDialog extends TestBase {
void shouldFire() { void shouldFire() {
page.addListener(DIALOG, event -> { page.addListener(DIALOG, event -> {
Dialog dialog = (Dialog) event.data(); Dialog dialog = (Dialog) event.data();
assertEquals( "alert", dialog.type()); assertEquals(ALERT, dialog.type());
assertEquals( "", dialog.defaultValue()); assertEquals( "", dialog.defaultValue());
assertEquals( "yo", dialog.message()); assertEquals( "yo", dialog.message());
dialog.accept(); dialog.accept();
@ -40,7 +42,7 @@ public class TestDialog extends TestBase {
void shouldAllowAcceptingPrompts() { void shouldAllowAcceptingPrompts() {
page.addListener(DIALOG, event -> { page.addListener(DIALOG, event -> {
Dialog dialog = (Dialog) event.data(); Dialog dialog = (Dialog) event.data();
assertEquals("prompt", dialog.type()); assertEquals(PROMPT, dialog.type());
assertEquals("yes.", dialog.defaultValue()); assertEquals("yes.", dialog.defaultValue());
assertEquals("question?", dialog.message()); assertEquals("question?", dialog.message());
dialog.accept("answer!"); dialog.accept("answer!");

View File

@ -62,7 +62,7 @@ public class TestPageBasic extends TestBase {
newPage.addListener(DIALOG, event -> { newPage.addListener(DIALOG, event -> {
didShowDialog[0] = true; didShowDialog[0] = true;
Dialog dialog = (Dialog) event.data(); Dialog dialog = (Dialog) event.data();
assertEquals("beforeunload", dialog.type()); assertEquals(Dialog.Type.BEFOREUNLOAD, dialog.type());
assertEquals("", dialog.defaultValue()); assertEquals("", dialog.defaultValue());
if (isChromium) { if (isChromium) {
assertEquals("", dialog.message()); assertEquals("", dialog.message());

View File

@ -74,7 +74,6 @@ public class TestPageSetExtraHttpHeaders extends TestBase {
browser.newContext(new Browser.NewContextOptions().withExtraHTTPHeaders(mapOf("foo", null))); browser.newContext(new Browser.NewContextOptions().withExtraHTTPHeaders(mapOf("foo", null)));
fail("did not throw"); fail("did not throw");
} catch (PlaywrightException e) { } catch (PlaywrightException e) {
System.out.println(e.getMessage());
assertTrue(e.getMessage().contains("expected string, got undefined")); assertTrue(e.getMessage().contains("expected string, got undefined"));
} }
} }