mirror of
https://github.com/microsoft/playwright-java.git
synced 2025-09-08 21:01:00 +00:00
chore: use enum for dialog type (#59)
This commit is contained in:
parent
ad51236002
commit
92ae6cae49
@ -713,6 +713,11 @@ 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("");
|
||||
|
@ -179,6 +179,7 @@ class Types {
|
||||
add("Worker.evaluateHandle.pageFunction", "function|string", "String");
|
||||
|
||||
// Return structures
|
||||
add("Dialog.type", "string", "Type", new Empty());
|
||||
add("ConsoleMessage.location", "Object", "Location");
|
||||
add("ElementHandle.boundingBox", "Promise<null|Object>", "BoundingBox", new Empty());
|
||||
|
||||
|
@ -19,6 +19,8 @@ package com.microsoft.playwright;
|
||||
import java.util.*;
|
||||
|
||||
public interface Dialog {
|
||||
enum Type { ALERT, BEFOREUNLOAD, CONFIRM, PROMPT }
|
||||
|
||||
default void accept() {
|
||||
accept(null);
|
||||
}
|
||||
@ -26,6 +28,6 @@ public interface Dialog {
|
||||
String defaultValue();
|
||||
void dismiss();
|
||||
String message();
|
||||
String type();
|
||||
Type type();
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@ package com.microsoft.playwright.impl;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.microsoft.playwright.Dialog;
|
||||
import com.microsoft.playwright.PlaywrightException;
|
||||
|
||||
public class DialogImpl extends ChannelOwner implements Dialog {
|
||||
private boolean handled;
|
||||
@ -50,10 +51,15 @@ public class DialogImpl extends ChannelOwner implements Dialog {
|
||||
return initializer.get("message").getAsString();
|
||||
}
|
||||
|
||||
// public enum Type { Alert, BeforeUnload, Confirm, Prompt }
|
||||
@Override
|
||||
public String type() {
|
||||
return initializer.get("type").getAsString();
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
boolean isHandled() {
|
||||
|
@ -18,6 +18,8 @@ package com.microsoft.playwright;
|
||||
|
||||
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 org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
@ -28,7 +30,7 @@ public class TestDialog extends TestBase {
|
||||
void shouldFire() {
|
||||
page.addListener(DIALOG, event -> {
|
||||
Dialog dialog = (Dialog) event.data();
|
||||
assertEquals( "alert", dialog.type());
|
||||
assertEquals(ALERT, dialog.type());
|
||||
assertEquals( "", dialog.defaultValue());
|
||||
assertEquals( "yo", dialog.message());
|
||||
dialog.accept();
|
||||
@ -40,7 +42,7 @@ public class TestDialog extends TestBase {
|
||||
void shouldAllowAcceptingPrompts() {
|
||||
page.addListener(DIALOG, event -> {
|
||||
Dialog dialog = (Dialog) event.data();
|
||||
assertEquals("prompt", dialog.type());
|
||||
assertEquals(PROMPT, dialog.type());
|
||||
assertEquals("yes.", dialog.defaultValue());
|
||||
assertEquals("question?", dialog.message());
|
||||
dialog.accept("answer!");
|
||||
|
@ -62,7 +62,7 @@ public class TestPageBasic extends TestBase {
|
||||
newPage.addListener(DIALOG, event -> {
|
||||
didShowDialog[0] = true;
|
||||
Dialog dialog = (Dialog) event.data();
|
||||
assertEquals("beforeunload", dialog.type());
|
||||
assertEquals(Dialog.Type.BEFOREUNLOAD, dialog.type());
|
||||
assertEquals("", dialog.defaultValue());
|
||||
if (isChromium) {
|
||||
assertEquals("", dialog.message());
|
||||
|
@ -74,7 +74,6 @@ public class TestPageSetExtraHttpHeaders extends TestBase {
|
||||
browser.newContext(new Browser.NewContextOptions().withExtraHTTPHeaders(mapOf("foo", null)));
|
||||
fail("did not throw");
|
||||
} catch (PlaywrightException e) {
|
||||
System.out.println(e.getMessage());
|
||||
assertTrue(e.getMessage().contains("expected string, got undefined"));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user