From 3da0ac6e85484241c94257ac02c59b0a0323bd0f Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Thu, 29 Oct 2020 17:58:02 -0700 Subject: [PATCH] chore: use Path instead of File in public API (#56) --- .../playwright/tools/ApiGenerator.java | 29 +++-- .../com/microsoft/playwright/tools/Types.java | 16 +-- .../microsoft/playwright/ChromiumBrowser.java | 6 +- .../microsoft/playwright/ElementHandle.java | 14 +-- .../com/microsoft/playwright/FileChooser.java | 10 +- .../java/com/microsoft/playwright/Frame.java | 18 +-- .../java/com/microsoft/playwright/Page.java | 26 ++--- .../microsoft/playwright/example/Main.java | 3 +- .../playwright/impl/BindingCall.java | 7 +- .../playwright/impl/BrowserContextImpl.java | 41 ++----- .../playwright/impl/BrowserImpl.java | 4 +- .../playwright/impl/BrowserTypeImpl.java | 46 +------- .../microsoft/playwright/impl/Connection.java | 6 +- .../playwright/impl/ConsoleMessageImpl.java | 5 +- .../playwright/impl/ElementHandleImpl.java | 54 ++++----- .../playwright/impl/FileChooserImpl.java | 3 +- .../microsoft/playwright/impl/FrameImpl.java | 77 ++++++------ .../playwright/impl/JSHandleImpl.java | 11 +- .../microsoft/playwright/impl/MouseImpl.java | 9 +- .../microsoft/playwright/impl/PageImpl.java | 25 ++-- .../playwright/impl/PlaywrightImpl.java | 2 +- .../playwright/impl/Serialization.java | 110 +++++++++++++++--- .../com/microsoft/playwright/impl/Utils.java | 27 +++-- .../microsoft/playwright/impl/WorkerImpl.java | 9 +- .../playwright/TestPageSetInputFiles.java | 8 +- .../com/microsoft/playwright/TestPdf.java | 7 +- 26 files changed, 301 insertions(+), 272 deletions(-) diff --git a/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java b/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java index 26fb946d..35ced21c 100644 --- a/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java +++ b/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java @@ -285,30 +285,30 @@ class Method extends Element { "void addCookies(List cookies);" }); customSignature.put("FileChooser.setFiles", new String[]{ - "default void setFiles(File file) { setFiles(file, null); }", - "default void setFiles(File file, SetFilesOptions options) { setFiles(new File[]{ file }, options); }", - "default void setFiles(File[] files) { setFiles(files, null); }", - "void setFiles(File[] files, SetFilesOptions options);", + "default void setFiles(Path file) { setFiles(file, null); }", + "default void setFiles(Path file, SetFilesOptions options) { setFiles(new Path[]{ file }, options); }", + "default void setFiles(Path[] files) { setFiles(files, null); }", + "void setFiles(Path[] files, SetFilesOptions options);", "default void setFiles(FileChooser.FilePayload file) { setFiles(file, null); }", "default void setFiles(FileChooser.FilePayload file, SetFilesOptions options) { setFiles(new FileChooser.FilePayload[]{ file }, options); }", "default void setFiles(FileChooser.FilePayload[] files) { setFiles(files, null); }", "void setFiles(FileChooser.FilePayload[] files, SetFilesOptions options);", }); customSignature.put("ElementHandle.setInputFiles", new String[]{ - "default void setInputFiles(File file) { setInputFiles(file, null); }", - "default void setInputFiles(File file, SetInputFilesOptions options) { setInputFiles(new File[]{ file }, options); }", - "default void setInputFiles(File[] files) { setInputFiles(files, null); }", - "void setInputFiles(File[] files, SetInputFilesOptions options);", + "default void setInputFiles(Path file) { setInputFiles(file, null); }", + "default void setInputFiles(Path file, SetInputFilesOptions options) { setInputFiles(new Path[]{ file }, options); }", + "default void setInputFiles(Path[] files) { setInputFiles(files, null); }", + "void setInputFiles(Path[] files, SetInputFilesOptions options);", "default void setInputFiles(FileChooser.FilePayload file) { setInputFiles(file, null); }", "default void setInputFiles(FileChooser.FilePayload file, SetInputFilesOptions options) { setInputFiles(new FileChooser.FilePayload[]{ file }, options); }", "default void setInputFiles(FileChooser.FilePayload[] files) { setInputFiles(files, null); }", "void setInputFiles(FileChooser.FilePayload[] files, SetInputFilesOptions options);", }); String[] setInputFilesWithSelector = { - "default void setInputFiles(String selector, File file) { setInputFiles(selector, file, null); }", - "default void setInputFiles(String selector, File file, SetInputFilesOptions options) { setInputFiles(selector, new File[]{ file }, options); }", - "default void setInputFiles(String selector, File[] files) { setInputFiles(selector, files, null); }", - "void setInputFiles(String selector, File[] files, SetInputFilesOptions options);", + "default void setInputFiles(String selector, Path file) { setInputFiles(selector, file, null); }", + "default void setInputFiles(String selector, Path file, SetInputFilesOptions options) { setInputFiles(selector, new Path[]{ file }, options); }", + "default void setInputFiles(String selector, Path[] files) { setInputFiles(selector, files, null); }", + "void setInputFiles(String selector, Path[] files, SetInputFilesOptions options);", "default void setInputFiles(String selector, FileChooser.FilePayload file) { setInputFiles(selector, file, null); }", "default void setInputFiles(String selector, FileChooser.FilePayload file, SetInputFilesOptions options) { setInputFiles(selector, new FileChooser.FilePayload[]{ file }, options); }", "default void setInputFiles(String selector, FileChooser.FilePayload[] files) { setInputFiles(selector, files, null); }", @@ -643,13 +643,10 @@ class Interface extends TypeDefinition { if (jsonName.equals("Route")) { output.add("import java.nio.charset.StandardCharsets;"); } - if (asList("Page", "Frame", "ElementHandle", "FileChooser", "ChromiumBrowser").contains(jsonName)) { - output.add("import java.io.File;"); - } if ("Download".equals(jsonName)) { output.add("import java.io.InputStream;"); } - if (asList("Download", "Route").contains(jsonName)) { + if (asList("Page", "Frame", "ElementHandle", "FileChooser", "ChromiumBrowser", "Download", "Route").contains(jsonName)) { output.add("import java.nio.file.Path;"); } output.add("import java.util.*;"); diff --git a/api-generator/src/main/java/com/microsoft/playwright/tools/Types.java b/api-generator/src/main/java/com/microsoft/playwright/tools/Types.java index 8a6aed18..6f6b04b1 100644 --- a/api-generator/src/main/java/com/microsoft/playwright/tools/Types.java +++ b/api-generator/src/main/java/com/microsoft/playwright/tools/Types.java @@ -95,16 +95,16 @@ class Types { add("BrowserType.launchPersistentContext.options.colorScheme", "\"dark\"|\"light\"|\"no-preference\"", "ColorScheme"); // File - add("Page.addScriptTag.options.path", "string", "File"); - add("Page.addStyleTag.options.path", "string", "File"); - add("Page.pdf.options.path", "string", "File"); - add("Page.screenshot.options.path", "string", "File"); - add("Frame.addScriptTag.options.path", "string", "File"); - add("Frame.addStyleTag.options.path", "string", "File"); - add("ElementHandle.screenshot.options.path", "string", "File"); + add("Page.addScriptTag.options.path", "string", "Path"); + add("Page.addStyleTag.options.path", "string", "Path"); + add("Page.pdf.options.path", "string", "Path"); + add("Page.screenshot.options.path", "string", "Path"); + add("Frame.addScriptTag.options.path", "string", "Path"); + add("Frame.addStyleTag.options.path", "string", "Path"); + add("ElementHandle.screenshot.options.path", "string", "Path"); add("Route.fulfill.response.path", "string", "Path"); add("Route.fulfill.response.status", "number", "int"); - add("ChromiumBrowser.startTracing.options.path", "string", "File"); + add("ChromiumBrowser.startTracing.options.path", "string", "Path"); // Route add("BrowserContext.route.handler", "function(Route, Request)", "BiConsumer"); diff --git a/playwright/src/main/java/com/microsoft/playwright/ChromiumBrowser.java b/playwright/src/main/java/com/microsoft/playwright/ChromiumBrowser.java index 34031270..a5688e5f 100644 --- a/playwright/src/main/java/com/microsoft/playwright/ChromiumBrowser.java +++ b/playwright/src/main/java/com/microsoft/playwright/ChromiumBrowser.java @@ -16,16 +16,16 @@ package com.microsoft.playwright; -import java.io.File; +import java.nio.file.Path; import java.util.*; public interface ChromiumBrowser extends Browser { class StartTracingOptions { - public File path; + public Path path; public Boolean screenshots; public List categories; - public StartTracingOptions withPath(File path) { + public StartTracingOptions withPath(Path path) { this.path = path; return this; } diff --git a/playwright/src/main/java/com/microsoft/playwright/ElementHandle.java b/playwright/src/main/java/com/microsoft/playwright/ElementHandle.java index facb0864..21db8e74 100644 --- a/playwright/src/main/java/com/microsoft/playwright/ElementHandle.java +++ b/playwright/src/main/java/com/microsoft/playwright/ElementHandle.java @@ -16,7 +16,7 @@ package com.microsoft.playwright; -import java.io.File; +import java.nio.file.Path; import java.util.*; public interface ElementHandle extends JSHandle { @@ -211,13 +211,13 @@ public interface ElementHandle extends JSHandle { } class ScreenshotOptions { public enum Type { JPEG, PNG } - public File path; + public Path path; public Type type; public Integer quality; public Boolean omitBackground; public Integer timeout; - public ScreenshotOptions withPath(File path) { + public ScreenshotOptions withPath(Path path) { this.path = path; return this; } @@ -434,10 +434,10 @@ public interface ElementHandle extends JSHandle { selectText(null); } void selectText(SelectTextOptions options); - default void setInputFiles(File file) { setInputFiles(file, null); } - default void setInputFiles(File file, SetInputFilesOptions options) { setInputFiles(new File[]{ file }, options); } - default void setInputFiles(File[] files) { setInputFiles(files, null); } - void setInputFiles(File[] files, SetInputFilesOptions options); + default void setInputFiles(Path file) { setInputFiles(file, null); } + default void setInputFiles(Path file, SetInputFilesOptions options) { setInputFiles(new Path[]{ file }, options); } + default void setInputFiles(Path[] files) { setInputFiles(files, null); } + void setInputFiles(Path[] files, SetInputFilesOptions options); default void setInputFiles(FileChooser.FilePayload file) { setInputFiles(file, null); } default void setInputFiles(FileChooser.FilePayload file, SetInputFilesOptions options) { setInputFiles(new FileChooser.FilePayload[]{ file }, options); } default void setInputFiles(FileChooser.FilePayload[] files) { setInputFiles(files, null); } diff --git a/playwright/src/main/java/com/microsoft/playwright/FileChooser.java b/playwright/src/main/java/com/microsoft/playwright/FileChooser.java index 35283938..bb6bc505 100644 --- a/playwright/src/main/java/com/microsoft/playwright/FileChooser.java +++ b/playwright/src/main/java/com/microsoft/playwright/FileChooser.java @@ -16,7 +16,7 @@ package com.microsoft.playwright; -import java.io.File; +import java.nio.file.Path; import java.util.*; public interface FileChooser { @@ -48,10 +48,10 @@ public interface FileChooser { ElementHandle element(); boolean isMultiple(); Page page(); - default void setFiles(File file) { setFiles(file, null); } - default void setFiles(File file, SetFilesOptions options) { setFiles(new File[]{ file }, options); } - default void setFiles(File[] files) { setFiles(files, null); } - void setFiles(File[] files, SetFilesOptions options); + default void setFiles(Path file) { setFiles(file, null); } + default void setFiles(Path file, SetFilesOptions options) { setFiles(new Path[]{ file }, options); } + default void setFiles(Path[] files) { setFiles(files, null); } + void setFiles(Path[] files, SetFilesOptions options); default void setFiles(FileChooser.FilePayload file) { setFiles(file, null); } default void setFiles(FileChooser.FilePayload file, SetFilesOptions options) { setFiles(new FileChooser.FilePayload[]{ file }, options); } default void setFiles(FileChooser.FilePayload[] files) { setFiles(files, null); } diff --git a/playwright/src/main/java/com/microsoft/playwright/Frame.java b/playwright/src/main/java/com/microsoft/playwright/Frame.java index ed5f5c57..be32fe8a 100644 --- a/playwright/src/main/java/com/microsoft/playwright/Frame.java +++ b/playwright/src/main/java/com/microsoft/playwright/Frame.java @@ -16,7 +16,7 @@ package com.microsoft.playwright; -import java.io.File; +import java.nio.file.Path; import java.util.*; import java.util.function.Predicate; import java.util.regex.Pattern; @@ -25,7 +25,7 @@ public interface Frame { enum LoadState { DOMCONTENTLOADED, LOAD, NETWORKIDLE } class AddScriptTagOptions { public String url; - public File path; + public Path path; public String content; public String type; @@ -33,7 +33,7 @@ public interface Frame { this.url = url; return this; } - public AddScriptTagOptions withPath(File path) { + public AddScriptTagOptions withPath(Path path) { this.path = path; return this; } @@ -48,14 +48,14 @@ public interface Frame { } class AddStyleTagOptions { public String url; - public File path; + public Path path; public String content; public AddStyleTagOptions withUrl(String url) { this.url = url; return this; } - public AddStyleTagOptions withPath(File path) { + public AddStyleTagOptions withPath(Path path) { this.path = path; return this; } @@ -555,10 +555,10 @@ public interface Frame { setContent(html, null); } void setContent(String html, SetContentOptions options); - default void setInputFiles(String selector, File file) { setInputFiles(selector, file, null); } - default void setInputFiles(String selector, File file, SetInputFilesOptions options) { setInputFiles(selector, new File[]{ file }, options); } - default void setInputFiles(String selector, File[] files) { setInputFiles(selector, files, null); } - void setInputFiles(String selector, File[] files, SetInputFilesOptions options); + default void setInputFiles(String selector, Path file) { setInputFiles(selector, file, null); } + default void setInputFiles(String selector, Path file, SetInputFilesOptions options) { setInputFiles(selector, new Path[]{ file }, options); } + default void setInputFiles(String selector, Path[] files) { setInputFiles(selector, files, null); } + void setInputFiles(String selector, Path[] files, SetInputFilesOptions options); default void setInputFiles(String selector, FileChooser.FilePayload file) { setInputFiles(selector, file, null); } default void setInputFiles(String selector, FileChooser.FilePayload file, SetInputFilesOptions options) { setInputFiles(selector, new FileChooser.FilePayload[]{ file }, options); } default void setInputFiles(String selector, FileChooser.FilePayload[] files) { setInputFiles(selector, files, null); } diff --git a/playwright/src/main/java/com/microsoft/playwright/Page.java b/playwright/src/main/java/com/microsoft/playwright/Page.java index 5a4d4c10..90322a4e 100644 --- a/playwright/src/main/java/com/microsoft/playwright/Page.java +++ b/playwright/src/main/java/com/microsoft/playwright/Page.java @@ -16,7 +16,7 @@ package com.microsoft.playwright; -import java.io.File; +import java.nio.file.Path; import java.util.*; import java.util.function.BiConsumer; import java.util.function.Predicate; @@ -108,7 +108,7 @@ public interface Page { } class AddScriptTagOptions { public String url; - public File path; + public Path path; public String content; public String type; @@ -116,7 +116,7 @@ public interface Page { this.url = url; return this; } - public AddScriptTagOptions withPath(File path) { + public AddScriptTagOptions withPath(Path path) { this.path = path; return this; } @@ -131,14 +131,14 @@ public interface Page { } class AddStyleTagOptions { public String url; - public File path; + public Path path; public String content; public AddStyleTagOptions withUrl(String url) { this.url = url; return this; } - public AddStyleTagOptions withPath(File path) { + public AddStyleTagOptions withPath(Path path) { this.path = path; return this; } @@ -428,7 +428,7 @@ public interface Page { return this; } } - public File path; + public Path path; public Integer scale; public Boolean displayHeaderFooter; public String headerTemplate; @@ -442,7 +442,7 @@ public interface Page { public Margin margin; public Boolean preferCSSPageSize; - public PdfOptions withPath(File path) { + public PdfOptions withPath(Path path) { this.path = path; return this; } @@ -557,7 +557,7 @@ public interface Page { return this; } } - public File path; + public Path path; public Type type; public Integer quality; public Boolean fullPage; @@ -565,7 +565,7 @@ public interface Page { public Boolean omitBackground; public Integer timeout; - public ScreenshotOptions withPath(File path) { + public ScreenshotOptions withPath(Path path) { this.path = path; return this; } @@ -929,10 +929,10 @@ public interface Page { void setDefaultNavigationTimeout(int timeout); void setDefaultTimeout(int timeout); void setExtraHTTPHeaders(Map headers); - default void setInputFiles(String selector, File file) { setInputFiles(selector, file, null); } - default void setInputFiles(String selector, File file, SetInputFilesOptions options) { setInputFiles(selector, new File[]{ file }, options); } - default void setInputFiles(String selector, File[] files) { setInputFiles(selector, files, null); } - void setInputFiles(String selector, File[] files, SetInputFilesOptions options); + default void setInputFiles(String selector, Path file) { setInputFiles(selector, file, null); } + default void setInputFiles(String selector, Path file, SetInputFilesOptions options) { setInputFiles(selector, new Path[]{ file }, options); } + default void setInputFiles(String selector, Path[] files) { setInputFiles(selector, files, null); } + void setInputFiles(String selector, Path[] files, SetInputFilesOptions options); default void setInputFiles(String selector, FileChooser.FilePayload file) { setInputFiles(selector, file, null); } default void setInputFiles(String selector, FileChooser.FilePayload file, SetInputFilesOptions options) { setInputFiles(selector, new FileChooser.FilePayload[]{ file }, options); } default void setInputFiles(String selector, FileChooser.FilePayload[] files) { setInputFiles(selector, files, null); } diff --git a/playwright/src/main/java/com/microsoft/playwright/example/Main.java b/playwright/src/main/java/com/microsoft/playwright/example/Main.java index 56cd5948..bafc5349 100644 --- a/playwright/src/main/java/com/microsoft/playwright/example/Main.java +++ b/playwright/src/main/java/com/microsoft/playwright/example/Main.java @@ -18,6 +18,7 @@ package com.microsoft.playwright.example; import com.microsoft.playwright.*; import java.io.File; +import java.nio.file.Paths; public class Main { public static void main(String[] args) { @@ -28,7 +29,7 @@ public class Main { Page page = context.newPage(); page.navigate("https://webkit.org"); page.click("text=check feature status"); - page.screenshot(new Page.ScreenshotOptions().withPath(new File("s.png"))); + page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("s.png"))); browser.close(); } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/BindingCall.java b/playwright/src/main/java/com/microsoft/playwright/impl/BindingCall.java index 6152af43..71e961b0 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/BindingCall.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/BindingCall.java @@ -16,7 +16,6 @@ package com.microsoft.playwright.impl; -import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.microsoft.playwright.BrowserContext; @@ -71,17 +70,17 @@ class BindingCall extends ChannelOwner { args.add(handle); } else { for (JsonElement arg : initializer.getAsJsonArray("args")) { - args.add(deserialize(new Gson().fromJson(arg, SerializedValue.class))); + args.add(deserialize(gson().fromJson(arg, SerializedValue.class))); } } Object result = binding.call(source, args.toArray()); JsonObject params = new JsonObject(); - params.add("result", new Gson().toJsonTree(serializeArgument(result))); + params.add("result", gson().toJsonTree(serializeArgument(result))); sendMessage("resolve", params); } catch (RuntimeException exception) { JsonObject params = new JsonObject(); - params.add("error", new Gson().toJsonTree(serializeError(exception))); + params.add("error", gson().toJsonTree(serializeError(exception))); sendMessage("reject", params); } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java index 18aba368..45963797 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java @@ -27,6 +27,7 @@ import java.util.function.BiConsumer; import java.util.function.Predicate; import java.util.regex.Pattern; +import static com.microsoft.playwright.impl.Serialization.gson; import static com.microsoft.playwright.impl.Utils.isFunctionBody; import static com.microsoft.playwright.impl.Utils.isSafeCloseError; @@ -77,7 +78,7 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext { @Override public void addCookies(List cookies) { JsonObject params = new JsonObject(); - params.add("cookies", new Gson().toJsonTree(cookies)); + params.add("cookies", gson().toJsonTree(cookies)); sendMessage("addCookies", params); } @@ -107,43 +108,15 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext { sendMessage("clearPermissions"); } - private static class SameSiteAdapter extends TypeAdapter { - @Override - public void write(JsonWriter out, SameSite value) throws IOException { - String stringValue; - switch (value) { - case STRICT: - stringValue = "Strict"; - break; - case LAX: - stringValue = "Lax"; - break; - case NONE: - stringValue = "None"; - break; - default: - throw new PlaywrightException("Unexpected value: " + value); - } - out.value(stringValue); - } - - @Override - public SameSite read(JsonReader in) throws IOException { - String value = in.nextString(); - return SameSite.valueOf(value.toUpperCase()); - } - } - @Override public List cookies(List urls) { JsonObject params = new JsonObject(); if (urls == null) { urls = Collections.emptyList(); } - params.add("urls", new Gson().toJsonTree(urls)); + params.add("urls", gson().toJsonTree(urls)); JsonObject json = sendMessage("cookies", params).getAsJsonObject(); - Gson gson = new GsonBuilder().registerTypeAdapter(SameSite.class, new SameSiteAdapter().nullSafe()).create(); - Cookie[] cookies = gson.fromJson(json.getAsJsonArray("cookies"), Cookie[].class); + Cookie[] cookies = gson().fromJson(json.getAsJsonArray("cookies"), Cookie[].class); return Arrays.asList(cookies); } @@ -180,8 +153,8 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext { if (permissions == null) { permissions = Collections.emptyList(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); - params.add("permissions", new Gson().toJsonTree(permissions)); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); + params.add("permissions", gson().toJsonTree(permissions)); sendMessage("grantPermissions", params); } @@ -257,7 +230,7 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext { public void setGeolocation(Geolocation geolocation) { JsonObject params = new JsonObject(); if (geolocation != null) { - params.add("geolocation", new Gson().toJsonTree(geolocation)); + params.add("geolocation", gson().toJsonTree(geolocation)); } sendMessage("setGeolocation", params); } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserImpl.java index b1f52eb7..4dec9c72 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserImpl.java @@ -16,7 +16,6 @@ package com.microsoft.playwright.impl; -import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.microsoft.playwright.*; @@ -26,6 +25,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import static com.microsoft.playwright.impl.Serialization.gson; import static com.microsoft.playwright.impl.Utils.convertViaJson; import static com.microsoft.playwright.impl.Utils.isSafeCloseError; @@ -74,7 +74,7 @@ class BrowserImpl extends ChannelOwner implements Browser { if (options == null) { options = new NewContextOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); if (options.extraHTTPHeaders != null) { params.remove("extraHTTPHeaders"); params.add("extraHTTPHeaders", Serialization.toProtocol(options.extraHTTPHeaders)); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserTypeImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserTypeImpl.java index a5a267a2..f08ef2f5 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/BrowserTypeImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/BrowserTypeImpl.java @@ -16,15 +16,12 @@ package com.microsoft.playwright.impl; -import com.google.gson.*; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.microsoft.playwright.BrowserContext; import com.microsoft.playwright.BrowserType; -import com.microsoft.playwright.PlaywrightException; -import java.io.IOException; -import java.util.Map; +import static com.microsoft.playwright.impl.Serialization.gson; class BrowserTypeImpl extends ChannelOwner implements BrowserType { BrowserTypeImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) { @@ -36,7 +33,7 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType { if (options == null) { options = new LaunchOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); JsonElement result = sendMessage("launch", params); return connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("browser").get("guid").getAsString()); } @@ -46,45 +43,12 @@ class BrowserTypeImpl extends ChannelOwner implements BrowserType { } - private static class ColorSchemeAdapter extends TypeAdapter { - @Override - public void write(JsonWriter out, LaunchPersistentContextOptions.ColorScheme value) throws IOException { - String stringValue; - switch (value) { - case DARK: - stringValue = "dark"; - break; - case LIGHT: - stringValue = "light"; - break; - case NO_PREFERENCE: - stringValue = "no-preference"; - break; - default: - throw new PlaywrightException("Unexpected value: " + value); - } - out.value(stringValue); - } - - @Override - public LaunchPersistentContextOptions.ColorScheme read(JsonReader in) throws IOException { - String value = in.nextString(); - switch (value) { - case "dark": return LaunchPersistentContextOptions.ColorScheme.DARK; - case "light": return LaunchPersistentContextOptions.ColorScheme.LIGHT; - case "no-preference": return LaunchPersistentContextOptions.ColorScheme.NO_PREFERENCE; - default: throw new PlaywrightException("Unexpected value: " + value); - } - } - } - @Override public BrowserContext launchPersistentContext(String userDataDir, LaunchPersistentContextOptions options) { if (options == null) { options = new LaunchPersistentContextOptions(); } - Gson gson = new GsonBuilder().registerTypeAdapter(LaunchPersistentContextOptions.ColorScheme.class, new ColorSchemeAdapter().nullSafe()).create(); - JsonObject params = gson.toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); if (options.extraHTTPHeaders != null) { params.remove("extraHTTPHeaders"); params.add("extraHTTPHeaders", Serialization.toProtocol(options.extraHTTPHeaders)); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/Connection.java b/playwright/src/main/java/com/microsoft/playwright/impl/Connection.java index e49c140b..2b7bc13e 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/Connection.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Connection.java @@ -26,6 +26,8 @@ import java.time.Duration; import java.util.HashMap; import java.util.Map; +import static com.microsoft.playwright.impl.Serialization.gson; + class Message { int id; String guid; @@ -87,7 +89,7 @@ public class Connection { message.addProperty("guid", guid); message.addProperty("method", method); message.add("params", params); - transport.send(new Gson().toJson(message)); + transport.send(gson().toJson(message)); return result; } @@ -119,7 +121,7 @@ public class Connection { if (messageString == null) { return; } - Gson gson = new Gson(); + Gson gson = gson(); Message message = gson.fromJson(messageString, Message.class); dispatch(message); } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/ConsoleMessageImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/ConsoleMessageImpl.java index e7d18cbe..3f97fee6 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/ConsoleMessageImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/ConsoleMessageImpl.java @@ -16,7 +16,6 @@ package com.microsoft.playwright.impl; -import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.microsoft.playwright.ConsoleMessage; @@ -25,6 +24,8 @@ import com.microsoft.playwright.JSHandle; import java.util.ArrayList; import java.util.List; +import static com.microsoft.playwright.impl.Serialization.gson; + public class ConsoleMessageImpl extends ChannelOwner implements ConsoleMessage { public ConsoleMessageImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) { super(parent, type, guid, initializer); @@ -48,6 +49,6 @@ public class ConsoleMessageImpl extends ChannelOwner implements ConsoleMessage { } public Location location() { - return new Gson().fromJson(initializer.get("location"), Location.class); + return gson().fromJson(initializer.get("location"), Location.class); } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/ElementHandleImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/ElementHandleImpl.java index 48363068..a0ec27d8 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/ElementHandleImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/ElementHandleImpl.java @@ -26,6 +26,7 @@ import com.microsoft.playwright.FileChooser; import com.microsoft.playwright.Frame; import java.io.File; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Base64; import java.util.List; @@ -77,9 +78,9 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { params.addProperty("selector", selector); params.addProperty("expression", pageFunction); params.addProperty("isFunction", isFunctionBody(pageFunction)); - params.add("arg", new Gson().toJsonTree(serializeArgument(arg))); + params.add("arg", gson().toJsonTree(serializeArgument(arg))); JsonElement json = sendMessage("evalOnSelector", params); - SerializedValue value = new Gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); + SerializedValue value = gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); return deserialize(value); } @@ -89,9 +90,9 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { params.addProperty("selector", selector); params.addProperty("expression", pageFunction); params.addProperty("isFunction", isFunctionBody(pageFunction)); - params.add("arg", new Gson().toJsonTree(serializeArgument(arg))); + params.add("arg", gson().toJsonTree(serializeArgument(arg))); JsonElement json = sendMessage("evalOnSelectorAll", params); - SerializedValue value = new Gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); + SerializedValue value = gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); return deserialize(value); } @@ -101,7 +102,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (!json.has("value")) { return null; } - return new Gson().fromJson(json.get("value"), BoundingBox.class); + return gson().fromJson(json.get("value"), BoundingBox.class); } @Override @@ -109,7 +110,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new CheckOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); sendMessage("check", params); } @@ -118,7 +119,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new ClickOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.remove("button"); if (options.button != null) { params.addProperty("button", Serialization.toProtocol(options.button)); @@ -146,7 +147,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new DblclickOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.remove("button"); if (options.button != null) { params.addProperty("button", Serialization.toProtocol(options.button)); @@ -164,7 +165,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { public void dispatchEvent(String type, Object eventInit) { JsonObject params = new JsonObject(); params.addProperty("type", type); - params.add("eventInit", new Gson().toJsonTree(serializeArgument(eventInit))); + params.add("eventInit", gson().toJsonTree(serializeArgument(eventInit))); sendMessage("dispatchEvent", params); } @@ -173,7 +174,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new FillOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("value", value); sendMessage("fill", params); } @@ -193,7 +194,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { @Override public void hover(HoverOptions options) { - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.remove("modifiers"); if (options.modifiers != null) { params.add("modifiers", Serialization.toProtocol(options.modifiers)); @@ -227,7 +228,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new PressOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("key", key); sendMessage("press", params); } @@ -244,16 +245,17 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options.type == null) { options.type = ScreenshotOptions.Type.PNG; if (options.path != null) { - int extStart = options.path.getName().lastIndexOf('.'); + String fileName = options.path.getFileName().toString(); + int extStart = fileName.lastIndexOf('.'); if (extStart != -1) { - String extension = options.path.getName().substring(extStart).toLowerCase(); + String extension = fileName.substring(extStart).toLowerCase(); if (".jpeg".equals(extension) || ".jpg".equals(extension)) { options.type = ScreenshotOptions.Type.JPEG; } } } } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.remove("type"); params.addProperty("type", toProtocol(options.type)); params.remove("path"); @@ -271,7 +273,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new ScrollIntoViewIfNeededOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); sendMessage("scrollIntoViewIfNeeded", params); } @@ -280,9 +282,9 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new SelectOptionOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); if (values != null) { - params.add("options", new Gson().toJsonTree(values)); + params.add("options", gson().toJsonTree(values)); } return selectOption(params); } @@ -292,7 +294,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new SelectOptionOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); if (values != null) { params.add("elements", Serialization.toProtocol(values)); } @@ -309,12 +311,12 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new SelectTextOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); sendMessage("selectText", params); } @Override - public void setInputFiles(File[] files, SetInputFilesOptions options) { + public void setInputFiles(Path[] files, SetInputFilesOptions options) { setInputFiles(Utils.toFilePayloads(files), options); } @@ -323,7 +325,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new SetInputFilesOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.add("files", Serialization.toJsonArray(files)); sendMessage("setInputFiles", params); } @@ -339,7 +341,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new TypeOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("text", text); sendMessage("type", params); } @@ -349,7 +351,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new UncheckOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); sendMessage("uncheck", params); } @@ -358,7 +360,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new WaitForElementStateOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("state", toProtocol(state)); return toDeferred(sendMessageAsync("waitForElementState", params).apply(json -> null)); } @@ -375,7 +377,7 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle { if (options == null) { options = new WaitForSelectorOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.remove("state"); params.addProperty("state", toProtocol(options.state)); params.addProperty("selector", selector); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/FileChooserImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/FileChooserImpl.java index cdc475ea..993ac0db 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/FileChooserImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/FileChooserImpl.java @@ -23,6 +23,7 @@ import com.microsoft.playwright.FileChooser; import com.microsoft.playwright.Page; import java.io.File; +import java.nio.file.Path; import static com.microsoft.playwright.impl.Utils.convertViaJson; @@ -53,7 +54,7 @@ class FileChooserImpl implements FileChooser { } @Override - public void setFiles(File[] files, SetFilesOptions options) { + public void setFiles(Path[] files, SetFilesOptions options) { setFiles(Utils.toFilePayloads(files), options); } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/FrameImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/FrameImpl.java index d2cc7e0b..f1fa659e 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/FrameImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/FrameImpl.java @@ -26,6 +26,7 @@ import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.util.*; import static com.microsoft.playwright.Frame.LoadState.*; @@ -100,9 +101,9 @@ public class FrameImpl extends ChannelOwner implements Frame { params.addProperty("selector", selector); params.addProperty("expression", pageFunction); params.addProperty("isFunction", isFunctionBody(pageFunction)); - params.add("arg", new Gson().toJsonTree(serializeArgument(arg))); + params.add("arg", gson().toJsonTree(serializeArgument(arg))); JsonElement json = sendMessage("evalOnSelector", params); - SerializedValue value = new Gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); + SerializedValue value = gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); return deserialize(value); } @@ -112,9 +113,9 @@ public class FrameImpl extends ChannelOwner implements Frame { params.addProperty("selector", selector); params.addProperty("expression", pageFunction); params.addProperty("isFunction", isFunctionBody(pageFunction)); - params.add("arg", new Gson().toJsonTree(serializeArgument(arg))); + params.add("arg", gson().toJsonTree(serializeArgument(arg))); JsonElement json = sendMessage("evalOnSelectorAll", params); - SerializedValue value = new Gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); + SerializedValue value = gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); return deserialize(value); } @@ -123,17 +124,17 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new AddScriptTagOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); if (options.path != null) { params.remove("path"); byte[] encoded; try { - encoded = Files.readAllBytes(options.path.toPath()); + encoded = Files.readAllBytes(options.path); } catch (IOException e) { throw new PlaywrightException("Failed to read from file", e); } String content = new String(encoded, StandardCharsets.UTF_8); - content += "//# sourceURL=" + options.path.getPath().replace("\n", ""); + content += "//# sourceURL=" + options.path.toString().replace("\n", ""); params.addProperty("content", content); } JsonElement json = sendMessage("addScriptTag", params); @@ -145,17 +146,17 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new AddStyleTagOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); if (options.path != null) { params.remove("path"); byte[] encoded; try { - encoded = Files.readAllBytes(options.path.toPath()); + encoded = Files.readAllBytes(options.path); } catch (IOException e) { throw new PlaywrightException("Failed to read from file", e); } String content = new String(encoded, StandardCharsets.UTF_8); - content += "/*# sourceURL=" + options.path.getPath().replace("\n", "") + "*/"; + content += "/*# sourceURL=" + options.path.toString().replace("\n", "") + "*/"; params.addProperty("content", content); } JsonElement json = sendMessage("addStyleTag", params); @@ -167,7 +168,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new CheckOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); sendMessage("check", params); } @@ -182,7 +183,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new ClickOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); params.remove("button"); @@ -208,7 +209,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new DblclickOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); params.remove("button"); @@ -229,10 +230,10 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new DispatchEventOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); params.addProperty("type", type); - params.add("eventInit", new Gson().toJsonTree(serializeArgument(eventInit))); + params.add("eventInit", gson().toJsonTree(serializeArgument(eventInit))); sendMessage("dispatchEvent", params); } @@ -242,9 +243,9 @@ public class FrameImpl extends ChannelOwner implements Frame { params.addProperty("expression", expression); params.addProperty("world", "main"); params.addProperty("isFunction", isFunctionBody(expression)); - params.add("arg", new Gson().toJsonTree(serializeArgument(arg))); + params.add("arg", gson().toJsonTree(serializeArgument(arg))); JsonElement json = sendMessage("evaluateExpression", params); - SerializedValue value = new Gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); + SerializedValue value = gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); return deserialize(value); } @@ -254,7 +255,7 @@ public class FrameImpl extends ChannelOwner implements Frame { params.addProperty("expression", pageFunction); params.addProperty("world", "main"); params.addProperty("isFunction", isFunctionBody(pageFunction)); - params.add("arg", new Gson().toJsonTree(serializeArgument(arg))); + params.add("arg", gson().toJsonTree(serializeArgument(arg))); JsonElement json = sendMessage("evaluateExpressionHandle", params); return connection.getExistingObject(json.getAsJsonObject().getAsJsonObject("handle").get("guid").getAsString()); } @@ -264,7 +265,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new FillOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); params.addProperty("value", value); sendMessage("fill", params); @@ -275,7 +276,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new FocusOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); sendMessage("focus", params); } @@ -291,7 +292,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new GetAttributeOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); params.addProperty("name", name); JsonObject json = sendMessage("getAttribute", params).getAsJsonObject(); @@ -306,7 +307,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new NavigateOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("url", url); if (options.waitUntil != null) { params.remove("waitUntil"); @@ -325,7 +326,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new HoverOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); sendMessage("hover", params); } @@ -335,7 +336,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new InnerHTMLOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); JsonObject json = sendMessage("innerHTML", params).getAsJsonObject(); return json.get("value").getAsString(); @@ -346,7 +347,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new InnerTextOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); JsonObject json = sendMessage("innerText", params).getAsJsonObject(); return json.get("value").getAsString(); @@ -377,7 +378,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new PressOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); params.addProperty("key", key); sendMessage("press", params); @@ -388,10 +389,10 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new SelectOptionOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); if (values != null) { - params.add("options", new Gson().toJsonTree(values)); + params.add("options", gson().toJsonTree(values)); } return selectOption(params); } @@ -401,7 +402,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new SelectOptionOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); if (values != null) { params.add("elements", Serialization.toProtocol(values)); @@ -431,7 +432,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new SetContentOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("html", html); params.remove("waitUntil"); params.addProperty("waitUntil", toProtocol(options.waitUntil)); @@ -439,7 +440,7 @@ public class FrameImpl extends ChannelOwner implements Frame { } @Override - public void setInputFiles(String selector, File[] files, SetInputFilesOptions options) { + public void setInputFiles(String selector, Path[] files, SetInputFilesOptions options) { setInputFiles(selector, Utils.toFilePayloads(files), options); } @@ -448,7 +449,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new SetInputFilesOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); params.add("files", toJsonArray(files)); sendMessage("setInputFiles", params); @@ -459,7 +460,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new TextContentOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); return sendMessage("textContent", params).getAsJsonObject().get("value").getAsString(); } @@ -475,7 +476,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new TypeOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); params.addProperty("text", text); sendMessage("type", params); @@ -486,7 +487,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new UncheckOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); sendMessage("uncheck", params); } @@ -501,10 +502,10 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new WaitForFunctionOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("expression", pageFunction); params.addProperty("isFunction", isFunctionBody(pageFunction)); - params.add("arg", new Gson().toJsonTree(serializeArgument(arg))); + params.add("arg", gson().toJsonTree(serializeArgument(arg))); Waitable handle = sendMessageAsync("waitForFunction", params).apply(json -> { JsonObject element = json.getAsJsonObject().getAsJsonObject("handle"); return connection.getExistingObject(element.get("guid").getAsString()); @@ -657,7 +658,7 @@ public class FrameImpl extends ChannelOwner implements Frame { if (options == null) { options = new WaitForSelectorOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); if (options.state != null) { params.remove("state"); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/JSHandleImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/JSHandleImpl.java index c473f8a0..b13a60a0 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/JSHandleImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/JSHandleImpl.java @@ -25,8 +25,7 @@ import com.microsoft.playwright.JSHandle; import java.util.HashMap; import java.util.Map; -import static com.microsoft.playwright.impl.Serialization.deserialize; -import static com.microsoft.playwright.impl.Serialization.serializeArgument; +import static com.microsoft.playwright.impl.Serialization.*; import static com.microsoft.playwright.impl.Utils.isFunctionBody; public class JSHandleImpl extends ChannelOwner implements JSHandle { @@ -45,9 +44,9 @@ public class JSHandleImpl extends ChannelOwner implements JSHandle { params.addProperty("expression", pageFunction); params.addProperty("world", "main"); params.addProperty("isFunction", isFunctionBody(pageFunction)); - params.add("arg", new Gson().toJsonTree(serializeArgument(arg))); + params.add("arg", gson().toJsonTree(serializeArgument(arg))); JsonElement json = sendMessage("evaluateExpression", params); - SerializedValue value = new Gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); + SerializedValue value = gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); return deserialize(value); } @@ -57,7 +56,7 @@ public class JSHandleImpl extends ChannelOwner implements JSHandle { params.addProperty("expression", pageFunction); params.addProperty("world", "main"); params.addProperty("isFunction", isFunctionBody(pageFunction)); - params.add("arg", new Gson().toJsonTree(serializeArgument(arg))); + params.add("arg", gson().toJsonTree(serializeArgument(arg))); JsonElement json = sendMessage("evaluateExpressionHandle", params); return connection.getExistingObject(json.getAsJsonObject().getAsJsonObject("handle").get("guid").getAsString()); } @@ -85,7 +84,7 @@ public class JSHandleImpl extends ChannelOwner implements JSHandle { @Override public Object jsonValue() { JsonObject json = sendMessage("jsonValue").getAsJsonObject(); - SerializedValue value = new Gson().fromJson(json.get("value"), SerializedValue.class); + SerializedValue value = gson().fromJson(json.get("value"), SerializedValue.class); return deserialize(value); } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/MouseImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/MouseImpl.java index ef74cd8a..367a519c 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/MouseImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/MouseImpl.java @@ -20,6 +20,7 @@ import com.google.gson.Gson; import com.google.gson.JsonObject; import com.microsoft.playwright.Mouse; +import static com.microsoft.playwright.impl.Serialization.gson; import static com.microsoft.playwright.impl.Serialization.toProtocol; import static com.microsoft.playwright.impl.Utils.convertViaJson; @@ -35,7 +36,7 @@ class MouseImpl implements Mouse { if (options == null) { options = new ClickOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("x", x); params.addProperty("y", y); if (options.button != null) { @@ -62,7 +63,7 @@ class MouseImpl implements Mouse { if (options == null) { options = new DownOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); page.sendMessage("mouseDown", params); } @@ -71,7 +72,7 @@ class MouseImpl implements Mouse { if (options == null) { options = new MoveOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("x", x); params.addProperty("y", y); page.sendMessage("mouseMove", params); @@ -82,7 +83,7 @@ class MouseImpl implements Mouse { if (options == null) { options = new UpOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); page.sendMessage("mouseUp", params); } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java index 02e0a45f..9637f9d8 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java @@ -23,11 +23,13 @@ import com.microsoft.playwright.*; import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.*; import java.util.function.BiConsumer; import java.util.function.Predicate; import java.util.regex.Pattern; +import static com.microsoft.playwright.impl.Serialization.gson; import static com.microsoft.playwright.impl.Utils.convertViaJson; @@ -156,7 +158,7 @@ public class PageImpl extends ChannelOwner implements Page { route.continue_(); } } else if ("pageError".equals(event)) { - SerializedError error = new Gson().fromJson(params.getAsJsonObject("error"), SerializedError.class); + SerializedError error = gson().fromJson(params.getAsJsonObject("error"), SerializedError.class); listeners.notify(EventType.PAGEERROR, new ErrorImpl(error)); } else if ("crash".equals(event)) { listeners.notify(EventType.CRASH, null); @@ -203,7 +205,7 @@ public class PageImpl extends ChannelOwner implements Page { @Override public void close(CloseOptions options) { - JsonObject params = options == null ? new JsonObject() : new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = options == null ? new JsonObject() : gson().toJsonTree(options).getAsJsonObject(); sendMessage("close", params); if (ownedContext != null) { ownedContext.close(); @@ -416,7 +418,7 @@ public class PageImpl extends ChannelOwner implements Page { if (options == null) { options = new GoBackOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.remove("waitUntil"); params.addProperty("waitUntil", FrameImpl.toProtocol(options.waitUntil)); JsonObject json = sendMessage("goBack", params).getAsJsonObject(); @@ -431,7 +433,7 @@ public class PageImpl extends ChannelOwner implements Page { if (options == null) { options = new GoForwardOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.remove("waitUntil"); params.addProperty("waitUntil", FrameImpl.toProtocol(options.waitUntil)); JsonObject json = sendMessage("goForward", params).getAsJsonObject(); @@ -498,7 +500,7 @@ public class PageImpl extends ChannelOwner implements Page { if (options == null) { options = new PdfOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.remove("path"); JsonObject json = sendMessage("pdf", params).getAsJsonObject(); byte[] buffer = Base64.getDecoder().decode(json.get("pdf").getAsString()); @@ -518,7 +520,7 @@ public class PageImpl extends ChannelOwner implements Page { if (options == null) { options = new ReloadOptions(); } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.remove("waitUntil"); params.addProperty("waitUntil", FrameImpl.toProtocol(options.waitUntil)); JsonObject json = sendMessage("reload", params).getAsJsonObject(); @@ -564,16 +566,17 @@ public class PageImpl extends ChannelOwner implements Page { if (options.type == null) { options.type = ScreenshotOptions.Type.PNG; if (options.path != null) { - int extStart = options.path.getName().lastIndexOf('.'); + String fileName = options.path.getFileName().toString(); + int extStart = fileName.lastIndexOf('.'); if (extStart != -1) { - String extension = options.path.getName().substring(extStart).toLowerCase(); + String extension = fileName.substring(extStart).toLowerCase(); if (".jpeg".equals(extension) || ".jpg".equals(extension)) { options.type = ScreenshotOptions.Type.JPEG; } } } } - JsonObject params = new Gson().toJsonTree(options).getAsJsonObject(); + JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.remove("type"); params.addProperty("type", toProtocol(options.type)); params.remove("path"); @@ -632,7 +635,7 @@ public class PageImpl extends ChannelOwner implements Page { } @Override - public void setInputFiles(String selector, File[] files, SetInputFilesOptions options) { + public void setInputFiles(String selector, Path[] files, SetInputFilesOptions options) { mainFrame.setInputFiles(selector, files, convertViaJson(options, Frame.SetInputFilesOptions.class)); } @@ -645,7 +648,7 @@ public class PageImpl extends ChannelOwner implements Page { public void setViewportSize(int width, int height) { viewport = new Viewport(width, height); JsonObject params = new JsonObject(); - params.add("viewportSize", new Gson().toJsonTree(viewport)); + params.add("viewportSize", gson().toJsonTree(viewport)); sendMessage("setViewportSize", params); } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/PlaywrightImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/PlaywrightImpl.java index c784b4de..b312b75d 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/PlaywrightImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/PlaywrightImpl.java @@ -59,7 +59,7 @@ public class PlaywrightImpl extends ChannelOwner implements Playwright { firefox = parent.connection.getExistingObject(initializer.getAsJsonObject("firefox").get("guid").getAsString()); webkit = parent.connection.getExistingObject(initializer.getAsJsonObject("webkit").get("guid").getAsString()); - Gson gson = new Gson(); + Gson gson = Serialization.gson(); for (JsonElement item : initializer.getAsJsonArray("deviceDescriptors")) { JsonObject o = item.getAsJsonObject(); String name = o.get("name").getAsString(); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java b/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java index 794465cf..72356e00 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java @@ -16,21 +16,32 @@ package com.microsoft.playwright.impl; -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import com.google.gson.*; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; import com.microsoft.playwright.*; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; -import java.lang.reflect.Array; +import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.util.*; -import java.util.stream.Collectors; -import java.util.stream.StreamSupport; class Serialization { + private static Gson gson; + + static Gson gson() { + if (gson == null) { + gson = new GsonBuilder() + .registerTypeAdapter(BrowserContext.SameSite.class, new SameSiteAdapter().nullSafe()) + .registerTypeAdapter(BrowserType.LaunchPersistentContextOptions.ColorScheme.class, new ColorSchemeAdapter().nullSafe()) + .registerTypeAdapter(Path.class, new PathSerializer()).create(); + } + return gson; + } + static SerializedError serializeError(Throwable e) { SerializedError result = new SerializedError(); result.error = new SerializedError.Error(); @@ -40,7 +51,7 @@ class Serialization { ByteArrayOutputStream out = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(out)); result.error.stack = new String(out.toByteArray(), StandardCharsets.UTF_8); - return result; + return result; } private static SerializedValue serializeValue(Object value, List handles, int depth) { @@ -56,7 +67,7 @@ class Serialization { if (value == null) { result.v = "undefined"; } else if (value instanceof Double) { - double d = ((Double) value).doubleValue(); + double d = ((Double) value); if (d == Double.POSITIVE_INFINITY) { result.v = "Infinity"; } else if (d == Double.NEGATIVE_INFINITY) { @@ -160,15 +171,19 @@ class Serialization { } return (T) map; } - throw new PlaywrightException("Unexpected result: " + new Gson().toJson(value)); + 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); + case LEFT: + return "left"; + case RIGHT: + return "right"; + case MIDDLE: + return "middle"; + default: + throw new PlaywrightException("Unexpected value: " + button); } } @@ -229,4 +244,71 @@ class Serialization { } return result; } + + private static class PathSerializer implements JsonSerializer { + @Override + public JsonElement serialize(Path src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(src.toString()); + } + } + + private static class SameSiteAdapter extends TypeAdapter { + @Override + public void write(JsonWriter out, BrowserContext.SameSite value) throws IOException { + String stringValue; + switch (value) { + case STRICT: + stringValue = "Strict"; + break; + case LAX: + stringValue = "Lax"; + break; + case NONE: + stringValue = "None"; + break; + default: + throw new PlaywrightException("Unexpected value: " + value); + } + out.value(stringValue); + } + + @Override + public BrowserContext.SameSite read(JsonReader in) throws IOException { + String value = in.nextString(); + return BrowserContext.SameSite.valueOf(value.toUpperCase()); + } + } + + private static class ColorSchemeAdapter extends TypeAdapter { + @Override + public void write(JsonWriter out, BrowserType.LaunchPersistentContextOptions.ColorScheme value) throws IOException { + String stringValue; + switch (value) { + case DARK: + stringValue = "dark"; + break; + case LIGHT: + stringValue = "light"; + break; + case NO_PREFERENCE: + stringValue = "no-preference"; + break; + default: + throw new PlaywrightException("Unexpected value: " + value); + } + out.value(stringValue); + } + + @Override + public BrowserType.LaunchPersistentContextOptions.ColorScheme read(JsonReader in) throws IOException { + String value = in.nextString(); + switch (value) { + case "dark": return BrowserType.LaunchPersistentContextOptions.ColorScheme.DARK; + case "light": return BrowserType.LaunchPersistentContextOptions.ColorScheme.LIGHT; + case "no-preference": return BrowserType.LaunchPersistentContextOptions.ColorScheme.NO_PREFERENCE; + default: throw new PlaywrightException("Unexpected value: " + value); + } + } + } } + diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/Utils.java b/playwright/src/main/java/com/microsoft/playwright/impl/Utils.java index 7b104453..3ea434e4 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/Utils.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Utils.java @@ -32,8 +32,9 @@ import java.util.*; class Utils { // TODO: generate converter. static T convertViaJson(F f, Class t) { - String json = new Gson().toJson(f); - return new Gson().fromJson(json, t); + Gson gson = new Gson(); + String json = gson.toJson(f); + return gson.fromJson(json, t); } static boolean isFunctionBody(String expression) { @@ -113,30 +114,32 @@ class Utils { return mimeType; } - static FileChooser.FilePayload[] toFilePayloads(File[] files) { + static FileChooser.FilePayload[] toFilePayloads(Path[] files) { List payloads = new ArrayList<>(); - for (File file : files) { + for (Path file : files) { byte[] buffer; try { - buffer = Files.readAllBytes(file.toPath()); + buffer = Files.readAllBytes(file); } catch (IOException e) { throw new PlaywrightException("Failed to read from file", e); } - payloads.add(new FileChooser.FilePayload(file.getName(), mimeType(file.toPath()), buffer)); + payloads.add(new FileChooser.FilePayload(file.getFileName().toString(), mimeType(file), buffer)); } return payloads.toArray(new FileChooser.FilePayload[0]); } - static void writeToFile(byte[] buffer, File path) { - File dir = path.getParentFile(); + static void writeToFile(byte[] buffer, Path path) { + Path dir = path.getParent(); if (dir != null) { - if (!dir.exists()) { - if (!dir.mkdirs()) { - throw new PlaywrightException("Failed to create parent directory: " + dir.getPath()); + if (!Files.exists(dir)) { + try { + Files.createDirectories(dir); + } catch (IOException e) { + throw new PlaywrightException("Failed to create parent directory: " + dir.toString(), e); } } } - try (DataOutputStream out = new DataOutputStream(new FileOutputStream(path));) { + try (DataOutputStream out = new DataOutputStream(new FileOutputStream(path.toFile()));) { out.write(buffer); } catch (IOException e) { throw new PlaywrightException("Failed to write to file", e); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/WorkerImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/WorkerImpl.java index ea5d8c4b..98fdc699 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/WorkerImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/WorkerImpl.java @@ -21,8 +21,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.microsoft.playwright.*; -import static com.microsoft.playwright.impl.Serialization.deserialize; -import static com.microsoft.playwright.impl.Serialization.serializeArgument; +import static com.microsoft.playwright.impl.Serialization.*; import static com.microsoft.playwright.impl.Utils.isFunctionBody; class WorkerImpl extends ChannelOwner implements Worker { @@ -48,9 +47,9 @@ class WorkerImpl extends ChannelOwner implements Worker { JsonObject params = new JsonObject(); params.addProperty("expression", pageFunction); params.addProperty("isFunction", isFunctionBody(pageFunction)); - params.add("arg", new Gson().toJsonTree(serializeArgument(arg))); + params.add("arg", gson().toJsonTree(serializeArgument(arg))); JsonElement json = sendMessage("evaluateExpression", params); - SerializedValue value = new Gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); + SerializedValue value = gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class); return deserialize(value); } @@ -59,7 +58,7 @@ class WorkerImpl extends ChannelOwner implements Worker { JsonObject params = new JsonObject(); params.addProperty("expression", pageFunction); params.addProperty("isFunction", isFunctionBody(pageFunction)); - params.add("arg", new Gson().toJsonTree(serializeArgument(arg))); + params.add("arg", gson().toJsonTree(serializeArgument(arg))); JsonElement json = sendMessage("evaluateExpressionHandle", params); return connection.getExistingObject(json.getAsJsonObject().getAsJsonObject("handle").get("guid").getAsString()); } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageSetInputFiles.java b/playwright/src/test/java/com/microsoft/playwright/TestPageSetInputFiles.java index 0bc82fc1..1cfbc11c 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageSetInputFiles.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageSetInputFiles.java @@ -19,6 +19,8 @@ package com.microsoft.playwright; import org.junit.jupiter.api.Test; import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; import java.time.Duration; import java.time.Instant; import java.util.ArrayList; @@ -30,7 +32,7 @@ import static org.junit.jupiter.api.Assertions.*; public class TestPageSetInputFiles extends TestBase { - static File FILE_TO_UPLOAD = new File("src/test/resources/file-to-upload.txt"); + static Path FILE_TO_UPLOAD = Paths.get("src/test/resources/file-to-upload.txt"); @Test void shouldUploadTheFile() { @@ -238,7 +240,7 @@ public class TestPageSetInputFiles extends TestBase { @Override public void handle(Event event) { FileChooser fileChooser = (FileChooser) event.data(); - fileChooser.setFiles(new File[0]); + fileChooser.setFiles(new Path[0]); page.removeListener(Page.EventType.FILECHOOSER, this); } }); @@ -257,7 +259,7 @@ public class TestPageSetInputFiles extends TestBase { page.click("input"); FileChooser fileChooser = (FileChooser) event.get().data(); try { - fileChooser.setFiles(new File[]{FILE_TO_UPLOAD, new File("src/test/resources/pptr.png")}); + fileChooser.setFiles(new Path[]{FILE_TO_UPLOAD, Paths.get("src/test/resources/pptr.png")}); fail("did not throw"); } catch (PlaywrightException e) { assertTrue(e.getMessage().contains("Non-multiple file input can only accept single file")); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPdf.java b/playwright/src/test/java/com/microsoft/playwright/TestPdf.java index 195a13f6..80d1f487 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPdf.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPdf.java @@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test; import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import static org.junit.jupiter.api.Assertions.*; @@ -28,12 +29,10 @@ public class TestPdf extends TestBase { @Test void shouldBeAbleToSaveFile() throws IOException { // TODO: test.skip(headful || browserName !== "chromium", "Printing to pdf is currently only supported in headless chromium."); - File path = File.createTempFile("output", ".pdf"); + Path path = File.createTempFile("output", ".pdf").toPath(); page.pdf(new Page.PdfOptions().withPath(path)); - long size = Files.size(path.toPath()); + long size = Files.size(path); assertTrue(size > 0); - Browser b = playwright.webkit().launch(); - System.out.println(b.version()); } @Test