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 43edfa44..31c4c4b7 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 @@ -478,6 +478,10 @@ class Field extends Element { super(parent, jsonElement); this.name = name; this.type = new TypeRef(this, jsonElement.getAsJsonObject().get("type")); + + if (name.equals("position")) { + System.out.println(jsonPath + " " + type.jsonName); + } } void writeTo(List output, String offset, String access) { @@ -531,6 +535,25 @@ class Field extends Element { output.add(offset + "}"); return; } + if (asList("Page.click.options.position", + "Page.dblclick.options.position", + "Page.hover.options.position", + "Frame.click.options.position", + "Frame.dblclick.options.position", + "Frame.hover.options.position", + "ElementHandle.click.options.position", + "ElementHandle.dblclick.options.position", + "ElementHandle.hover.options.position").contains(jsonPath)) { + output.add(offset + "public " + parentClass + " withPosition(Position position) {"); + output.add(offset + " this.position = position;"); + output.add(offset + " return this;"); + output.add(offset + "}"); + output.add(offset + "public " + parentClass + " withPosition(int x, int y) {"); + output.add(offset + " return withPosition(new Position(x, y));"); + output.add(offset + "}"); + return; + } + if (jsonPath.equals("Route.continue.overrides.postData")) { output.add(offset + "public ContinueOverrides withPostData(String postData) {"); output.add(offset + " this.postData = postData.getBytes(StandardCharsets.UTF_8);"); 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 46fc179d..ac032852 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 @@ -208,6 +208,16 @@ class Types { add("Page.goto.options", "Object", "NavigateOptions"); add("Frame.goto.options", "Object", "NavigateOptions"); + add("Page.click.options.position", "Object", "Position", new Empty()); + add("Page.dblclick.options.position", "Object", "Position", new Empty()); + add("Page.hover.options.position", "Object", "Position", new Empty()); + add("Frame.click.options.position", "Object", "Position", new Empty()); + add("Frame.dblclick.options.position", "Object", "Position", new Empty()); + add("Frame.hover.options.position", "Object", "Position", new Empty()); + add("ElementHandle.click.options.position", "Object", "Position", new Empty()); + add("ElementHandle.dblclick.options.position", "Object", "Position", new Empty()); + add("ElementHandle.hover.options.position", "Object", "Position", new Empty()); + // The method has custom signatures add("BrowserContext.cookies", "Promise>", "Cookie"); add("BrowserContext.cookies.sameSite", "\"Lax\"|\"None\"|\"Strict\"", "SameSite", new Empty()); diff --git a/playwright/src/main/java/com/microsoft/playwright/ElementHandle.java b/playwright/src/main/java/com/microsoft/playwright/ElementHandle.java index 44133454..facb0864 100644 --- a/playwright/src/main/java/com/microsoft/playwright/ElementHandle.java +++ b/playwright/src/main/java/com/microsoft/playwright/ElementHandle.java @@ -66,25 +66,6 @@ public interface ElementHandle extends JSHandle { } } class ClickOptions { - public class Position { - public int x; - public int y; - - Position() { - } - public ClickOptions done() { - return ClickOptions.this; - } - - public Position withX(int x) { - this.x = x; - return this; - } - public Position withY(int y) { - this.y = y; - return this; - } - } public Mouse.Button button; public Integer clickCount; public Integer delay; @@ -106,9 +87,12 @@ public interface ElementHandle extends JSHandle { this.delay = delay; return this; } - public Position setPosition() { - this.position = new Position(); - return this.position; + public ClickOptions withPosition(Position position) { + this.position = position; + return this; + } + public ClickOptions withPosition(int x, int y) { + return withPosition(new Position(x, y)); } public ClickOptions withModifiers(Keyboard.Modifier... modifiers) { this.modifiers = new HashSet<>(Arrays.asList(modifiers)); @@ -128,25 +112,6 @@ public interface ElementHandle extends JSHandle { } } class DblclickOptions { - public class Position { - public int x; - public int y; - - Position() { - } - public DblclickOptions done() { - return DblclickOptions.this; - } - - public Position withX(int x) { - this.x = x; - return this; - } - public Position withY(int y) { - this.y = y; - return this; - } - } public Mouse.Button button; public Integer delay; public Position position; @@ -163,9 +128,12 @@ public interface ElementHandle extends JSHandle { this.delay = delay; return this; } - public Position setPosition() { - this.position = new Position(); - return this.position; + public DblclickOptions withPosition(Position position) { + this.position = position; + return this; + } + public DblclickOptions withPosition(int x, int y) { + return withPosition(new Position(x, y)); } public DblclickOptions withModifiers(Keyboard.Modifier... modifiers) { this.modifiers = new HashSet<>(Arrays.asList(modifiers)); @@ -198,33 +166,17 @@ public interface ElementHandle extends JSHandle { } } class HoverOptions { - public class Position { - public int x; - public int y; - - Position() { - } - public HoverOptions done() { - return HoverOptions.this; - } - - public Position withX(int x) { - this.x = x; - return this; - } - public Position withY(int y) { - this.y = y; - return this; - } - } public Position position; public Set modifiers; public Boolean force; public Integer timeout; - public Position setPosition() { - this.position = new Position(); - return this.position; + public HoverOptions withPosition(Position position) { + this.position = position; + return this; + } + public HoverOptions withPosition(int x, int y) { + return withPosition(new Position(x, y)); } public HoverOptions withModifiers(Keyboard.Modifier... modifiers) { this.modifiers = new HashSet<>(Arrays.asList(modifiers)); diff --git a/playwright/src/main/java/com/microsoft/playwright/Frame.java b/playwright/src/main/java/com/microsoft/playwright/Frame.java index bf5a5ba0..ed5f5c57 100644 --- a/playwright/src/main/java/com/microsoft/playwright/Frame.java +++ b/playwright/src/main/java/com/microsoft/playwright/Frame.java @@ -83,25 +83,6 @@ public interface Frame { } } class ClickOptions { - public class Position { - public int x; - public int y; - - Position() { - } - public ClickOptions done() { - return ClickOptions.this; - } - - public Position withX(int x) { - this.x = x; - return this; - } - public Position withY(int y) { - this.y = y; - return this; - } - } public Mouse.Button button; public Integer clickCount; public Integer delay; @@ -123,9 +104,12 @@ public interface Frame { this.delay = delay; return this; } - public Position setPosition() { - this.position = new Position(); - return this.position; + public ClickOptions withPosition(Position position) { + this.position = position; + return this; + } + public ClickOptions withPosition(int x, int y) { + return withPosition(new Position(x, y)); } public ClickOptions withModifiers(Keyboard.Modifier... modifiers) { this.modifiers = new HashSet<>(Arrays.asList(modifiers)); @@ -145,25 +129,6 @@ public interface Frame { } } class DblclickOptions { - public class Position { - public int x; - public int y; - - Position() { - } - public DblclickOptions done() { - return DblclickOptions.this; - } - - public Position withX(int x) { - this.x = x; - return this; - } - public Position withY(int y) { - this.y = y; - return this; - } - } public Mouse.Button button; public Integer delay; public Position position; @@ -180,9 +145,12 @@ public interface Frame { this.delay = delay; return this; } - public Position setPosition() { - this.position = new Position(); - return this.position; + public DblclickOptions withPosition(Position position) { + this.position = position; + return this; + } + public DblclickOptions withPosition(int x, int y) { + return withPosition(new Position(x, y)); } public DblclickOptions withModifiers(Keyboard.Modifier... modifiers) { this.modifiers = new HashSet<>(Arrays.asList(modifiers)); @@ -257,33 +225,17 @@ public interface Frame { } } class HoverOptions { - public class Position { - public int x; - public int y; - - Position() { - } - public HoverOptions done() { - return HoverOptions.this; - } - - public Position withX(int x) { - this.x = x; - return this; - } - public Position withY(int y) { - this.y = y; - return this; - } - } public Position position; public Set modifiers; public Boolean force; public Integer timeout; - public Position setPosition() { - this.position = new Position(); - return this.position; + public HoverOptions withPosition(Position position) { + this.position = position; + return this; + } + public HoverOptions withPosition(int x, int y) { + return withPosition(new Position(x, y)); } public HoverOptions withModifiers(Keyboard.Modifier... modifiers) { this.modifiers = new HashSet<>(Arrays.asList(modifiers)); diff --git a/playwright/src/main/java/com/microsoft/playwright/Page.java b/playwright/src/main/java/com/microsoft/playwright/Page.java index 22fc8daf..5a4d4c10 100644 --- a/playwright/src/main/java/com/microsoft/playwright/Page.java +++ b/playwright/src/main/java/com/microsoft/playwright/Page.java @@ -166,25 +166,6 @@ public interface Page { } } class ClickOptions { - public class Position { - public int x; - public int y; - - Position() { - } - public ClickOptions done() { - return ClickOptions.this; - } - - public Position withX(int x) { - this.x = x; - return this; - } - public Position withY(int y) { - this.y = y; - return this; - } - } public Mouse.Button button; public Integer clickCount; public Integer delay; @@ -206,9 +187,12 @@ public interface Page { this.delay = delay; return this; } - public Position setPosition() { - this.position = new Position(); - return this.position; + public ClickOptions withPosition(Position position) { + this.position = position; + return this; + } + public ClickOptions withPosition(int x, int y) { + return withPosition(new Position(x, y)); } public ClickOptions withModifiers(Keyboard.Modifier... modifiers) { this.modifiers = new HashSet<>(Arrays.asList(modifiers)); @@ -228,25 +212,6 @@ public interface Page { } } class DblclickOptions { - public class Position { - public int x; - public int y; - - Position() { - } - public DblclickOptions done() { - return DblclickOptions.this; - } - - public Position withX(int x) { - this.x = x; - return this; - } - public Position withY(int y) { - this.y = y; - return this; - } - } public Mouse.Button button; public Integer delay; public Position position; @@ -263,9 +228,12 @@ public interface Page { this.delay = delay; return this; } - public Position setPosition() { - this.position = new Position(); - return this.position; + public DblclickOptions withPosition(Position position) { + this.position = position; + return this; + } + public DblclickOptions withPosition(int x, int y) { + return withPosition(new Position(x, y)); } public DblclickOptions withModifiers(Keyboard.Modifier... modifiers) { this.modifiers = new HashSet<>(Arrays.asList(modifiers)); @@ -389,33 +357,17 @@ public interface Page { } } class HoverOptions { - public class Position { - public int x; - public int y; - - Position() { - } - public HoverOptions done() { - return HoverOptions.this; - } - - public Position withX(int x) { - this.x = x; - return this; - } - public Position withY(int y) { - this.y = y; - return this; - } - } public Position position; public Set modifiers; public Boolean force; public Integer timeout; - public Position setPosition() { - this.position = new Position(); - return this.position; + public HoverOptions withPosition(Position position) { + this.position = position; + return this; + } + public HoverOptions withPosition(int x, int y) { + return withPosition(new Position(x, y)); } public HoverOptions withModifiers(Keyboard.Modifier... modifiers) { this.modifiers = new HashSet<>(Arrays.asList(modifiers)); diff --git a/playwright/src/main/java/com/microsoft/playwright/Position.java b/playwright/src/main/java/com/microsoft/playwright/Position.java new file mode 100644 index 00000000..99eee841 --- /dev/null +++ b/playwright/src/main/java/com/microsoft/playwright/Position.java @@ -0,0 +1,39 @@ +/* + * 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; + +public class Position { + public int x; + public int y; + + public Position() { + } + + public Position(int x, int y) { + this.x = x; + this.y = y; + } + + public Position withX(int x) { + this.x = x; + return this; + } + public Position withY(int y) { + this.y = y; + return this; + } +} diff --git a/playwright/src/test/java/com/microsoft/playwright/TestClick.java b/playwright/src/test/java/com/microsoft/playwright/TestClick.java index 32767336..48dbdc29 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestClick.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestClick.java @@ -311,7 +311,7 @@ public class TestClick extends TestBase { void shouldClickTheButtonWithPxBorderWithOffset() { page.navigate(server.PREFIX + "/input/button.html"); page.evalOnSelector("button", "button => button.style.borderWidth = '8px'"); - page.click("button", new Page.ClickOptions().setPosition().withX(20).withY(10).done()); + page.click("button", new Page.ClickOptions().withPosition(20, 10)); assertEquals(page.evaluate("result"), "Clicked"); // Safari reports border-relative offsetX/offsetY. assertEquals(isWebKit ? 20 + 8 : 20, page.evaluate("offsetX")); @@ -323,7 +323,7 @@ public class TestClick extends TestBase { page.navigate(server.PREFIX + "/input/button.html"); page.evalOnSelector("button", "button => button.style.borderWidth = '2em'"); page.evalOnSelector("button", "button => button.style.fontSize = '12px'"); - page.click("button", new Page.ClickOptions().setPosition().withX(20).withY(10).done()); + page.click("button", new Page.ClickOptions().withPosition(20, 10)); assertEquals("Clicked", page.evaluate("result")); // Safari reports border-relative offsetX/offsetY. assertEquals(isWebKit ? 12 * 2 + 20 : 20, page.evaluate("offsetX")); @@ -335,7 +335,7 @@ public class TestClick extends TestBase { page.navigate(server.PREFIX + "/input/button.html"); page.evalOnSelector("button", "button => button.style.borderWidth = '8px'"); page.evalOnSelector("button", "button => button.style.height = button.style.width = '2000px'"); - page.click("button", new Page.ClickOptions().setPosition().withX(1900).withY(1910).done()); + page.click("button", new Page.ClickOptions().withPosition(1900, 1910)); assertEquals("Clicked", page.evaluate("() => window['result']")); // Safari reports border-relative offsetX/offsetY. assertEquals(isWebKit ? 1900 + 8 : 1900, page.evaluate("offsetX")); @@ -356,7 +356,7 @@ public class TestClick extends TestBase { " button.style.width = '2000px';\n" + " button.style.borderWidth = '8px';\n" + "}"); - page.click("button", new Page.ClickOptions().setPosition().withX(1900).withY(1910).done()); + page.click("button", new Page.ClickOptions().withPosition(1900, 1910)); assertEquals("Clicked", page.evaluate("() => window['result']")); // Safari reports border-relative offsetX/offsetY. assertEquals(isWebKit ? 1900 + 8 : 1900, page.evaluate("offsetX")); @@ -375,7 +375,7 @@ public class TestClick extends TestBase { " button.style.borderWidth = '8px';\n" + " document.body.style.margin = '0';\n" + "}"); - page.click("button", new Page.ClickOptions().setPosition().withX(20).withY(10).done()); + page.click("button", new Page.ClickOptions().withPosition(20, 10)); assertEquals("Clicked", page.evaluate("result")); // 20;10 + 8px of border in each direction int expectedX = 28;