From 98eeb5e78b843146168b7ea22bb23e3ea5a5b2d6 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Fri, 2 Oct 2020 15:36:57 -0700 Subject: [PATCH] chore: simplify credentials and viewport api --- .../playwright/tools/ApiGenerator.java | 46 +++++++++++-- .../com/microsoft/playwright/tools/Types.java | 28 +++++++- .../com/microsoft/playwright/Browser.java | 66 +++++-------------- .../microsoft/playwright/BrowserContext.java | 2 +- .../com/microsoft/playwright/BrowserType.java | 33 +++------- .../microsoft/playwright/ChromiumBrowser.java | 66 +++++-------------- .../playwright/ChromiumBrowserContext.java | 2 +- .../microsoft/playwright/FirefoxBrowser.java | 66 +++++-------------- .../java/com/microsoft/playwright/Page.java | 46 ++++++------- .../microsoft/playwright/WebKitBrowser.java | 66 +++++-------------- .../microsoft/playwright/example/Main.java | 2 +- .../playwright/impl/BrowserContextImpl.java | 2 +- .../microsoft/playwright/impl/FrameImpl.java | 1 - .../microsoft/playwright/impl/PageImpl.java | 9 ++- .../com/microsoft/playwright/TestClick.java | 10 +-- .../com/microsoft/playwright/TestPopup.java | 10 +-- 16 files changed, 178 insertions(+), 277 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 823706b3..0add4c37 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 @@ -128,9 +128,6 @@ class TypeRef extends Element { if (jsonName.replace("null|", "").contains("|")) { throw new RuntimeException("Missing mapping for type union: " + jsonPath + ": " + jsonName); } -// if (jsonPath.contains("geolocation")) { -// System.out.println(jsonPath + ": " + jsonName); -// } return convertBuiltinType(stripPromise(jsonName)); } @@ -299,6 +296,13 @@ class Method extends Element { tsToJavaMethodName.put("goto", "navigate"); } + private static Map customSignature = new HashMap<>(); + static { + customSignature.put("Page.setViewportSize", "void setViewportSize(int width, int height);"); + customSignature.put("BrowserContext.setHTTPCredentials", "void setHTTPCredentials(String username, String password);"); + customSignature.put("ChromiumBrowserContext.setHTTPCredentials", "void setHTTPCredentials(String username, String password);"); + } + Method(TypeDefinition parent, JsonObject jsonElement) { super(parent, jsonElement); returnType = new TypeRef(this, jsonElement.get("type")); @@ -322,6 +326,10 @@ class Method extends Element { } void writeTo(List output, String offset) { + if (customSignature.containsKey(jsonPath)) { + output.add(offset + customSignature.get(jsonPath)); + return; + } for (int i = params.size() - 1; i >= 0; i--) { Param p = params.get(i); if (!p.isOptional()) { @@ -411,10 +419,20 @@ class Field extends Element { } void writeBuilderMethod(List output, String offset, String parentClass) { - if (type.isNestedClass) { + if (name.equals("httpCredentials") && type.toJava().equals("HttpCredentials")) { + output.add(offset + "public " + parentClass + " with" + toTitle(name) + "(String username, String password) {"); + output.add(offset + " this." + name + " = new " + type.toJava() + "();"); + output.add(offset + " this." + name + ".username = username;"); + output.add(offset + " this." + name + ".password = password;"); + output.add(offset + " return this;"); + } else if (type.isNestedClass) { output.add(offset + "public " + type.toJava() + " set" + toTitle(name) + "() {"); output.add(offset + " this." + name + " = new " + type.toJava() + "();"); output.add(offset + " return this." + name + ";"); + } else if ("Page.Viewport".equals(type.toJava()) || "Viewport".equals(type.toJava())) { + output.add(offset + "public " + parentClass + " with" + toTitle(name) + "(int width, int height) {"); + output.add(offset + " this." + name + " = new " + type.toJava() + "(width, height);"); + output.add(offset + " return this;"); } else if ("Set".equals(type.toJava())) { output.add(offset + "public " + parentClass + " with" + toTitle(name) + "(Keyboard.Modifier... modifiers) {"); output.add(offset + " this." + name + " = new HashSet<>(Arrays.asList(modifiers));"); @@ -498,6 +516,26 @@ class Interface extends TypeDefinition { output.add(offset + "enum Modifier { ALT, CONTROL, META, SHIFT }"); break; } + case "Page": { + output.add(offset + "class Viewport {"); + output.add(offset + " private final int width;"); + output.add(offset + " private final int height;"); + output.add(""); + output.add(offset + " public Viewport(int width, int height) {"); + output.add(offset + " this.width = width;"); + output.add(offset + " this.height = height;"); + output.add(offset + " }"); + output.add(""); + output.add(offset + " public int width() {"); + output.add(offset + " return width;"); + output.add(offset + " }"); + output.add(""); + output.add(offset + " public int height() {"); + output.add(offset + " return height;"); + output.add(offset + " }"); + output.add(offset + "}"); + break; + } default: return; } output.add(""); 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 74a31943..512eb0db 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 @@ -108,6 +108,30 @@ class Types { add("ChromiumBrowserContext.route.handler", "function(Route, Request)", "BiConsumer"); add("ChromiumBrowserContext.unroute.handler", "function(Route, Request)", "BiConsumer"); + // Viewport size. + add("Browser.newContext.options.viewport", "null|Object", "Page.Viewport", new Empty()); + add("Browser.newPage.options.viewport", "null|Object", "Page.Viewport", new Empty()); + add("Page.setViewportSize.viewportSize", "Object", "Viewport", new Empty()); + add("Page.viewportSize", "null|Object", "Viewport", new Empty()); + add("BrowserType.launchPersistentContext.options.viewport", "null|Object", "Page.Viewport", new Empty()); + add("ChromiumBrowser.newContext.options.viewport", "null|Object", "Page.Viewport", new Empty()); + add("ChromiumBrowser.newPage.options.viewport", "null|Object", "Page.Viewport", new Empty()); + add("FirefoxBrowser.newContext.options.viewport", "null|Object", "Page.Viewport", new Empty()); + add("FirefoxBrowser.newPage.options.viewport", "null|Object", "Page.Viewport", new Empty()); + add("WebKitBrowser.newContext.options.viewport", "null|Object", "Page.Viewport", new Empty()); + add("WebKitBrowser.newPage.options.viewport", "null|Object", "Page.Viewport", new Empty()); + + // HTTP credentials. + add("Browser.newContext.options.httpCredentials", "Object", "HttpCredentials"); + add("Browser.newPage.options.httpCredentials", "Object", "HttpCredentials"); + add("BrowserType.launchPersistentContext.options.httpCredentials", "Object", "HttpCredentials"); + add("ChromiumBrowser.newContext.options.httpCredentials", "Object", "HttpCredentials"); + add("ChromiumBrowser.newPage.options.httpCredentials", "Object", "HttpCredentials"); + add("FirefoxBrowser.newContext.options.httpCredentials", "Object", "HttpCredentials"); + add("FirefoxBrowser.newPage.options.httpCredentials", "Object", "HttpCredentials"); + add("WebKitBrowser.newContext.options.httpCredentials", "Object", "HttpCredentials"); + add("WebKitBrowser.newPage.options.httpCredentials", "Object", "HttpCredentials"); + // EvaluationArgument add("Page.$eval.arg", "EvaluationArgument", "Object"); add("Page.$$eval.arg", "EvaluationArgument", "Object"); @@ -245,7 +269,6 @@ class Types { add("ChromiumBrowserContext.setGeolocation.geolocation", "null|Object", "Geolocation"); add("BrowserContext.setHTTPCredentials.httpCredentials", "null|Object", "HTTPCredentials"); add("ChromiumBrowserContext.setHTTPCredentials.httpCredentials", "null|Object", "HTTPCredentials"); - add("Page.setViewportSize.viewportSize", "Object", "ViewportSize"); // Single field options add("Keyboard.type.options", "Object", "int", new Empty()); @@ -288,6 +311,9 @@ class Types { } private void add(String jsonPath, String fromType, String toType) { + if (jsonPathToMapping.containsKey(jsonPath)) { + throw new RuntimeException("Duplicate entry: " + jsonPath); + } jsonPathToMapping.put(jsonPath, new Mapping(fromType, toType)); } diff --git a/lib/src/main/java/com/microsoft/playwright/Browser.java b/lib/src/main/java/com/microsoft/playwright/Browser.java index 7e41ca75..ad418343 100644 --- a/lib/src/main/java/com/microsoft/playwright/Browser.java +++ b/lib/src/main/java/com/microsoft/playwright/Browser.java @@ -22,25 +22,6 @@ import java.util.function.BiConsumer; public interface Browser { class NewContextOptions { public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE } - public class Viewport { - public int width; - public int height; - - Viewport() { - } - public NewContextOptions done() { - return NewContextOptions.this; - } - - public Viewport withWidth(int width) { - this.width = width; - return this; - } - public Viewport withHeight(int height) { - this.height = height; - return this; - } - } public class Geolocation { public double latitude; public double longitude; @@ -106,7 +87,7 @@ public interface Browser { public Boolean acceptDownloads; public Boolean ignoreHTTPSErrors; public Boolean bypassCSP; - public Viewport viewport; + public Page.Viewport viewport; public String userAgent; public Integer deviceScaleFactor; public Boolean isMobile; @@ -138,9 +119,9 @@ public interface Browser { this.bypassCSP = bypassCSP; return this; } - public Viewport setViewport() { - this.viewport = new Viewport(); - return this.viewport; + public NewContextOptions withViewport(int width, int height) { + this.viewport = new Page.Viewport(width, height); + return this; } public NewContextOptions withUserAgent(String userAgent) { this.userAgent = userAgent; @@ -186,9 +167,11 @@ public interface Browser { this.offline = offline; return this; } - public HttpCredentials setHttpCredentials() { + public NewContextOptions withHttpCredentials(String username, String password) { this.httpCredentials = new HttpCredentials(); - return this.httpCredentials; + this.httpCredentials.username = username; + this.httpCredentials.password = password; + return this; } public NewContextOptions withColorScheme(ColorScheme colorScheme) { this.colorScheme = colorScheme; @@ -217,25 +200,6 @@ public interface Browser { } class NewPageOptions { public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE } - public class Viewport { - public int width; - public int height; - - Viewport() { - } - public NewPageOptions done() { - return NewPageOptions.this; - } - - public Viewport withWidth(int width) { - this.width = width; - return this; - } - public Viewport withHeight(int height) { - this.height = height; - return this; - } - } public class Geolocation { public double latitude; public double longitude; @@ -301,7 +265,7 @@ public interface Browser { public Boolean acceptDownloads; public Boolean ignoreHTTPSErrors; public Boolean bypassCSP; - public Viewport viewport; + public Page.Viewport viewport; public String userAgent; public Integer deviceScaleFactor; public Boolean isMobile; @@ -333,9 +297,9 @@ public interface Browser { this.bypassCSP = bypassCSP; return this; } - public Viewport setViewport() { - this.viewport = new Viewport(); - return this.viewport; + public NewPageOptions withViewport(int width, int height) { + this.viewport = new Page.Viewport(width, height); + return this; } public NewPageOptions withUserAgent(String userAgent) { this.userAgent = userAgent; @@ -381,9 +345,11 @@ public interface Browser { this.offline = offline; return this; } - public HttpCredentials setHttpCredentials() { + public NewPageOptions withHttpCredentials(String username, String password) { this.httpCredentials = new HttpCredentials(); - return this.httpCredentials; + this.httpCredentials.username = username; + this.httpCredentials.password = password; + return this; } public NewPageOptions withColorScheme(ColorScheme colorScheme) { this.colorScheme = colorScheme; diff --git a/lib/src/main/java/com/microsoft/playwright/BrowserContext.java b/lib/src/main/java/com/microsoft/playwright/BrowserContext.java index 2c5e1b02..70005572 100644 --- a/lib/src/main/java/com/microsoft/playwright/BrowserContext.java +++ b/lib/src/main/java/com/microsoft/playwright/BrowserContext.java @@ -86,7 +86,7 @@ public interface BrowserContext { void setDefaultTimeout(int timeout); void setExtraHTTPHeaders(Map headers); void setGeolocation(Geolocation geolocation); - void setHTTPCredentials(HTTPCredentials httpCredentials); + void setHTTPCredentials(String username, String password); void setOffline(boolean offline); default void unroute(String url) { unroute(url, null); diff --git a/lib/src/main/java/com/microsoft/playwright/BrowserType.java b/lib/src/main/java/com/microsoft/playwright/BrowserType.java index 88fa0261..d30fdb66 100644 --- a/lib/src/main/java/com/microsoft/playwright/BrowserType.java +++ b/lib/src/main/java/com/microsoft/playwright/BrowserType.java @@ -191,25 +191,6 @@ public interface BrowserType { return this; } } - public class Viewport { - public int width; - public int height; - - Viewport() { - } - public LaunchPersistentContextOptions done() { - return LaunchPersistentContextOptions.this; - } - - public Viewport withWidth(int width) { - this.width = width; - return this; - } - public Viewport withHeight(int height) { - this.height = height; - return this; - } - } public class Geolocation { public double latitude; public double longitude; @@ -291,7 +272,7 @@ public interface BrowserType { public Integer slowMo; public Boolean ignoreHTTPSErrors; public Boolean bypassCSP; - public Viewport viewport; + public Page.Viewport viewport; public String userAgent; public Integer deviceScaleFactor; public Boolean isMobile; @@ -386,9 +367,9 @@ public interface BrowserType { this.bypassCSP = bypassCSP; return this; } - public Viewport setViewport() { - this.viewport = new Viewport(); - return this.viewport; + public LaunchPersistentContextOptions withViewport(int width, int height) { + this.viewport = new Page.Viewport(width, height); + return this; } public LaunchPersistentContextOptions withUserAgent(String userAgent) { this.userAgent = userAgent; @@ -434,9 +415,11 @@ public interface BrowserType { this.offline = offline; return this; } - public HttpCredentials setHttpCredentials() { + public LaunchPersistentContextOptions withHttpCredentials(String username, String password) { this.httpCredentials = new HttpCredentials(); - return this.httpCredentials; + this.httpCredentials.username = username; + this.httpCredentials.password = password; + return this; } public LaunchPersistentContextOptions withColorScheme(ColorScheme colorScheme) { this.colorScheme = colorScheme; diff --git a/lib/src/main/java/com/microsoft/playwright/ChromiumBrowser.java b/lib/src/main/java/com/microsoft/playwright/ChromiumBrowser.java index d73ae186..df98f443 100644 --- a/lib/src/main/java/com/microsoft/playwright/ChromiumBrowser.java +++ b/lib/src/main/java/com/microsoft/playwright/ChromiumBrowser.java @@ -40,25 +40,6 @@ public interface ChromiumBrowser { } class NewContextOptions { public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE } - public class Viewport { - public int width; - public int height; - - Viewport() { - } - public NewContextOptions done() { - return NewContextOptions.this; - } - - public Viewport withWidth(int width) { - this.width = width; - return this; - } - public Viewport withHeight(int height) { - this.height = height; - return this; - } - } public class Geolocation { public double latitude; public double longitude; @@ -124,7 +105,7 @@ public interface ChromiumBrowser { public Boolean acceptDownloads; public Boolean ignoreHTTPSErrors; public Boolean bypassCSP; - public Viewport viewport; + public Page.Viewport viewport; public String userAgent; public Integer deviceScaleFactor; public Boolean isMobile; @@ -156,9 +137,9 @@ public interface ChromiumBrowser { this.bypassCSP = bypassCSP; return this; } - public Viewport setViewport() { - this.viewport = new Viewport(); - return this.viewport; + public NewContextOptions withViewport(int width, int height) { + this.viewport = new Page.Viewport(width, height); + return this; } public NewContextOptions withUserAgent(String userAgent) { this.userAgent = userAgent; @@ -204,9 +185,11 @@ public interface ChromiumBrowser { this.offline = offline; return this; } - public HttpCredentials setHttpCredentials() { + public NewContextOptions withHttpCredentials(String username, String password) { this.httpCredentials = new HttpCredentials(); - return this.httpCredentials; + this.httpCredentials.username = username; + this.httpCredentials.password = password; + return this; } public NewContextOptions withColorScheme(ColorScheme colorScheme) { this.colorScheme = colorScheme; @@ -235,25 +218,6 @@ public interface ChromiumBrowser { } class NewPageOptions { public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE } - public class Viewport { - public int width; - public int height; - - Viewport() { - } - public NewPageOptions done() { - return NewPageOptions.this; - } - - public Viewport withWidth(int width) { - this.width = width; - return this; - } - public Viewport withHeight(int height) { - this.height = height; - return this; - } - } public class Geolocation { public double latitude; public double longitude; @@ -319,7 +283,7 @@ public interface ChromiumBrowser { public Boolean acceptDownloads; public Boolean ignoreHTTPSErrors; public Boolean bypassCSP; - public Viewport viewport; + public Page.Viewport viewport; public String userAgent; public Integer deviceScaleFactor; public Boolean isMobile; @@ -351,9 +315,9 @@ public interface ChromiumBrowser { this.bypassCSP = bypassCSP; return this; } - public Viewport setViewport() { - this.viewport = new Viewport(); - return this.viewport; + public NewPageOptions withViewport(int width, int height) { + this.viewport = new Page.Viewport(width, height); + return this; } public NewPageOptions withUserAgent(String userAgent) { this.userAgent = userAgent; @@ -399,9 +363,11 @@ public interface ChromiumBrowser { this.offline = offline; return this; } - public HttpCredentials setHttpCredentials() { + public NewPageOptions withHttpCredentials(String username, String password) { this.httpCredentials = new HttpCredentials(); - return this.httpCredentials; + this.httpCredentials.username = username; + this.httpCredentials.password = password; + return this; } public NewPageOptions withColorScheme(ColorScheme colorScheme) { this.colorScheme = colorScheme; diff --git a/lib/src/main/java/com/microsoft/playwright/ChromiumBrowserContext.java b/lib/src/main/java/com/microsoft/playwright/ChromiumBrowserContext.java index 33100954..ca1bec4e 100644 --- a/lib/src/main/java/com/microsoft/playwright/ChromiumBrowserContext.java +++ b/lib/src/main/java/com/microsoft/playwright/ChromiumBrowserContext.java @@ -88,7 +88,7 @@ public interface ChromiumBrowserContext { void setDefaultTimeout(int timeout); void setExtraHTTPHeaders(Map headers); void setGeolocation(Geolocation geolocation); - void setHTTPCredentials(HTTPCredentials httpCredentials); + void setHTTPCredentials(String username, String password); void setOffline(boolean offline); default void unroute(String url) { unroute(url, null); diff --git a/lib/src/main/java/com/microsoft/playwright/FirefoxBrowser.java b/lib/src/main/java/com/microsoft/playwright/FirefoxBrowser.java index 2892aa64..e7f51ad0 100644 --- a/lib/src/main/java/com/microsoft/playwright/FirefoxBrowser.java +++ b/lib/src/main/java/com/microsoft/playwright/FirefoxBrowser.java @@ -22,25 +22,6 @@ import java.util.function.BiConsumer; public interface FirefoxBrowser { class NewContextOptions { public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE } - public class Viewport { - public int width; - public int height; - - Viewport() { - } - public NewContextOptions done() { - return NewContextOptions.this; - } - - public Viewport withWidth(int width) { - this.width = width; - return this; - } - public Viewport withHeight(int height) { - this.height = height; - return this; - } - } public class Geolocation { public double latitude; public double longitude; @@ -106,7 +87,7 @@ public interface FirefoxBrowser { public Boolean acceptDownloads; public Boolean ignoreHTTPSErrors; public Boolean bypassCSP; - public Viewport viewport; + public Page.Viewport viewport; public String userAgent; public Integer deviceScaleFactor; public Boolean isMobile; @@ -138,9 +119,9 @@ public interface FirefoxBrowser { this.bypassCSP = bypassCSP; return this; } - public Viewport setViewport() { - this.viewport = new Viewport(); - return this.viewport; + public NewContextOptions withViewport(int width, int height) { + this.viewport = new Page.Viewport(width, height); + return this; } public NewContextOptions withUserAgent(String userAgent) { this.userAgent = userAgent; @@ -186,9 +167,11 @@ public interface FirefoxBrowser { this.offline = offline; return this; } - public HttpCredentials setHttpCredentials() { + public NewContextOptions withHttpCredentials(String username, String password) { this.httpCredentials = new HttpCredentials(); - return this.httpCredentials; + this.httpCredentials.username = username; + this.httpCredentials.password = password; + return this; } public NewContextOptions withColorScheme(ColorScheme colorScheme) { this.colorScheme = colorScheme; @@ -217,25 +200,6 @@ public interface FirefoxBrowser { } class NewPageOptions { public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE } - public class Viewport { - public int width; - public int height; - - Viewport() { - } - public NewPageOptions done() { - return NewPageOptions.this; - } - - public Viewport withWidth(int width) { - this.width = width; - return this; - } - public Viewport withHeight(int height) { - this.height = height; - return this; - } - } public class Geolocation { public double latitude; public double longitude; @@ -301,7 +265,7 @@ public interface FirefoxBrowser { public Boolean acceptDownloads; public Boolean ignoreHTTPSErrors; public Boolean bypassCSP; - public Viewport viewport; + public Page.Viewport viewport; public String userAgent; public Integer deviceScaleFactor; public Boolean isMobile; @@ -333,9 +297,9 @@ public interface FirefoxBrowser { this.bypassCSP = bypassCSP; return this; } - public Viewport setViewport() { - this.viewport = new Viewport(); - return this.viewport; + public NewPageOptions withViewport(int width, int height) { + this.viewport = new Page.Viewport(width, height); + return this; } public NewPageOptions withUserAgent(String userAgent) { this.userAgent = userAgent; @@ -381,9 +345,11 @@ public interface FirefoxBrowser { this.offline = offline; return this; } - public HttpCredentials setHttpCredentials() { + public NewPageOptions withHttpCredentials(String username, String password) { this.httpCredentials = new HttpCredentials(); - return this.httpCredentials; + this.httpCredentials.username = username; + this.httpCredentials.password = password; + return this; } public NewPageOptions withColorScheme(ColorScheme colorScheme) { this.colorScheme = colorScheme; diff --git a/lib/src/main/java/com/microsoft/playwright/Page.java b/lib/src/main/java/com/microsoft/playwright/Page.java index 7b69842a..229024b1 100644 --- a/lib/src/main/java/com/microsoft/playwright/Page.java +++ b/lib/src/main/java/com/microsoft/playwright/Page.java @@ -20,6 +20,24 @@ import java.util.*; import java.util.function.BiConsumer; public interface Page { + class Viewport { + private final int width; + private final int height; + + public Viewport(int width, int height) { + this.width = width; + this.height = height; + } + + public int width() { + return width; + } + + public int height() { + return height; + } + } + enum LoadState { DOMCONTENTLOADED, LOAD, NETWORKIDLE } class CloseOptions { public Boolean runBeforeUnload; @@ -601,19 +619,6 @@ public interface Page { return this; } } - class ViewportSize { - public int width; - public int height; - - public ViewportSize withWidth(int width) { - this.width = width; - return this; - } - public ViewportSize withHeight(int height) { - this.height = height; - return this; - } - } class TextContentOptions { public Integer timeout; @@ -658,17 +663,6 @@ public interface Page { return this; } } - class PageViewportSize { - private int width; - private int height; - - public int width() { - return this.width; - } - public int height() { - return this.height; - } - } class WaitForFunctionOptions { public double polling; public Integer timeout; @@ -872,7 +866,7 @@ public interface Page { setInputFiles(selector, files, null); } void setInputFiles(String selector, String files, SetInputFilesOptions options); - void setViewportSize(ViewportSize viewportSize); + void setViewportSize(int width, int height); default String textContent(String selector) { return textContent(selector, null); } @@ -891,7 +885,7 @@ public interface Page { } void unroute(String url, BiConsumer handler); String url(); - PageViewportSize viewportSize(); + Viewport viewportSize(); default Object waitForEvent(String event) { return waitForEvent(event, null); } diff --git a/lib/src/main/java/com/microsoft/playwright/WebKitBrowser.java b/lib/src/main/java/com/microsoft/playwright/WebKitBrowser.java index 959d44d3..6a2353a8 100644 --- a/lib/src/main/java/com/microsoft/playwright/WebKitBrowser.java +++ b/lib/src/main/java/com/microsoft/playwright/WebKitBrowser.java @@ -22,25 +22,6 @@ import java.util.function.BiConsumer; public interface WebKitBrowser { class NewContextOptions { public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE } - public class Viewport { - public int width; - public int height; - - Viewport() { - } - public NewContextOptions done() { - return NewContextOptions.this; - } - - public Viewport withWidth(int width) { - this.width = width; - return this; - } - public Viewport withHeight(int height) { - this.height = height; - return this; - } - } public class Geolocation { public double latitude; public double longitude; @@ -106,7 +87,7 @@ public interface WebKitBrowser { public Boolean acceptDownloads; public Boolean ignoreHTTPSErrors; public Boolean bypassCSP; - public Viewport viewport; + public Page.Viewport viewport; public String userAgent; public Integer deviceScaleFactor; public Boolean isMobile; @@ -138,9 +119,9 @@ public interface WebKitBrowser { this.bypassCSP = bypassCSP; return this; } - public Viewport setViewport() { - this.viewport = new Viewport(); - return this.viewport; + public NewContextOptions withViewport(int width, int height) { + this.viewport = new Page.Viewport(width, height); + return this; } public NewContextOptions withUserAgent(String userAgent) { this.userAgent = userAgent; @@ -186,9 +167,11 @@ public interface WebKitBrowser { this.offline = offline; return this; } - public HttpCredentials setHttpCredentials() { + public NewContextOptions withHttpCredentials(String username, String password) { this.httpCredentials = new HttpCredentials(); - return this.httpCredentials; + this.httpCredentials.username = username; + this.httpCredentials.password = password; + return this; } public NewContextOptions withColorScheme(ColorScheme colorScheme) { this.colorScheme = colorScheme; @@ -217,25 +200,6 @@ public interface WebKitBrowser { } class NewPageOptions { public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE } - public class Viewport { - public int width; - public int height; - - Viewport() { - } - public NewPageOptions done() { - return NewPageOptions.this; - } - - public Viewport withWidth(int width) { - this.width = width; - return this; - } - public Viewport withHeight(int height) { - this.height = height; - return this; - } - } public class Geolocation { public double latitude; public double longitude; @@ -301,7 +265,7 @@ public interface WebKitBrowser { public Boolean acceptDownloads; public Boolean ignoreHTTPSErrors; public Boolean bypassCSP; - public Viewport viewport; + public Page.Viewport viewport; public String userAgent; public Integer deviceScaleFactor; public Boolean isMobile; @@ -333,9 +297,9 @@ public interface WebKitBrowser { this.bypassCSP = bypassCSP; return this; } - public Viewport setViewport() { - this.viewport = new Viewport(); - return this.viewport; + public NewPageOptions withViewport(int width, int height) { + this.viewport = new Page.Viewport(width, height); + return this; } public NewPageOptions withUserAgent(String userAgent) { this.userAgent = userAgent; @@ -381,9 +345,11 @@ public interface WebKitBrowser { this.offline = offline; return this; } - public HttpCredentials setHttpCredentials() { + public NewPageOptions withHttpCredentials(String username, String password) { this.httpCredentials = new HttpCredentials(); - return this.httpCredentials; + this.httpCredentials.username = username; + this.httpCredentials.password = password; + return this; } public NewPageOptions withColorScheme(ColorScheme colorScheme) { this.colorScheme = colorScheme; diff --git a/lib/src/main/java/com/microsoft/playwright/example/Main.java b/lib/src/main/java/com/microsoft/playwright/example/Main.java index c1a11844..69658c27 100644 --- a/lib/src/main/java/com/microsoft/playwright/example/Main.java +++ b/lib/src/main/java/com/microsoft/playwright/example/Main.java @@ -39,7 +39,7 @@ public class Main { System.out.println("browser = " + browser); Browser.NewContextOptions contextOptions = new Browser.NewContextOptions(); - contextOptions.setViewport().withWidth(800).withHeight(600); + contextOptions.withViewport(800, 600); BrowserContext context = browser.newContext(contextOptions); Page page = context.newPage(); // page.navigate("http://example.com", null); diff --git a/lib/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java b/lib/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java index 816e5c8b..b199823a 100644 --- a/lib/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java +++ b/lib/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java @@ -142,7 +142,7 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext { } @Override - public void setHTTPCredentials(HTTPCredentials httpCredentials) { + public void setHTTPCredentials(String username, String password) { } diff --git a/lib/src/main/java/com/microsoft/playwright/impl/FrameImpl.java b/lib/src/main/java/com/microsoft/playwright/impl/FrameImpl.java index 17f06959..fbeb6bcd 100644 --- a/lib/src/main/java/com/microsoft/playwright/impl/FrameImpl.java +++ b/lib/src/main/java/com/microsoft/playwright/impl/FrameImpl.java @@ -416,7 +416,6 @@ public class FrameImpl extends ChannelOwner implements Frame { @Override public String title() { JsonElement json = sendMessage("title", new JsonObject()); - System.out.println(new Gson().toJson(json)); return json.getAsJsonObject().get("value").getAsString(); } diff --git a/lib/src/main/java/com/microsoft/playwright/impl/PageImpl.java b/lib/src/main/java/com/microsoft/playwright/impl/PageImpl.java index edccd799..f2e83af3 100644 --- a/lib/src/main/java/com/microsoft/playwright/impl/PageImpl.java +++ b/lib/src/main/java/com/microsoft/playwright/impl/PageImpl.java @@ -347,9 +347,12 @@ public class PageImpl extends ChannelOwner implements Page { } @Override - public void setViewportSize(ViewportSize viewportSize) { + public void setViewportSize(int width, int height) { + JsonObject size = new JsonObject(); + size.addProperty("width", width); + size.addProperty("height", height); JsonObject params = new JsonObject(); - params.add("viewportSize", new Gson().toJsonTree(viewportSize).getAsJsonObject()); + params.add("viewportSize", size); sendMessage("setViewportSize", params); } @@ -384,7 +387,7 @@ public class PageImpl extends ChannelOwner implements Page { } @Override - public PageViewportSize viewportSize() { + public Viewport viewportSize() { return null; } diff --git a/lib/src/test/java/com/microsoft/playwright/TestClick.java b/lib/src/test/java/com/microsoft/playwright/TestClick.java index e54432f9..d011086f 100644 --- a/lib/src/test/java/com/microsoft/playwright/TestClick.java +++ b/lib/src/test/java/com/microsoft/playwright/TestClick.java @@ -269,10 +269,7 @@ public class TestClick { // @see https://github.com/GoogleChrome/puppeteer/issues/161 DeviceDescriptor descriptor = playwright.devices().get("iPhone 6"); BrowserContext context = browser.newContext(new Browser.NewContextOptions() - .setViewport() - .withWidth(descriptor.viewport().width()) - .withHeight(descriptor.viewport().height()) - .done() + .withViewport(descriptor.viewport().width(), descriptor.viewport().height()) .withHasTouch(descriptor.hasTouch())); Page page = context.newPage(); page.mouse().down(); @@ -413,10 +410,7 @@ public class TestClick { void should_click_the_button_with_offset_with_page_scale() { // TODO: test.skip(browserName === "firefox"); BrowserContext context = browser.newContext(new Browser.NewContextOptions() - .setViewport() - .withWidth(400) - .withHeight(400) - .done() + .withViewport(400, 400) .withIsMobile(true)); Page page = context.newPage(); page.navigate(server.PREFIX + "/input/button.html"); diff --git a/lib/src/test/java/com/microsoft/playwright/TestPopup.java b/lib/src/test/java/com/microsoft/playwright/TestPopup.java index 09431b43..bc53b967 100644 --- a/lib/src/test/java/com/microsoft/playwright/TestPopup.java +++ b/lib/src/test/java/com/microsoft/playwright/TestPopup.java @@ -138,7 +138,7 @@ public class TestPopup { void should_inherit_http_credentials_from_browser_context() { server.setAuth("/title.html", "user", "pass"); BrowserContext context = browser.newContext(new Browser.NewContextOptions() - .setHttpCredentials().withUsername("user").withPassword("pass").done()); + .withHttpCredentials("user", "pass")); Page page = context.newPage(); page.navigate(server.EMPTY_PAGE); Deferred popup = page.waitForPopup(); @@ -151,7 +151,7 @@ public class TestPopup { @Test void should_inherit_touch_support_from_browser_context() { BrowserContext context = browser.newContext(new Browser.NewContextOptions() - .setViewport().withWidth(400).withHeight(500).done() + .withViewport(400, 500) .withHasTouch(true)); Page page = context.newPage(); page.navigate(server.EMPTY_PAGE); @@ -166,7 +166,7 @@ public class TestPopup { @Test void should_inherit_viewport_size_from_browser_context() { BrowserContext context = browser.newContext(new Browser.NewContextOptions() - .setViewport().withWidth(400).withHeight(500).done()); + .withViewport(400, 500)); Page page = context.newPage(); page.navigate(server.EMPTY_PAGE); Object size = page.evaluate("() => {\n" + @@ -180,7 +180,7 @@ public class TestPopup { @Test void should_use_viewport_size_from_window_features() { BrowserContext context = browser.newContext(new Browser.NewContextOptions() - .setViewport().withWidth(700).withHeight(700).done()); + .withViewport(700, 700)); Page page = context.newPage(); page.navigate(server.EMPTY_PAGE); Deferred popupEvent = page.waitForPopup(); @@ -189,7 +189,7 @@ public class TestPopup { " return { width: win.innerWidth, height: win.innerHeight };\n" + "}"); Page popup = popupEvent.get(); - popup.setViewportSize(new Page.ViewportSize().withWidth(500).withHeight(400)); + popup.setViewportSize(500, 400); popup.waitForLoadState(); Object resized = popup.evaluate("() => ({ width: window.innerWidth, height: window.innerHeight })"); context.close();