feat(api): implement Keyboard, Mouse, DeviceDescriptors
This commit is contained in:
@@ -346,7 +346,7 @@ class Method extends Element {
|
||||
if (argList.length() > 0) {
|
||||
argList.append(", ");
|
||||
}
|
||||
argList.append("null");
|
||||
argList.append("int".equals(params.get(paramCount).type.toJava()) ? "0" : "null");
|
||||
String returns = returnType.toJava().equals("void") ? "" : "return ";
|
||||
output.add(offset + "default " + returnType.toJava() + " " + name + "(" + paramList + ") {");
|
||||
output.add(offset + " " + returns + name + "(" + argList + ");");
|
||||
@@ -357,6 +357,12 @@ class Method extends Element {
|
||||
class Param extends Element {
|
||||
final TypeRef type;
|
||||
|
||||
private static Map<String, String> customName = new HashMap<>();
|
||||
static {
|
||||
customName.put("Keyboard.type.options", "delay");
|
||||
customName.put("Keyboard.press.options", "delay");
|
||||
}
|
||||
|
||||
Param(Method method, JsonObject jsonElement) {
|
||||
super(method, jsonElement);
|
||||
type = new TypeRef(this, jsonElement.get("type").getAsJsonObject());
|
||||
@@ -366,8 +372,16 @@ class Param extends Element {
|
||||
return !jsonElement.getAsJsonObject().get("required").getAsBoolean();
|
||||
}
|
||||
|
||||
private String name() {
|
||||
String name = customName.get(jsonPath);
|
||||
if (name != null) {
|
||||
return name;
|
||||
}
|
||||
return jsonName;
|
||||
}
|
||||
|
||||
String toJava() {
|
||||
return type.toJava() + " " + jsonName;
|
||||
return type.toJava() + " " + name();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -427,6 +441,10 @@ class Interface extends TypeDefinition {
|
||||
if ("method".equals(kind)) {
|
||||
methods.add(new Method(this, json));
|
||||
}
|
||||
// All properties are converted to methods in Java.
|
||||
if ("property".equals(kind)) {
|
||||
methods.add(new Method(this, json));
|
||||
}
|
||||
if ("event".equals(kind)) {
|
||||
events.add(new Event(this, json));
|
||||
}
|
||||
@@ -439,16 +457,33 @@ class Interface extends TypeDefinition {
|
||||
output.add("import java.util.function.BiConsumer;");
|
||||
output.add("");
|
||||
output.add("public interface " + jsonName + " {");
|
||||
super.writeTo(output, offset + " ");
|
||||
offset = " ";
|
||||
writeSharedTypes(output, offset);
|
||||
super.writeTo(output, offset);
|
||||
for (Event e : events) {
|
||||
e.writeTo(output, offset + " ");
|
||||
e.writeTo(output, offset);
|
||||
}
|
||||
for (Method m : methods) {
|
||||
m.writeTo(output, offset + " ");
|
||||
m.writeTo(output, offset);
|
||||
}
|
||||
output.add("}");
|
||||
output.add("\n");
|
||||
}
|
||||
|
||||
void writeSharedTypes(List<String> output, String offset) {
|
||||
switch (jsonName) {
|
||||
case "Mouse": {
|
||||
output.add(offset + "enum Button { LEFT, MIDDLE, RIGHT }");
|
||||
break;
|
||||
}
|
||||
case "Keyboard": {
|
||||
output.add(offset + "enum Modifier { ALT, CONTROL, META, SHIFT }");
|
||||
break;
|
||||
}
|
||||
default: return;
|
||||
}
|
||||
output.add("");
|
||||
}
|
||||
}
|
||||
|
||||
class NestedClass extends TypeDefinition {
|
||||
@@ -528,7 +563,7 @@ class Enum extends TypeDefinition {
|
||||
|
||||
void writeTo(List<String> output, String offset) {
|
||||
String access = parent.typeScope() instanceof NestedClass ? "public " : "";
|
||||
output.add(offset + access + "enum " + name + " { " + String.join(", ", enumValues) + "}");
|
||||
output.add(offset + access + "enum " + name + " { " + String.join(", ", enumValues) + " }");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,43 +55,43 @@ class Types {
|
||||
// Option enums
|
||||
add("Browser.newContext.options.colorScheme", "\"dark\"|\"light\"|\"no-preference\"", "ColorScheme");
|
||||
add("Browser.newPage.options.colorScheme", "\"dark\"|\"light\"|\"no-preference\"", "ColorScheme");
|
||||
add("Page.click.options.button", "\"left\"|\"middle\"|\"right\"", "Button");
|
||||
add("Page.click.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Modifier>", new ModifierEnum());
|
||||
add("Page.dblclick.options.button", "\"left\"|\"middle\"|\"right\"", "Button");
|
||||
add("Page.dblclick.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Modifier>", new ModifierEnum());
|
||||
add("Page.click.options.button", "\"left\"|\"middle\"|\"right\"", "Mouse.Button", new Empty());
|
||||
add("Page.click.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Keyboard.Modifier>", new Empty());
|
||||
add("Page.dblclick.options.button", "\"left\"|\"middle\"|\"right\"", "Mouse.Button", new Empty());
|
||||
add("Page.dblclick.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Keyboard.Modifier>", new Empty());
|
||||
add("Page.emulateMedia.options.media", "null|\"print\"|\"screen\"", "Media");
|
||||
add("Page.emulateMedia.options.colorScheme", "null|\"dark\"|\"light\"|\"no-preference\"", "ColorScheme");
|
||||
add("Page.goBack.options.waitUntil", "\"domcontentloaded\"|\"load\"|\"networkidle\"", "WaitUntil");
|
||||
add("Page.goForward.options.waitUntil", "\"domcontentloaded\"|\"load\"|\"networkidle\"", "WaitUntil");
|
||||
add("Page.goto.options.waitUntil", "\"domcontentloaded\"|\"load\"|\"networkidle\"", "WaitUntil");
|
||||
add("Page.hover.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Modifier>", new ModifierEnum());
|
||||
add("Page.hover.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Keyboard.Modifier>", new Empty());
|
||||
add("Page.reload.options.waitUntil", "\"domcontentloaded\"|\"load\"|\"networkidle\"", "WaitUntil");
|
||||
add("Page.screenshot.options.type", "\"jpeg\"|\"png\"", "Type");
|
||||
add("Page.setContent.options.waitUntil", "\"domcontentloaded\"|\"load\"|\"networkidle\"", "WaitUntil");
|
||||
add("Page.waitForFunction.options.polling", "number|\"raf\"", "double", new PollingOption());
|
||||
add("Page.waitForNavigation.options.waitUntil", "\"domcontentloaded\"|\"load\"|\"networkidle\"", "WaitUntil");
|
||||
add("Page.waitForSelector.options.state", "\"attached\"|\"detached\"|\"hidden\"|\"visible\"", "State");
|
||||
add("Frame.click.options.button", "\"left\"|\"middle\"|\"right\"", "Button");
|
||||
add("Frame.click.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Modifier>", new ModifierEnum());
|
||||
add("Frame.dblclick.options.button", "\"left\"|\"middle\"|\"right\"", "Button");
|
||||
add("Frame.dblclick.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Modifier>", new ModifierEnum());
|
||||
add("Frame.click.options.button", "\"left\"|\"middle\"|\"right\"", "Mouse.Button", new Empty());
|
||||
add("Frame.click.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Keyboard.Modifier>", new Empty());
|
||||
add("Frame.dblclick.options.button", "\"left\"|\"middle\"|\"right\"", "Mouse.Button", new Empty());
|
||||
add("Frame.dblclick.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Keyboard.Modifier>", new Empty());
|
||||
add("Frame.goto.options.waitUntil", "\"domcontentloaded\"|\"load\"|\"networkidle\"", "WaitUntil");
|
||||
add("Frame.hover.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Modifier>", new ModifierEnum());
|
||||
add("Frame.hover.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Keyboard.Modifier>", new Empty());
|
||||
add("Frame.setContent.options.waitUntil", "\"domcontentloaded\"|\"load\"|\"networkidle\"", "WaitUntil");
|
||||
add("Frame.waitForFunction.options.polling", "number|\"raf\"", "double", new PollingOption());
|
||||
add("Frame.waitForNavigation.options.waitUntil", "\"domcontentloaded\"|\"load\"|\"networkidle\"", "WaitUntil");
|
||||
add("Frame.waitForSelector.options.state", "\"attached\"|\"detached\"|\"hidden\"|\"visible\"", "State");
|
||||
add("ElementHandle.click.options.button", "\"left\"|\"middle\"|\"right\"", "Button");
|
||||
add("ElementHandle.click.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Modifier>", new ModifierEnum());
|
||||
add("ElementHandle.dblclick.options.button", "\"left\"|\"middle\"|\"right\"", "Button");
|
||||
add("ElementHandle.dblclick.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Modifier>", new ModifierEnum());
|
||||
add("ElementHandle.hover.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Modifier>", new ModifierEnum());
|
||||
add("ElementHandle.click.options.button", "\"left\"|\"middle\"|\"right\"", "Mouse.Button", new Empty());
|
||||
add("ElementHandle.click.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Keyboard.Modifier>", new Empty());
|
||||
add("ElementHandle.dblclick.options.button", "\"left\"|\"middle\"|\"right\"", "Mouse.Button", new Empty());
|
||||
add("ElementHandle.dblclick.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Keyboard.Modifier>", new Empty());
|
||||
add("ElementHandle.hover.options.modifiers", "Array<\"Alt\"|\"Control\"|\"Meta\"|\"Shift\">", "Set<Keyboard.Modifier>", new Empty());
|
||||
add("ElementHandle.screenshot.options.type", "\"jpeg\"|\"png\"", "Type");
|
||||
add("ElementHandle.waitForSelector.options.state", "\"attached\"|\"detached\"|\"hidden\"|\"visible\"", "State");
|
||||
add("Mouse.click.options.button", "\"left\"|\"middle\"|\"right\"", "Button");
|
||||
add("Mouse.dblclick.options.button", "\"left\"|\"middle\"|\"right\"", "Button");
|
||||
add("Mouse.down.options.button", "\"left\"|\"middle\"|\"right\"", "Button");
|
||||
add("Mouse.up.options.button", "\"left\"|\"middle\"|\"right\"", "Button");
|
||||
add("Mouse.click.options.button", "\"left\"|\"middle\"|\"right\"", "Button", new Empty());
|
||||
add("Mouse.dblclick.options.button", "\"left\"|\"middle\"|\"right\"", "Button", new Empty());
|
||||
add("Mouse.down.options.button", "\"left\"|\"middle\"|\"right\"", "Button", new Empty());
|
||||
add("Mouse.up.options.button", "\"left\"|\"middle\"|\"right\"", "Button", new Empty());
|
||||
add("BrowserType.launchPersistentContext.options.colorScheme", "\"dark\"|\"light\"|\"no-preference\"", "ColorScheme");
|
||||
add("ChromiumBrowser.newContext.options.colorScheme", "\"dark\"|\"light\"|\"no-preference\"", "ColorScheme");
|
||||
add("ChromiumBrowser.newPage.options.colorScheme", "\"dark\"|\"light\"|\"no-preference\"", "ColorScheme");
|
||||
@@ -247,6 +247,10 @@ class Types {
|
||||
add("ChromiumBrowserContext.setHTTPCredentials.httpCredentials", "null|Object", "HTTPCredentials");
|
||||
add("Page.setViewportSize.viewportSize", "Object", "ViewportSize");
|
||||
|
||||
// Single field options
|
||||
add("Keyboard.type.options", "Object", "int", new Empty());
|
||||
add("Keyboard.press.options", "Object", "int", new Empty());
|
||||
|
||||
// node.js types
|
||||
add("BrowserServer.process", "ChildProcess", "Object");
|
||||
|
||||
@@ -291,13 +295,6 @@ class Types {
|
||||
jsonPathToMapping.put(jsonPath, new Mapping(fromType, toType, factory));
|
||||
}
|
||||
|
||||
private static class ModifierEnum implements CustomMapping {
|
||||
@Override
|
||||
public void defineTypesIn(TypeDefinition scope) {
|
||||
scope.addEnum(new Enum(scope, "Modifier", "\"Alt\"|\"Control\"|\"Meta\"|\"Shift\""));
|
||||
}
|
||||
}
|
||||
|
||||
private static class PollingOption implements CustomMapping {
|
||||
@Override
|
||||
public void defineTypesIn(TypeDefinition scope) {
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.util.function.BiConsumer;
|
||||
|
||||
public interface Browser {
|
||||
class NewContextOptions {
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE }
|
||||
public class Viewport {
|
||||
public int width;
|
||||
public int height;
|
||||
@@ -216,7 +216,7 @@ public interface Browser {
|
||||
}
|
||||
}
|
||||
class NewPageOptions {
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE }
|
||||
public class Viewport {
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
@@ -161,7 +161,7 @@ public interface BrowserType {
|
||||
}
|
||||
}
|
||||
class LaunchPersistentContextOptions {
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE }
|
||||
public class Proxy {
|
||||
public String server;
|
||||
public String bypass;
|
||||
|
||||
@@ -39,7 +39,7 @@ public interface ChromiumBrowser {
|
||||
}
|
||||
}
|
||||
class NewContextOptions {
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE }
|
||||
public class Viewport {
|
||||
public int width;
|
||||
public int height;
|
||||
@@ -234,7 +234,7 @@ public interface ChromiumBrowser {
|
||||
}
|
||||
}
|
||||
class NewPageOptions {
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE }
|
||||
public class Viewport {
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.microsoft.playwright;
|
||||
|
||||
public interface DeviceDescriptor {
|
||||
interface Viewport {
|
||||
int width();
|
||||
int height();
|
||||
}
|
||||
Viewport viewport();
|
||||
String userAgent();
|
||||
int deviceScaleFactor();
|
||||
boolean isMobile();
|
||||
boolean hasTouch();
|
||||
enum BrowserType { CHROMIUM, FIREFOX, WEBKIT}
|
||||
BrowserType defaultBrowserType();
|
||||
}
|
||||
@@ -20,7 +20,7 @@ import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface ElementHandle {
|
||||
enum ElementState { DISABLED, ENABLED, HIDDEN, STABLE, VISIBLE}
|
||||
enum ElementState { DISABLED, ENABLED, HIDDEN, STABLE, VISIBLE }
|
||||
class CheckOptions {
|
||||
public Boolean force;
|
||||
public Boolean noWaitAfter;
|
||||
@@ -40,8 +40,6 @@ public interface ElementHandle {
|
||||
}
|
||||
}
|
||||
class ClickOptions {
|
||||
public enum Button { LEFT, MIDDLE, RIGHT}
|
||||
public enum Modifier { ALT, CONTROL, META, SHIFT}
|
||||
public class Position {
|
||||
public int x;
|
||||
public int y;
|
||||
@@ -61,16 +59,16 @@ public interface ElementHandle {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
public Button button;
|
||||
public Mouse.Button button;
|
||||
public Integer clickCount;
|
||||
public Integer delay;
|
||||
public Position position;
|
||||
public Set<Modifier> modifiers;
|
||||
public Set<Keyboard.Modifier> modifiers;
|
||||
public Boolean force;
|
||||
public Boolean noWaitAfter;
|
||||
public Integer timeout;
|
||||
|
||||
public ClickOptions withButton(Button button) {
|
||||
public ClickOptions withButton(Mouse.Button button) {
|
||||
this.button = button;
|
||||
return this;
|
||||
}
|
||||
@@ -86,7 +84,7 @@ public interface ElementHandle {
|
||||
this.position = new Position();
|
||||
return this.position;
|
||||
}
|
||||
public ClickOptions withModifiers(Set<Modifier> modifiers) {
|
||||
public ClickOptions withModifiers(Set<Keyboard.Modifier> modifiers) {
|
||||
this.modifiers = modifiers;
|
||||
return this;
|
||||
}
|
||||
@@ -104,8 +102,6 @@ public interface ElementHandle {
|
||||
}
|
||||
}
|
||||
class DblclickOptions {
|
||||
public enum Button { LEFT, MIDDLE, RIGHT}
|
||||
public enum Modifier { ALT, CONTROL, META, SHIFT}
|
||||
public class Position {
|
||||
public int x;
|
||||
public int y;
|
||||
@@ -125,15 +121,15 @@ public interface ElementHandle {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
public Button button;
|
||||
public Mouse.Button button;
|
||||
public Integer delay;
|
||||
public Position position;
|
||||
public Set<Modifier> modifiers;
|
||||
public Set<Keyboard.Modifier> modifiers;
|
||||
public Boolean force;
|
||||
public Boolean noWaitAfter;
|
||||
public Integer timeout;
|
||||
|
||||
public DblclickOptions withButton(Button button) {
|
||||
public DblclickOptions withButton(Mouse.Button button) {
|
||||
this.button = button;
|
||||
return this;
|
||||
}
|
||||
@@ -145,7 +141,7 @@ public interface ElementHandle {
|
||||
this.position = new Position();
|
||||
return this.position;
|
||||
}
|
||||
public DblclickOptions withModifiers(Set<Modifier> modifiers) {
|
||||
public DblclickOptions withModifiers(Set<Keyboard.Modifier> modifiers) {
|
||||
this.modifiers = modifiers;
|
||||
return this;
|
||||
}
|
||||
@@ -176,7 +172,6 @@ public interface ElementHandle {
|
||||
}
|
||||
}
|
||||
class HoverOptions {
|
||||
public enum Modifier { ALT, CONTROL, META, SHIFT}
|
||||
public class Position {
|
||||
public int x;
|
||||
public int y;
|
||||
@@ -197,7 +192,7 @@ public interface ElementHandle {
|
||||
}
|
||||
}
|
||||
public Position position;
|
||||
public Set<Modifier> modifiers;
|
||||
public Set<Keyboard.Modifier> modifiers;
|
||||
public Boolean force;
|
||||
public Integer timeout;
|
||||
|
||||
@@ -205,7 +200,7 @@ public interface ElementHandle {
|
||||
this.position = new Position();
|
||||
return this.position;
|
||||
}
|
||||
public HoverOptions withModifiers(Set<Modifier> modifiers) {
|
||||
public HoverOptions withModifiers(Set<Keyboard.Modifier> modifiers) {
|
||||
this.modifiers = modifiers;
|
||||
return this;
|
||||
}
|
||||
@@ -237,7 +232,7 @@ public interface ElementHandle {
|
||||
}
|
||||
}
|
||||
class ScreenshotOptions {
|
||||
public enum Type { JPEG, PNG}
|
||||
public enum Type { JPEG, PNG }
|
||||
public String path;
|
||||
public Type type;
|
||||
public Integer quality;
|
||||
@@ -352,7 +347,7 @@ public interface ElementHandle {
|
||||
}
|
||||
}
|
||||
class WaitForSelectorOptions {
|
||||
public enum State { ATTACHED, DETACHED, HIDDEN, VISIBLE}
|
||||
public enum State { ATTACHED, DETACHED, HIDDEN, VISIBLE }
|
||||
public State state;
|
||||
public Integer timeout;
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.util.function.BiConsumer;
|
||||
|
||||
public interface FirefoxBrowser {
|
||||
class NewContextOptions {
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE }
|
||||
public class Viewport {
|
||||
public int width;
|
||||
public int height;
|
||||
@@ -216,7 +216,7 @@ public interface FirefoxBrowser {
|
||||
}
|
||||
}
|
||||
class NewPageOptions {
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE }
|
||||
public class Viewport {
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Frame {
|
||||
enum LoadState { DOMCONTENTLOADED, LOAD, NETWORKIDLE}
|
||||
enum LoadState { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
class AddScriptTagOptions {
|
||||
public String url;
|
||||
public String path;
|
||||
@@ -81,8 +81,6 @@ public interface Frame {
|
||||
}
|
||||
}
|
||||
class ClickOptions {
|
||||
public enum Button { LEFT, MIDDLE, RIGHT}
|
||||
public enum Modifier { ALT, CONTROL, META, SHIFT}
|
||||
public class Position {
|
||||
public int x;
|
||||
public int y;
|
||||
@@ -102,16 +100,16 @@ public interface Frame {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
public Button button;
|
||||
public Mouse.Button button;
|
||||
public Integer clickCount;
|
||||
public Integer delay;
|
||||
public Position position;
|
||||
public Set<Modifier> modifiers;
|
||||
public Set<Keyboard.Modifier> modifiers;
|
||||
public Boolean force;
|
||||
public Boolean noWaitAfter;
|
||||
public Integer timeout;
|
||||
|
||||
public ClickOptions withButton(Button button) {
|
||||
public ClickOptions withButton(Mouse.Button button) {
|
||||
this.button = button;
|
||||
return this;
|
||||
}
|
||||
@@ -127,7 +125,7 @@ public interface Frame {
|
||||
this.position = new Position();
|
||||
return this.position;
|
||||
}
|
||||
public ClickOptions withModifiers(Set<Modifier> modifiers) {
|
||||
public ClickOptions withModifiers(Set<Keyboard.Modifier> modifiers) {
|
||||
this.modifiers = modifiers;
|
||||
return this;
|
||||
}
|
||||
@@ -145,8 +143,6 @@ public interface Frame {
|
||||
}
|
||||
}
|
||||
class DblclickOptions {
|
||||
public enum Button { LEFT, MIDDLE, RIGHT}
|
||||
public enum Modifier { ALT, CONTROL, META, SHIFT}
|
||||
public class Position {
|
||||
public int x;
|
||||
public int y;
|
||||
@@ -166,15 +162,15 @@ public interface Frame {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
public Button button;
|
||||
public Mouse.Button button;
|
||||
public Integer delay;
|
||||
public Position position;
|
||||
public Set<Modifier> modifiers;
|
||||
public Set<Keyboard.Modifier> modifiers;
|
||||
public Boolean force;
|
||||
public Boolean noWaitAfter;
|
||||
public Integer timeout;
|
||||
|
||||
public DblclickOptions withButton(Button button) {
|
||||
public DblclickOptions withButton(Mouse.Button button) {
|
||||
this.button = button;
|
||||
return this;
|
||||
}
|
||||
@@ -186,7 +182,7 @@ public interface Frame {
|
||||
this.position = new Position();
|
||||
return this.position;
|
||||
}
|
||||
public DblclickOptions withModifiers(Set<Modifier> modifiers) {
|
||||
public DblclickOptions withModifiers(Set<Keyboard.Modifier> modifiers) {
|
||||
this.modifiers = modifiers;
|
||||
return this;
|
||||
}
|
||||
@@ -241,7 +237,7 @@ public interface Frame {
|
||||
}
|
||||
}
|
||||
class NavigateOptions {
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE}
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
public Integer timeout;
|
||||
public WaitUntil waitUntil;
|
||||
public String referer;
|
||||
@@ -260,7 +256,6 @@ public interface Frame {
|
||||
}
|
||||
}
|
||||
class HoverOptions {
|
||||
public enum Modifier { ALT, CONTROL, META, SHIFT}
|
||||
public class Position {
|
||||
public int x;
|
||||
public int y;
|
||||
@@ -281,7 +276,7 @@ public interface Frame {
|
||||
}
|
||||
}
|
||||
public Position position;
|
||||
public Set<Modifier> modifiers;
|
||||
public Set<Keyboard.Modifier> modifiers;
|
||||
public Boolean force;
|
||||
public Integer timeout;
|
||||
|
||||
@@ -289,7 +284,7 @@ public interface Frame {
|
||||
this.position = new Position();
|
||||
return this.position;
|
||||
}
|
||||
public HoverOptions withModifiers(Set<Modifier> modifiers) {
|
||||
public HoverOptions withModifiers(Set<Keyboard.Modifier> modifiers) {
|
||||
this.modifiers = modifiers;
|
||||
return this;
|
||||
}
|
||||
@@ -350,7 +345,7 @@ public interface Frame {
|
||||
}
|
||||
}
|
||||
class SetContentOptions {
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE}
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
public Integer timeout;
|
||||
public WaitUntil waitUntil;
|
||||
|
||||
@@ -442,7 +437,7 @@ public interface Frame {
|
||||
}
|
||||
}
|
||||
class WaitForNavigationOptions {
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE}
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
public Integer timeout;
|
||||
public String url;
|
||||
public WaitUntil waitUntil;
|
||||
@@ -461,7 +456,7 @@ public interface Frame {
|
||||
}
|
||||
}
|
||||
class WaitForSelectorOptions {
|
||||
public enum State { ATTACHED, DETACHED, HIDDEN, VISIBLE}
|
||||
public enum State { ATTACHED, DETACHED, HIDDEN, VISIBLE }
|
||||
public State state;
|
||||
public Integer timeout;
|
||||
|
||||
|
||||
@@ -20,32 +20,18 @@ import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Keyboard {
|
||||
class PressOptions {
|
||||
public Integer delay;
|
||||
enum Modifier { ALT, CONTROL, META, SHIFT }
|
||||
|
||||
public PressOptions withDelay(Integer delay) {
|
||||
this.delay = delay;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
class TypeOptions {
|
||||
public Integer delay;
|
||||
|
||||
public TypeOptions withDelay(Integer delay) {
|
||||
this.delay = delay;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
void down(String key);
|
||||
void insertText(String text);
|
||||
default void press(String key) {
|
||||
press(key, null);
|
||||
press(key, 0);
|
||||
}
|
||||
void press(String key, PressOptions options);
|
||||
void press(String key, int delay);
|
||||
default void type(String text) {
|
||||
type(text, null);
|
||||
type(text, 0);
|
||||
}
|
||||
void type(String text, TypeOptions options);
|
||||
void type(String text, int delay);
|
||||
void up(String key);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Logger {
|
||||
enum Severity { ERROR, INFO, VERBOSE, WARNING}
|
||||
enum Severity { ERROR, INFO, VERBOSE, WARNING }
|
||||
class LogHints {
|
||||
public String color;
|
||||
|
||||
|
||||
@@ -20,8 +20,9 @@ import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Mouse {
|
||||
enum Button { LEFT, MIDDLE, RIGHT }
|
||||
|
||||
class ClickOptions {
|
||||
public enum Button { LEFT, MIDDLE, RIGHT}
|
||||
public Button button;
|
||||
public Integer clickCount;
|
||||
public Integer delay;
|
||||
@@ -40,7 +41,6 @@ public interface Mouse {
|
||||
}
|
||||
}
|
||||
class DblclickOptions {
|
||||
public enum Button { LEFT, MIDDLE, RIGHT}
|
||||
public Button button;
|
||||
public Integer delay;
|
||||
|
||||
@@ -54,7 +54,6 @@ public interface Mouse {
|
||||
}
|
||||
}
|
||||
class DownOptions {
|
||||
public enum Button { LEFT, MIDDLE, RIGHT}
|
||||
public Button button;
|
||||
public Integer clickCount;
|
||||
|
||||
@@ -76,7 +75,6 @@ public interface Mouse {
|
||||
}
|
||||
}
|
||||
class UpOptions {
|
||||
public enum Button { LEFT, MIDDLE, RIGHT}
|
||||
public Button button;
|
||||
public Integer clickCount;
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Page {
|
||||
enum LoadState { DOMCONTENTLOADED, LOAD, NETWORKIDLE}
|
||||
enum LoadState { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
class CloseOptions {
|
||||
public Boolean runBeforeUnload;
|
||||
|
||||
@@ -89,8 +89,6 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
class ClickOptions {
|
||||
public enum Button { LEFT, MIDDLE, RIGHT}
|
||||
public enum Modifier { ALT, CONTROL, META, SHIFT}
|
||||
public class Position {
|
||||
public int x;
|
||||
public int y;
|
||||
@@ -110,16 +108,16 @@ public interface Page {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
public Button button;
|
||||
public Mouse.Button button;
|
||||
public Integer clickCount;
|
||||
public Integer delay;
|
||||
public Position position;
|
||||
public Set<Modifier> modifiers;
|
||||
public Set<Keyboard.Modifier> modifiers;
|
||||
public Boolean force;
|
||||
public Boolean noWaitAfter;
|
||||
public Integer timeout;
|
||||
|
||||
public ClickOptions withButton(Button button) {
|
||||
public ClickOptions withButton(Mouse.Button button) {
|
||||
this.button = button;
|
||||
return this;
|
||||
}
|
||||
@@ -135,7 +133,7 @@ public interface Page {
|
||||
this.position = new Position();
|
||||
return this.position;
|
||||
}
|
||||
public ClickOptions withModifiers(Set<Modifier> modifiers) {
|
||||
public ClickOptions withModifiers(Set<Keyboard.Modifier> modifiers) {
|
||||
this.modifiers = modifiers;
|
||||
return this;
|
||||
}
|
||||
@@ -153,8 +151,6 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
class DblclickOptions {
|
||||
public enum Button { LEFT, MIDDLE, RIGHT}
|
||||
public enum Modifier { ALT, CONTROL, META, SHIFT}
|
||||
public class Position {
|
||||
public int x;
|
||||
public int y;
|
||||
@@ -174,15 +170,15 @@ public interface Page {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
public Button button;
|
||||
public Mouse.Button button;
|
||||
public Integer delay;
|
||||
public Position position;
|
||||
public Set<Modifier> modifiers;
|
||||
public Set<Keyboard.Modifier> modifiers;
|
||||
public Boolean force;
|
||||
public Boolean noWaitAfter;
|
||||
public Integer timeout;
|
||||
|
||||
public DblclickOptions withButton(Button button) {
|
||||
public DblclickOptions withButton(Mouse.Button button) {
|
||||
this.button = button;
|
||||
return this;
|
||||
}
|
||||
@@ -194,7 +190,7 @@ public interface Page {
|
||||
this.position = new Position();
|
||||
return this.position;
|
||||
}
|
||||
public DblclickOptions withModifiers(Set<Modifier> modifiers) {
|
||||
public DblclickOptions withModifiers(Set<Keyboard.Modifier> modifiers) {
|
||||
this.modifiers = modifiers;
|
||||
return this;
|
||||
}
|
||||
@@ -220,8 +216,8 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
class EmulateMediaOptions {
|
||||
public enum Media { PRINT, SCREEN}
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
|
||||
public enum Media { PRINT, SCREEN }
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE }
|
||||
public Media media;
|
||||
public ColorScheme colorScheme;
|
||||
|
||||
@@ -264,7 +260,7 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
class GoBackOptions {
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE}
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
public Integer timeout;
|
||||
public WaitUntil waitUntil;
|
||||
|
||||
@@ -278,7 +274,7 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
class GoForwardOptions {
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE}
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
public Integer timeout;
|
||||
public WaitUntil waitUntil;
|
||||
|
||||
@@ -292,7 +288,7 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
class NavigateOptions {
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE}
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
public Integer timeout;
|
||||
public WaitUntil waitUntil;
|
||||
public String referer;
|
||||
@@ -311,7 +307,6 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
class HoverOptions {
|
||||
public enum Modifier { ALT, CONTROL, META, SHIFT}
|
||||
public class Position {
|
||||
public int x;
|
||||
public int y;
|
||||
@@ -332,7 +327,7 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
public Position position;
|
||||
public Set<Modifier> modifiers;
|
||||
public Set<Keyboard.Modifier> modifiers;
|
||||
public Boolean force;
|
||||
public Integer timeout;
|
||||
|
||||
@@ -340,7 +335,7 @@ public interface Page {
|
||||
this.position = new Position();
|
||||
return this.position;
|
||||
}
|
||||
public HoverOptions withModifiers(Set<Modifier> modifiers) {
|
||||
public HoverOptions withModifiers(Set<Keyboard.Modifier> modifiers) {
|
||||
this.modifiers = modifiers;
|
||||
return this;
|
||||
}
|
||||
@@ -485,7 +480,7 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
class ReloadOptions {
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE}
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
public Integer timeout;
|
||||
public WaitUntil waitUntil;
|
||||
|
||||
@@ -499,7 +494,7 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
class ScreenshotOptions {
|
||||
public enum Type { JPEG, PNG}
|
||||
public enum Type { JPEG, PNG }
|
||||
public class Clip {
|
||||
public int x;
|
||||
public int y;
|
||||
@@ -580,7 +575,7 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
class SetContentOptions {
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE}
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
public Integer timeout;
|
||||
public WaitUntil waitUntil;
|
||||
|
||||
@@ -696,7 +691,7 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
class WaitForNavigationOptions {
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE}
|
||||
public enum WaitUntil { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
public Integer timeout;
|
||||
public String url;
|
||||
public WaitUntil waitUntil;
|
||||
@@ -731,7 +726,7 @@ public interface Page {
|
||||
}
|
||||
}
|
||||
class WaitForSelectorOptions {
|
||||
public enum State { ATTACHED, DETACHED, HIDDEN, VISIBLE}
|
||||
public enum State { ATTACHED, DETACHED, HIDDEN, VISIBLE }
|
||||
public State state;
|
||||
public Integer timeout;
|
||||
|
||||
@@ -761,6 +756,7 @@ public interface Page {
|
||||
return evalOnSelectorAll(selector, pageFunction, null);
|
||||
}
|
||||
Object evalOnSelectorAll(String selector, String pageFunction, Object arg);
|
||||
Accessibility accessibility();
|
||||
default void addInitScript(String script) {
|
||||
addInitScript(script, null);
|
||||
}
|
||||
@@ -778,6 +774,7 @@ public interface Page {
|
||||
void click(String selector, ClickOptions options);
|
||||
String content();
|
||||
BrowserContext context();
|
||||
ChromiumCoverage coverage();
|
||||
default void dblclick(String selector) {
|
||||
dblclick(selector, null);
|
||||
}
|
||||
@@ -839,7 +836,9 @@ public interface Page {
|
||||
}
|
||||
String innerText(String selector, InnerTextOptions options);
|
||||
boolean isClosed();
|
||||
Keyboard keyboard();
|
||||
Frame mainFrame();
|
||||
Mouse mouse();
|
||||
Page opener();
|
||||
default byte[] pdf() {
|
||||
return pdf(null);
|
||||
|
||||
@@ -19,6 +19,7 @@ package com.microsoft.playwright;
|
||||
import com.microsoft.playwright.impl.PlaywrightImpl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public interface Playwright {
|
||||
static Playwright create() {
|
||||
@@ -28,4 +29,6 @@ public interface Playwright {
|
||||
BrowserType chromium();
|
||||
BrowserType firefox();
|
||||
BrowserType webkit();
|
||||
|
||||
Map<String, DeviceDescriptor> devices();
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.util.function.BiConsumer;
|
||||
|
||||
public interface WebKitBrowser {
|
||||
class NewContextOptions {
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE }
|
||||
public class Viewport {
|
||||
public int width;
|
||||
public int height;
|
||||
@@ -216,7 +216,7 @@ public interface WebKitBrowser {
|
||||
}
|
||||
}
|
||||
class NewPageOptions {
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
|
||||
public enum ColorScheme { DARK, LIGHT, NO_PREFERENCE }
|
||||
public class Viewport {
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.microsoft.playwright.impl;
|
||||
|
||||
import com.microsoft.playwright.DeviceDescriptor;
|
||||
|
||||
class DeviceDescriptorImpl implements DeviceDescriptor {
|
||||
private static class ViewportImpl implements Viewport{
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
@Override
|
||||
public int width() {
|
||||
return width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int height() {
|
||||
return height;
|
||||
}
|
||||
}
|
||||
private ViewportImpl viewport;
|
||||
private String userAgent;
|
||||
private int deviceScaleFactor;
|
||||
private boolean isMobile;
|
||||
private boolean hasTouch;
|
||||
// 'chromium' | 'firefox' | 'webkit'
|
||||
private String defaultBrowserType;
|
||||
|
||||
@Override
|
||||
public Viewport viewport() {
|
||||
return viewport;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String userAgent() {
|
||||
return userAgent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deviceScaleFactor() {
|
||||
return deviceScaleFactor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMobile() {
|
||||
return isMobile;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTouch() {
|
||||
return hasTouch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BrowserType defaultBrowserType() {
|
||||
switch (defaultBrowserType) {
|
||||
case "chromium": return BrowserType.CHROMIUM;
|
||||
case "firefox": return BrowserType.FIREFOX;
|
||||
case "webkit": return BrowserType.WEBKIT;
|
||||
default: throw new RuntimeException("Unknown type: " + defaultBrowserType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -194,7 +194,7 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
}
|
||||
|
||||
|
||||
private static String toProtocol(ClickOptions.Button button) {
|
||||
private static String toProtocol(Mouse.Button button) {
|
||||
switch (button) {
|
||||
case LEFT: return "left";
|
||||
case RIGHT: return "right";
|
||||
@@ -203,18 +203,18 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
}
|
||||
}
|
||||
|
||||
private static JsonArray toProtocol(Set<ClickOptions.Modifier> modifiers) {
|
||||
private static JsonArray toProtocol(Set<Keyboard.Modifier> modifiers) {
|
||||
JsonArray result = new JsonArray();
|
||||
if (modifiers.contains(ClickOptions.Modifier.ALT)) {
|
||||
if (modifiers.contains(Keyboard.Modifier.ALT)) {
|
||||
result.add("Alt");
|
||||
}
|
||||
if (modifiers.contains(ClickOptions.Modifier.CONTROL)) {
|
||||
if (modifiers.contains(Keyboard.Modifier.CONTROL)) {
|
||||
result.add("Control");
|
||||
}
|
||||
if (modifiers.contains(ClickOptions.Modifier.META)) {
|
||||
if (modifiers.contains(Keyboard.Modifier.META)) {
|
||||
result.add("Meta");
|
||||
}
|
||||
if (modifiers.contains(ClickOptions.Modifier.SHIFT)) {
|
||||
if (modifiers.contains(Keyboard.Modifier.SHIFT)) {
|
||||
result.add("Shift");
|
||||
}
|
||||
return result;
|
||||
@@ -238,8 +238,6 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
params.add("modifiers", toProtocol(options.modifiers));
|
||||
}
|
||||
|
||||
// System.err.println(new Gson().toJson(params));
|
||||
|
||||
sendMessage("click", params);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.microsoft.playwright.impl;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.microsoft.playwright.Keyboard;
|
||||
|
||||
class KeyboardImpl implements Keyboard {
|
||||
private final ChannelOwner page;
|
||||
|
||||
KeyboardImpl(ChannelOwner page) {
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void down(String key) {
|
||||
JsonObject params = new JsonObject();
|
||||
params.addProperty("key", key);
|
||||
page.sendMessage("keyboardDown", params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertText(String text) {
|
||||
JsonObject params = new JsonObject();
|
||||
params.addProperty("text", text);
|
||||
page.sendMessage("keyboardInsertText", params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void press(String key, int delay) {
|
||||
JsonObject params = new JsonObject();
|
||||
params.addProperty("key", key);
|
||||
if (delay != 0) {
|
||||
params.addProperty("delay", delay);
|
||||
}
|
||||
page.sendMessage("keyboardPress", params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void type(String text, int delay) {
|
||||
JsonObject params = new JsonObject();
|
||||
params.addProperty("text", text);
|
||||
if (delay != 0) {
|
||||
params.addProperty("delay", delay);
|
||||
}
|
||||
page.sendMessage("keyboardType", params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void up(String key) {
|
||||
JsonObject params = new JsonObject();
|
||||
params.addProperty("key", key);
|
||||
page.sendMessage("keyboardUp", params);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.microsoft.playwright.impl;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.microsoft.playwright.Mouse;
|
||||
|
||||
import static com.microsoft.playwright.impl.Utils.convertViaJson;
|
||||
|
||||
class MouseImpl implements Mouse {
|
||||
private final ChannelOwner page;
|
||||
|
||||
MouseImpl(ChannelOwner page) {
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
private static String toProtocol(Mouse.Button button) {
|
||||
switch (button) {
|
||||
case LEFT: return "left";
|
||||
case RIGHT: return "right";
|
||||
case MIDDLE: return "middle";
|
||||
default: throw new RuntimeException("Unexpected value: " + button);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click(int x, int y, ClickOptions options) {
|
||||
if (options == null) {
|
||||
options = new ClickOptions();
|
||||
}
|
||||
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
|
||||
params.addProperty("x", x);
|
||||
params.addProperty("y", y);
|
||||
if (options.button != null) {
|
||||
params.remove("button");
|
||||
params.addProperty("button", toProtocol(options.button));
|
||||
}
|
||||
page.sendMessage("mouseClick", params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dblclick(int x, int y, DblclickOptions options) {
|
||||
ClickOptions clickOptions;
|
||||
if (options == null) {
|
||||
clickOptions = new ClickOptions();
|
||||
} else {
|
||||
clickOptions = convertViaJson(options, ClickOptions.class);
|
||||
}
|
||||
clickOptions.clickCount = 2;
|
||||
click(x, y, clickOptions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void down(DownOptions options) {
|
||||
if (options == null) {
|
||||
options = new DownOptions();
|
||||
}
|
||||
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
|
||||
page.sendMessage("mouseDown", params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(int x, int y, MoveOptions options) {
|
||||
if (options == null) {
|
||||
options = new MoveOptions();
|
||||
}
|
||||
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
|
||||
params.addProperty("x", x);
|
||||
params.addProperty("y", y);
|
||||
page.sendMessage("mouseMove", params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void up(UpOptions options) {
|
||||
if (options == null) {
|
||||
options = new UpOptions();
|
||||
}
|
||||
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
|
||||
page.sendMessage("mouseUp", params);
|
||||
}
|
||||
}
|
||||
@@ -28,9 +28,13 @@ import java.util.concurrent.Future;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static com.microsoft.playwright.impl.Utils.convertViaJson;
|
||||
|
||||
|
||||
public class PageImpl extends ChannelOwner implements Page {
|
||||
private final FrameImpl mainFrame;
|
||||
private final KeyboardImpl keyboard;
|
||||
private final MouseImpl mouse;
|
||||
private final List<DialogHandler> dialogHandlers = new ArrayList<>();
|
||||
private final List<Listener<ConsoleMessage>> consoleListeners = new ArrayList<>();
|
||||
|
||||
@@ -38,6 +42,8 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
super(parent, type, guid, initializer);
|
||||
mainFrame = connection.getExistingObject(initializer.getAsJsonObject("mainFrame").get("guid").getAsString());
|
||||
mainFrame.page = this;
|
||||
keyboard = new KeyboardImpl(this);
|
||||
mouse = new MouseImpl(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -120,6 +126,11 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
return mainFrame.evalOnSelectorAll(selector, pageFunction, arg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Accessibility accessibility() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInitScript(String script, Object arg) {
|
||||
|
||||
@@ -160,6 +171,11 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChromiumCoverage coverage() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dblclick(String selector, DblclickOptions options) {
|
||||
|
||||
@@ -232,8 +248,7 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
|
||||
@Override
|
||||
public ResponseImpl navigate(String url, NavigateOptions options) {
|
||||
// TODO: convert params
|
||||
return mainFrame.navigate(url, new Frame.NavigateOptions());
|
||||
return mainFrame.navigate(url, convertViaJson(options, Frame.NavigateOptions.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -256,9 +271,19 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Keyboard keyboard() {
|
||||
return keyboard;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Frame mainFrame() {
|
||||
return null;
|
||||
return mainFrame;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mouse mouse() {
|
||||
return mouse;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -298,10 +323,7 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
|
||||
@Override
|
||||
public void setContent(String html, SetContentOptions options) {
|
||||
// TODO: generate converter.
|
||||
String json = new Gson().toJson(options);
|
||||
Frame.SetContentOptions frameOptions = new Gson().fromJson(json, Frame.SetContentOptions.class);
|
||||
mainFrame.setContent(html, frameOptions);
|
||||
mainFrame.setContent(html, convertViaJson(options, Frame.SetContentOptions.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -408,9 +430,4 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
public List<Worker> workers() {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static <F, T> T convertViaJson(F f, Class<T> t) {
|
||||
String json = new Gson().toJson(f);
|
||||
return new Gson().fromJson(json, t);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,13 +16,19 @@
|
||||
|
||||
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.microsoft.playwright.BrowserType;
|
||||
import com.microsoft.playwright.DeviceDescriptor;
|
||||
import com.microsoft.playwright.Playwright;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PlaywrightImpl extends ChannelOwner implements Playwright {
|
||||
public static PlaywrightImpl create() {
|
||||
@@ -45,12 +51,21 @@ public class PlaywrightImpl extends ChannelOwner implements Playwright {
|
||||
private final BrowserTypeImpl chromium;
|
||||
private final BrowserTypeImpl firefox;
|
||||
private final BrowserTypeImpl webkit;
|
||||
private final Map<String, DeviceDescriptor> devices = new HashMap<>();
|
||||
|
||||
public PlaywrightImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
|
||||
super(parent, type, guid, initializer);
|
||||
chromium = parent.connection.getExistingObject(initializer.getAsJsonObject("chromium").get("guid").getAsString());
|
||||
firefox = parent.connection.getExistingObject(initializer.getAsJsonObject("firefox").get("guid").getAsString());
|
||||
webkit = parent.connection.getExistingObject(initializer.getAsJsonObject("webkit").get("guid").getAsString());
|
||||
|
||||
Gson gson = new Gson();
|
||||
for (JsonElement item : initializer.getAsJsonArray("deviceDescriptors")) {
|
||||
JsonObject o = item.getAsJsonObject();
|
||||
String name = o.get("name").getAsString();
|
||||
DeviceDescriptorImpl descriptor = gson.fromJson(o.get("descriptor"), DeviceDescriptorImpl.class);
|
||||
devices.put(name, descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -67,4 +82,9 @@ public class PlaywrightImpl extends ChannelOwner implements Playwright {
|
||||
public BrowserTypeImpl webkit() {
|
||||
return webkit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, DeviceDescriptor> devices() {
|
||||
return devices;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.microsoft.playwright.impl;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
class Utils {
|
||||
// TODO: generate converter.
|
||||
static <F, T> T convertViaJson(F f, Class<T> t) {
|
||||
String json = new Gson().toJson(f);
|
||||
return new Gson().fromJson(json, t);
|
||||
}
|
||||
}
|
||||
@@ -254,4 +254,21 @@ public class TestClick {
|
||||
page.click("label[for='agree']");
|
||||
assertFalse((Boolean) page.evaluate("() => window['result'].check"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void should_not_hang_with_touch_enabled_viewports() {
|
||||
// @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()
|
||||
.withHasTouch(descriptor.hasTouch()));
|
||||
Page page = context.newPage();
|
||||
page.mouse().down();
|
||||
page.mouse().move(100, 10);
|
||||
page.mouse().up();
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user