chore: streamline enum serialization (#232)
This commit is contained in:
@@ -135,16 +135,6 @@ public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
||||
options = new ClickOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.remove("button");
|
||||
if (options.button != null) {
|
||||
params.addProperty("button", Serialization.toProtocol(options.button));
|
||||
}
|
||||
|
||||
params.remove("modifiers");
|
||||
if (options.modifiers != null) {
|
||||
params.add("modifiers", Serialization.toProtocol(options.modifiers));
|
||||
}
|
||||
|
||||
sendMessage("click", params);
|
||||
}
|
||||
|
||||
@@ -171,16 +161,6 @@ public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
||||
options = new DblclickOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.remove("button");
|
||||
if (options.button != null) {
|
||||
params.addProperty("button", Serialization.toProtocol(options.button));
|
||||
}
|
||||
|
||||
params.remove("modifiers");
|
||||
if (options.modifiers != null) {
|
||||
params.add("modifiers", Serialization.toProtocol(options.modifiers));
|
||||
}
|
||||
|
||||
sendMessage("dblclick", params);
|
||||
}
|
||||
|
||||
@@ -227,10 +207,6 @@ public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
||||
public void hover(HoverOptions options) {
|
||||
withLogging("ElementHandle.hover", () -> {
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.remove("modifiers");
|
||||
if (options.modifiers != null) {
|
||||
params.add("modifiers", Serialization.toProtocol(options.modifiers));
|
||||
}
|
||||
sendMessage("hover", params);
|
||||
});
|
||||
}
|
||||
@@ -323,10 +299,6 @@ public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
||||
sendMessage("press", params);
|
||||
}
|
||||
|
||||
private static String toProtocol(ScreenshotOptions.Type type) {
|
||||
return type.toString().toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] screenshot(ScreenshotOptions options) {
|
||||
return withLogging("ElementHandle.screenshot", () -> screenshotImpl(options));
|
||||
@@ -350,8 +322,6 @@ public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
||||
}
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.remove("type");
|
||||
params.addProperty("type", toProtocol(options.type));
|
||||
params.remove("path");
|
||||
JsonObject json = sendMessage("screenshot", params).getAsJsonObject();
|
||||
|
||||
@@ -448,10 +418,6 @@ public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
||||
options = new TapOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
if (options.modifiers != null) {
|
||||
params.remove("modifiers");
|
||||
params.add("modifiers", Serialization.toProtocol(options.modifiers));
|
||||
}
|
||||
sendMessage("tap", params);
|
||||
}
|
||||
|
||||
@@ -503,15 +469,15 @@ public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
||||
if (options == null) {
|
||||
options = new WaitForElementStateOptions();
|
||||
}
|
||||
if (state == null) {
|
||||
throw new IllegalArgumentException("State cannot be null");
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.addProperty("state", toProtocol(state));
|
||||
sendMessage("waitForElementState", params);
|
||||
}
|
||||
|
||||
private static String toProtocol(ElementState state) {
|
||||
if (state == null) {
|
||||
throw new IllegalArgumentException("State cannot by null");
|
||||
}
|
||||
return state.toString().toLowerCase();
|
||||
}
|
||||
|
||||
@@ -525,8 +491,6 @@ public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
||||
options = new WaitForSelectorOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.remove("state");
|
||||
params.addProperty("state", toProtocol(options.state));
|
||||
params.addProperty("selector", selector);
|
||||
JsonElement json = sendMessage("waitForSelector", params);
|
||||
JsonObject element = json.getAsJsonObject().getAsJsonObject("element");
|
||||
@@ -535,11 +499,4 @@ public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
|
||||
}
|
||||
return connection.getExistingObject(element.get("guid").getAsString());
|
||||
}
|
||||
|
||||
private static String toProtocol(WaitForSelectorOptions.State state) {
|
||||
if (state == null) {
|
||||
state = WaitForSelectorOptions.State.VISIBLE;
|
||||
}
|
||||
return state.toString().toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,17 +217,6 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.addProperty("selector", selector);
|
||||
|
||||
params.remove("button");
|
||||
if (options.button != null) {
|
||||
params.addProperty("button", Serialization.toProtocol(options.button));
|
||||
}
|
||||
|
||||
params.remove("modifiers");
|
||||
if (options.modifiers != null) {
|
||||
params.add("modifiers", Serialization.toProtocol(options.modifiers));
|
||||
}
|
||||
|
||||
sendMessage("click", params);
|
||||
}
|
||||
|
||||
@@ -251,17 +240,6 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.addProperty("selector", selector);
|
||||
|
||||
params.remove("button");
|
||||
if (options.button != null) {
|
||||
params.addProperty("button", Serialization.toProtocol(options.button));
|
||||
}
|
||||
|
||||
params.remove("modifiers");
|
||||
if (options.modifiers != null) {
|
||||
params.add("modifiers", Serialization.toProtocol(options.modifiers));
|
||||
}
|
||||
|
||||
sendMessage("dblclick", params);
|
||||
}
|
||||
|
||||
@@ -382,10 +360,6 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.addProperty("url", url);
|
||||
if (options.waitUntil != null) {
|
||||
params.remove("waitUntil");
|
||||
params.addProperty("waitUntil", toProtocol(options.waitUntil));
|
||||
}
|
||||
JsonElement result = sendMessage("goto", params);
|
||||
JsonObject jsonResponse = result.getAsJsonObject().getAsJsonObject("response");
|
||||
if (jsonResponse == null) {
|
||||
@@ -602,18 +576,6 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
return parseStringList(json.getAsJsonArray("values"));
|
||||
}
|
||||
|
||||
static String toProtocol(LoadState waitUntil) {
|
||||
if (waitUntil == null) {
|
||||
waitUntil = LoadState.LOAD;
|
||||
}
|
||||
switch (waitUntil) {
|
||||
case DOMCONTENTLOADED: return "domcontentloaded";
|
||||
case LOAD: return "load";
|
||||
case NETWORKIDLE: return "networkidle";
|
||||
default: throw new PlaywrightException("Unexpected value: " + waitUntil);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContent(String html, SetContentOptions options) {
|
||||
withLogging("Frame.setContent", () -> setContentImpl(html, options));
|
||||
@@ -625,8 +587,6 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.addProperty("html", html);
|
||||
params.remove("waitUntil");
|
||||
params.addProperty("waitUntil", toProtocol(options.waitUntil));
|
||||
sendMessage("setContent", params);
|
||||
}
|
||||
|
||||
@@ -663,10 +623,6 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
options = new TapOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.remove("modifiers");
|
||||
if (options.modifiers != null) {
|
||||
params.add("modifiers", Serialization.toProtocol(options.modifiers));
|
||||
}
|
||||
params.addProperty("selector", selector);
|
||||
sendMessage("tap", params);
|
||||
}
|
||||
@@ -888,10 +844,6 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
return runUntil(code, new WaitableRace<>(waitables));
|
||||
}
|
||||
|
||||
private static String toProtocol(WaitForSelectorOptions.State state) {
|
||||
return state.toString().toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ElementHandle waitForSelector(String selector, WaitForSelectorOptions options) {
|
||||
return withLogging("Frame.waitForSelector", () -> waitForSelectorImpl(selector, options));
|
||||
@@ -903,10 +855,6 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.addProperty("selector", selector);
|
||||
if (options.state != null) {
|
||||
params.remove("state");
|
||||
params.addProperty("state", toProtocol(options.state));
|
||||
}
|
||||
JsonElement json = sendMessage("waitForSelector", params);
|
||||
JsonObject element = json.getAsJsonObject().getAsJsonObject("element");
|
||||
if (element == null) {
|
||||
|
||||
@@ -43,10 +43,6 @@ class MouseImpl implements Mouse {
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.addProperty("x", x);
|
||||
params.addProperty("y", y);
|
||||
if (options.button != null) {
|
||||
params.remove("button");
|
||||
params.addProperty("button", toProtocol(options.button));
|
||||
}
|
||||
page.sendMessage("mouseClick", params);
|
||||
}
|
||||
|
||||
|
||||
@@ -762,8 +762,6 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
options = new GoBackOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.remove("waitUntil");
|
||||
params.addProperty("waitUntil", FrameImpl.toProtocol(options.waitUntil));
|
||||
JsonObject json = sendMessage("goBack", params).getAsJsonObject();
|
||||
if (json.has("response")) {
|
||||
return connection.getExistingObject(json.getAsJsonObject("response").get("guid").getAsString());
|
||||
@@ -781,8 +779,6 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
options = new GoForwardOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.remove("waitUntil");
|
||||
params.addProperty("waitUntil", FrameImpl.toProtocol(options.waitUntil));
|
||||
JsonObject json = sendMessage("goForward", params).getAsJsonObject();
|
||||
if (json.has("response")) {
|
||||
return connection.getExistingObject(json.getAsJsonObject("response").get("guid").getAsString());
|
||||
@@ -919,8 +915,6 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
options = new ReloadOptions();
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.remove("waitUntil");
|
||||
params.addProperty("waitUntil", FrameImpl.toProtocol(options.waitUntil));
|
||||
JsonObject json = sendMessage("reload", params).getAsJsonObject();
|
||||
if (json.has("response")) {
|
||||
return connection.getExistingObject(json.getAsJsonObject("response").get("guid").getAsString());
|
||||
@@ -954,10 +948,6 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
});
|
||||
}
|
||||
|
||||
private static String toProtocol(ScreenshotOptions.Type type) {
|
||||
return type.toString().toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] screenshot(ScreenshotOptions options) {
|
||||
return withLogging("Page.screenshot", () -> screenshotImpl(options));
|
||||
@@ -981,8 +971,6 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
}
|
||||
}
|
||||
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
|
||||
params.remove("type");
|
||||
params.addProperty("type", toProtocol(options.type));
|
||||
params.remove("path");
|
||||
JsonObject json = sendMessage("screenshot", params).getAsJsonObject();
|
||||
|
||||
|
||||
@@ -39,6 +39,15 @@ class Serialization {
|
||||
.registerTypeAdapter(BrowserContext.SameSite.class, new SameSiteAdapter().nullSafe())
|
||||
.registerTypeAdapter(ColorScheme.class, new ColorSchemeAdapter().nullSafe())
|
||||
.registerTypeAdapter(Page.EmulateMediaParams.Media.class, new MediaSerializer())
|
||||
.registerTypeAdapter(ElementHandle.ScreenshotOptions.Type.class, new ToLowerCaseSerializer<ElementHandle.ScreenshotOptions.Type>())
|
||||
.registerTypeAdapter(Page.ScreenshotOptions.Type.class, new ToLowerCaseSerializer<Page.ScreenshotOptions.Type>())
|
||||
.registerTypeAdapter(Mouse.Button.class, new ToLowerCaseSerializer<Mouse.Button>())
|
||||
.registerTypeAdapter(Frame.LoadState.class, new ToLowerCaseSerializer<Frame.LoadState>())
|
||||
.registerTypeAdapter(Page.LoadState.class, new ToLowerCaseSerializer<Page.LoadState>())
|
||||
.registerTypeAdapter(ElementHandle.WaitForSelectorOptions.State.class, new ToLowerCaseSerializer<ElementHandle.WaitForSelectorOptions.State>())
|
||||
.registerTypeAdapter(Frame.WaitForSelectorOptions.State.class, new ToLowerCaseSerializer<Frame.WaitForSelectorOptions.State>())
|
||||
.registerTypeAdapter(Page.WaitForSelectorOptions.State.class, new ToLowerCaseSerializer<Page.WaitForSelectorOptions.State>())
|
||||
.registerTypeAdapter((new TypeToken<Set<Keyboard.Modifier>>(){}).getType(), new KeyboardModifiersSerializer())
|
||||
.registerTypeAdapter(Optional.class, new OptionalSerializer())
|
||||
.registerTypeHierarchyAdapter(JSHandleImpl.class, new HandleSerializer())
|
||||
.registerTypeHierarchyAdapter(Map.class, new StringMapSerializer())
|
||||
@@ -180,36 +189,26 @@ class Serialization {
|
||||
throw new PlaywrightException("Unexpected result: " + gson().toJson(value));
|
||||
}
|
||||
|
||||
static String toProtocol(Mouse.Button button) {
|
||||
switch (button) {
|
||||
case LEFT:
|
||||
return "left";
|
||||
case RIGHT:
|
||||
return "right";
|
||||
case MIDDLE:
|
||||
return "middle";
|
||||
default:
|
||||
throw new PlaywrightException("Unexpected value: " + button);
|
||||
private static class KeyboardModifiersSerializer implements JsonSerializer<Set<Keyboard.Modifier>> {
|
||||
@Override
|
||||
public JsonArray serialize(Set<Keyboard.Modifier> modifiers, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonArray result = new JsonArray();
|
||||
if (modifiers.contains(Keyboard.Modifier.ALT)) {
|
||||
result.add("Alt");
|
||||
}
|
||||
if (modifiers.contains(Keyboard.Modifier.CONTROL)) {
|
||||
result.add("Control");
|
||||
}
|
||||
if (modifiers.contains(Keyboard.Modifier.META)) {
|
||||
result.add("Meta");
|
||||
}
|
||||
if (modifiers.contains(Keyboard.Modifier.SHIFT)) {
|
||||
result.add("Shift");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
static JsonArray toProtocol(Set<Keyboard.Modifier> modifiers) {
|
||||
JsonArray result = new JsonArray();
|
||||
if (modifiers.contains(Keyboard.Modifier.ALT)) {
|
||||
result.add("Alt");
|
||||
}
|
||||
if (modifiers.contains(Keyboard.Modifier.CONTROL)) {
|
||||
result.add("Control");
|
||||
}
|
||||
if (modifiers.contains(Keyboard.Modifier.META)) {
|
||||
result.add("Meta");
|
||||
}
|
||||
if (modifiers.contains(Keyboard.Modifier.SHIFT)) {
|
||||
result.add("Shift");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static JsonArray toJsonArray(FileChooser.FilePayload[] files) {
|
||||
JsonArray jsonFiles = new JsonArray();
|
||||
for (FileChooser.FilePayload p : files) {
|
||||
@@ -300,6 +299,13 @@ class Serialization {
|
||||
}
|
||||
}
|
||||
|
||||
private static class ToLowerCaseSerializer<E extends Enum<E>> implements JsonSerializer<E> {
|
||||
@Override
|
||||
public JsonElement serialize(E src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return new JsonPrimitive(src.toString().toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
private static class SameSiteAdapter extends TypeAdapter<BrowserContext.SameSite> {
|
||||
@Override
|
||||
public void write(JsonWriter out, BrowserContext.SameSite value) throws IOException {
|
||||
|
||||
Reference in New Issue
Block a user