From fcc1d8672a7643e8e424dcdb4f09e155efa50953 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 9 Feb 2021 17:58:55 -0800 Subject: [PATCH] fix: take required options as constructor params (#274) --- .../microsoft/playwright/BrowserContext.java | 6 +-- .../microsoft/playwright/options/Clip.java | 18 -------- .../playwright/options/HttpCredentials.java | 10 ---- .../microsoft/playwright/options/Margin.java | 8 ---- .../playwright/options/Position.java | 10 ---- .../microsoft/playwright/options/Proxy.java | 11 +---- .../playwright/options/RecordHar.java | 9 +--- .../playwright/options/RecordVideo.java | 9 +--- .../microsoft/playwright/options/Size.java | 10 ---- .../TestBrowserContextAddCookies.java | 46 ++++++++----------- .../TestBrowserContextClearCookies.java | 6 +-- .../playwright/TestBrowserContextCookies.java | 6 +-- .../playwright/TestBrowserContextProxy.java | 32 ++++++------- .../com/microsoft/playwright/TestHar.java | 8 ++-- .../microsoft/playwright/TestPageRoute.java | 4 +- .../playwright/TestPageScreenshot.java | 2 +- .../microsoft/playwright/TestScreencast.java | 2 +- .../playwright/tools/ApiGenerator.java | 30 ++++++------ 18 files changed, 70 insertions(+), 157 deletions(-) diff --git a/playwright/src/main/java/com/microsoft/playwright/BrowserContext.java b/playwright/src/main/java/com/microsoft/playwright/BrowserContext.java index e42aa695..f55fd057 100644 --- a/playwright/src/main/java/com/microsoft/playwright/BrowserContext.java +++ b/playwright/src/main/java/com/microsoft/playwright/BrowserContext.java @@ -74,13 +74,9 @@ public interface BrowserContext extends AutoCloseable { */ public SameSiteAttribute sameSite; - public AddCookie withName(String name) { + public AddCookie(String name, String value) { this.name = name; - return this; - } - public AddCookie withValue(String value) { this.value = value; - return this; } public AddCookie withUrl(String url) { this.url = url; diff --git a/playwright/src/main/java/com/microsoft/playwright/options/Clip.java b/playwright/src/main/java/com/microsoft/playwright/options/Clip.java index baaeb5cd..0b07dc37 100644 --- a/playwright/src/main/java/com/microsoft/playwright/options/Clip.java +++ b/playwright/src/main/java/com/microsoft/playwright/options/Clip.java @@ -40,22 +40,4 @@ public class Clip { this.width = width; this.height = height; } - public Clip() { - } - public Clip withX(double x) { - this.x = x; - return this; - } - public Clip withY(double y) { - this.y = y; - return this; - } - public Clip withWidth(double width) { - this.width = width; - return this; - } - public Clip withHeight(double height) { - this.height = height; - return this; - } } \ No newline at end of file diff --git a/playwright/src/main/java/com/microsoft/playwright/options/HttpCredentials.java b/playwright/src/main/java/com/microsoft/playwright/options/HttpCredentials.java index c85a1aa7..98ce1335 100644 --- a/playwright/src/main/java/com/microsoft/playwright/options/HttpCredentials.java +++ b/playwright/src/main/java/com/microsoft/playwright/options/HttpCredentials.java @@ -24,14 +24,4 @@ public class HttpCredentials { this.username = username; this.password = password; } - public HttpCredentials() { - } - public HttpCredentials withUsername(String username) { - this.username = username; - return this; - } - public HttpCredentials withPassword(String password) { - this.password = password; - return this; - } } \ No newline at end of file diff --git a/playwright/src/main/java/com/microsoft/playwright/options/Margin.java b/playwright/src/main/java/com/microsoft/playwright/options/Margin.java index e3e133fa..af1e97ae 100644 --- a/playwright/src/main/java/com/microsoft/playwright/options/Margin.java +++ b/playwright/src/main/java/com/microsoft/playwright/options/Margin.java @@ -34,14 +34,6 @@ public class Margin { */ public String left; - public Margin(String top, String right, String bottom, String left) { - this.top = top; - this.right = right; - this.bottom = bottom; - this.left = left; - } - public Margin() { - } public Margin withTop(String top) { this.top = top; return this; diff --git a/playwright/src/main/java/com/microsoft/playwright/options/Position.java b/playwright/src/main/java/com/microsoft/playwright/options/Position.java index 2dff8c61..f56d6ec7 100644 --- a/playwright/src/main/java/com/microsoft/playwright/options/Position.java +++ b/playwright/src/main/java/com/microsoft/playwright/options/Position.java @@ -24,14 +24,4 @@ public class Position { this.x = x; this.y = y; } - public Position() { - } - public Position withX(double x) { - this.x = x; - return this; - } - public Position withY(double y) { - this.y = y; - return this; - } } \ No newline at end of file diff --git a/playwright/src/main/java/com/microsoft/playwright/options/Proxy.java b/playwright/src/main/java/com/microsoft/playwright/options/Proxy.java index d7c9e0c1..a4c98437 100644 --- a/playwright/src/main/java/com/microsoft/playwright/options/Proxy.java +++ b/playwright/src/main/java/com/microsoft/playwright/options/Proxy.java @@ -35,17 +35,8 @@ public class Proxy { */ public String password; - public Proxy(String server, String bypass, String username, String password) { + public Proxy(String server) { this.server = server; - this.bypass = bypass; - this.username = username; - this.password = password; - } - public Proxy() { - } - public Proxy withServer(String server) { - this.server = server; - return this; } public Proxy withBypass(String bypass) { this.bypass = bypass; diff --git a/playwright/src/main/java/com/microsoft/playwright/options/RecordHar.java b/playwright/src/main/java/com/microsoft/playwright/options/RecordHar.java index 47a4f37d..9fb9a30e 100644 --- a/playwright/src/main/java/com/microsoft/playwright/options/RecordHar.java +++ b/playwright/src/main/java/com/microsoft/playwright/options/RecordHar.java @@ -27,18 +27,11 @@ public class RecordHar { */ public Path path; - public RecordHar(Boolean omitContent, Path path) { - this.omitContent = omitContent; + public RecordHar(Path path) { this.path = path; } - public RecordHar() { - } public RecordHar withOmitContent(boolean omitContent) { this.omitContent = omitContent; return this; } - public RecordHar withPath(Path path) { - this.path = path; - return this; - } } \ No newline at end of file diff --git a/playwright/src/main/java/com/microsoft/playwright/options/RecordVideo.java b/playwright/src/main/java/com/microsoft/playwright/options/RecordVideo.java index 336ecf80..517fec9f 100644 --- a/playwright/src/main/java/com/microsoft/playwright/options/RecordVideo.java +++ b/playwright/src/main/java/com/microsoft/playwright/options/RecordVideo.java @@ -29,15 +29,8 @@ public class RecordVideo { */ public Size size; - public RecordVideo(Path dir, Size size) { + public RecordVideo(Path dir) { this.dir = dir; - this.size = size; - } - public RecordVideo() { - } - public RecordVideo withDir(Path dir) { - this.dir = dir; - return this; } public RecordVideo withSize(Size size) { this.size = size; diff --git a/playwright/src/main/java/com/microsoft/playwright/options/Size.java b/playwright/src/main/java/com/microsoft/playwright/options/Size.java index 78cfce2e..6955e168 100644 --- a/playwright/src/main/java/com/microsoft/playwright/options/Size.java +++ b/playwright/src/main/java/com/microsoft/playwright/options/Size.java @@ -30,14 +30,4 @@ public class Size { this.width = width; this.height = height; } - public Size() { - } - public Size withWidth(int width) { - this.width = width; - return this; - } - public Size withHeight(int height) { - this.height = height; - return this; - } } \ No newline at end of file diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextAddCookies.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextAddCookies.java index 5a707a96..22fdc3a2 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextAddCookies.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextAddCookies.java @@ -35,7 +35,7 @@ public class TestBrowserContextAddCookies extends TestBase { void shouldWork() { page.navigate(server.EMPTY_PAGE); context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("password").withValue("123456"))); + new BrowserContext.AddCookie("password", "123456").withUrl(server.EMPTY_PAGE))); assertEquals("password=123456", page.evaluate("document.cookie")); } @@ -52,9 +52,7 @@ public class TestBrowserContextAddCookies extends TestBase { List cookies = context.cookies(); context.clearCookies(); assertEquals(emptyList(), context.cookies()); - context.addCookies(asList(new BrowserContext.AddCookie() - .withName(cookies.get(0).name()) - .withValue(cookies.get(0).value()) + context.addCookies(asList(new BrowserContext.AddCookie(cookies.get(0).name(), cookies.get(0).value()) .withDomain(cookies.get(0).domain()) .withPath(cookies.get(0).path()) .withExpires(cookies.get(0).expires()) @@ -66,7 +64,7 @@ public class TestBrowserContextAddCookies extends TestBase { void shouldSendCookieHeader() throws ExecutionException, InterruptedException { Future request = server.futureRequest("/empty.html"); context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("cookie").withValue("value"))); + new BrowserContext.AddCookie("cookie", "value").withUrl(server.EMPTY_PAGE))); Page page = context.newPage(); page.navigate(server.EMPTY_PAGE); List cookies = request.get().headers.get("cookie"); @@ -77,9 +75,9 @@ public class TestBrowserContextAddCookies extends TestBase { void shouldIsolateCookiesInBrowserContexts() { BrowserContext anotherContext = browser.newContext(); context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("isolatecookie").withValue("page1value"))); + new BrowserContext.AddCookie("isolatecookie", "page1value").withUrl(server.EMPTY_PAGE))); anotherContext.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("isolatecookie").withValue("page2value"))); + new BrowserContext.AddCookie("isolatecookie", "page2value").withUrl(server.EMPTY_PAGE))); List cookies1 = context.cookies(); List cookies2 = anotherContext.cookies(); assertEquals(1, cookies1.size()); @@ -147,7 +145,7 @@ public class TestBrowserContextAddCookies extends TestBase { @Test void shouldIsolateSendCookieHeader() throws ExecutionException, InterruptedException { context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("sendcookie").withValue("value"))); + new BrowserContext.AddCookie("sendcookie", "value").withUrl(server.EMPTY_PAGE))); { Page page = context.newPage(); Future request = server.futureRequest("/empty.html"); @@ -171,10 +169,8 @@ public class TestBrowserContextAddCookies extends TestBase { // TODO: const browser1 = browserType.launch(browserOptions); Browser browser1 = browserType.launch(); BrowserContext context1 = browser1.newContext(); - context1.addCookies(asList(new BrowserContext.AddCookie() + context1.addCookies(asList(new BrowserContext.AddCookie("cookie-in-context-1", "value") .withUrl(server.EMPTY_PAGE) - .withName("cookie-in-context-1") - .withValue("value") .withExpires(Instant.now().getEpochSecond() + + 10000))); browser1.close(); @@ -190,8 +186,8 @@ public class TestBrowserContextAddCookies extends TestBase { void shouldSetMultipleCookies() { page.navigate(server.EMPTY_PAGE); context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("multiple-1").withValue("123456"), - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("multiple-2").withValue("bar") + new BrowserContext.AddCookie("multiple-1", "123456").withUrl(server.EMPTY_PAGE), + new BrowserContext.AddCookie("multiple-2", "bar").withUrl(server.EMPTY_PAGE) )); assertEquals(asList("multiple-1=123456", "multiple-2=bar"), page.evaluate("() => {\n" + " const cookies = document.cookie.split(';');\n" + @@ -202,7 +198,7 @@ public class TestBrowserContextAddCookies extends TestBase { @Test void shouldHaveExpiresSetTo1ForSessionCookies() { context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("expires").withValue("123456"))); + new BrowserContext.AddCookie("expires", "123456").withUrl(server.EMPTY_PAGE))); List cookies = context.cookies(); assertEquals(-1, cookies.get(0).expires()); } @@ -210,7 +206,7 @@ public class TestBrowserContextAddCookies extends TestBase { @Test void shouldSetCookieWithReasonableDefaults() { context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("defaults").withValue("123456"))); + new BrowserContext.AddCookie("defaults", "123456").withUrl(server.EMPTY_PAGE))); List cookies = context.cookies(); assertJsonEquals("[{\n" + " name: 'defaults',\n" + @@ -227,11 +223,9 @@ public class TestBrowserContextAddCookies extends TestBase { @Test void shouldSetACookieWithAPath() { page.navigate(server.PREFIX + "/grid.html"); - context.addCookies(asList(new BrowserContext.AddCookie() + context.addCookies(asList(new BrowserContext.AddCookie("gridcookie", "GRID") .withDomain("localhost") - .withPath("/grid.html") - .withName("gridcookie") - .withValue("GRID"))); + .withPath("/grid.html"))); List cookies = context.cookies(); assertJsonEquals("[{\n" + " name: 'gridcookie',\n" + @@ -254,8 +248,8 @@ public class TestBrowserContextAddCookies extends TestBase { void shouldNotSetACookieWithBlankPageURL() { try { context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("example-cookie").withValue("best"), - new BrowserContext.AddCookie().withUrl("about:blank").withName("example-cookie-blank").withValue("best") + new BrowserContext.AddCookie("example-cookie", "best").withUrl(server.EMPTY_PAGE), + new BrowserContext.AddCookie("example-cookie-blank", "best").withUrl("about:blank") )); fail("did not throw"); } catch (PlaywrightException e) { @@ -267,7 +261,7 @@ public class TestBrowserContextAddCookies extends TestBase { void shouldNotSetACookieOnADataURLPage() { try { context.addCookies(asList( - new BrowserContext.AddCookie().withUrl("data:,Hello%2C%20World!").withName("example-cookie").withValue("best") + new BrowserContext.AddCookie("example-cookie", "best").withUrl("data:,Hello%2C%20World!") )); fail("did not throw"); } catch (PlaywrightException e) { @@ -280,7 +274,7 @@ public class TestBrowserContextAddCookies extends TestBase { page.navigate(server.EMPTY_PAGE); String SECURE_URL = "https://example.com"; context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(SECURE_URL).withName("foo").withValue("bar") + new BrowserContext.AddCookie("foo", "bar").withUrl(SECURE_URL) )); List cookies = context.cookies(SECURE_URL); assertEquals(1, cookies.size()); @@ -292,7 +286,7 @@ public class TestBrowserContextAddCookies extends TestBase { page.navigate(server.EMPTY_PAGE); String HTTP_URL = "http://example.com"; context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(HTTP_URL).withName("foo").withValue("bar") + new BrowserContext.AddCookie("foo", "bar").withUrl(HTTP_URL) )); List cookies = context.cookies(HTTP_URL); assertEquals(1, cookies.size()); @@ -303,7 +297,7 @@ public class TestBrowserContextAddCookies extends TestBase { void shouldSetACookieOnADifferentDomain() { page.navigate(server.EMPTY_PAGE); context.addCookies(asList( - new BrowserContext.AddCookie().withUrl("https://www.example.com").withName("example-cookie").withValue("best") + new BrowserContext.AddCookie("example-cookie", "best").withUrl("https://www.example.com") )); assertEquals("", page.evaluate("document.cookie")); assertJsonEquals("[{\n" + @@ -322,7 +316,7 @@ public class TestBrowserContextAddCookies extends TestBase { void shouldSetCookiesForAFrame() { page.navigate(server.EMPTY_PAGE); context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.PREFIX).withName("frame-cookie").withValue("value") + new BrowserContext.AddCookie("frame-cookie", "value").withUrl(server.PREFIX) )); page.evaluate("src => {\n" + " let fulfill;\n" + diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextClearCookies.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextClearCookies.java index 340df05d..d015fb85 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextClearCookies.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextClearCookies.java @@ -27,7 +27,7 @@ public class TestBrowserContextClearCookies extends TestBase { void shouldClearCookies() { page.navigate(server.EMPTY_PAGE); context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("cookie1").withValue("1"))); + new BrowserContext.AddCookie("cookie1", "1").withUrl(server.EMPTY_PAGE))); assertEquals("cookie1=1", page.evaluate("document.cookie")); context.clearCookies(); assertEquals(emptyList(), context.cookies()); @@ -39,9 +39,9 @@ public class TestBrowserContextClearCookies extends TestBase { void shouldIsolateCookiesWhenClearing() { BrowserContext anotherContext = browser.newContext(); context.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("page1cookie").withValue("page1value"))); + new BrowserContext.AddCookie("page1cookie", "page1value").withUrl(server.EMPTY_PAGE))); anotherContext.addCookies(asList( - new BrowserContext.AddCookie().withUrl(server.EMPTY_PAGE).withName("page2cookie").withValue("page2value"))); + new BrowserContext.AddCookie("page2cookie", "page2value").withUrl(server.EMPTY_PAGE))); assertEquals(1, (context.cookies()).size()); assertEquals(1, (anotherContext.cookies()).size()); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextCookies.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextCookies.java index 7fad83a0..9c9b0fa3 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextCookies.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextCookies.java @@ -157,9 +157,9 @@ public class TestBrowserContextCookies extends TestBase { @Test void shouldGetCookiesFromMultipleUrls() { context.addCookies(asList( - new BrowserContext.AddCookie().withUrl("https://foo.com").withName("doggo").withValue("woofs"), - new BrowserContext.AddCookie().withUrl("https://bar.com").withName("catto").withValue("purrs"), - new BrowserContext.AddCookie().withUrl("https://baz.com").withName("birdo").withValue("tweets"))); + new BrowserContext.AddCookie("doggo", "woofs").withUrl("https://foo.com"), + new BrowserContext.AddCookie("catto", "purrs").withUrl("https://bar.com"), + new BrowserContext.AddCookie("birdo", "tweets").withUrl("https://baz.com"))); List cookies = context.cookies(asList("https://foo.com", "https://baz.com")); cookies.sort(Comparator.comparing(BrowserContext.Cookie::name)); assertJsonEquals("[{\n" + diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextProxy.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextProxy.java index ef5105d0..bdf53d9e 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextProxy.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextProxy.java @@ -33,7 +33,7 @@ public class TestBrowserContextProxy extends TestBase { // Hide base class method to provide extra option. static void launchBrowser() { BrowserType.LaunchOptions options = createLaunchOptions(); - options.withProxy(new Proxy().withServer("per-context")); + options.withProxy(new Proxy("per-context")); launchBrowser(options); } @@ -41,7 +41,7 @@ public class TestBrowserContextProxy extends TestBase { void shouldThrowForMissingGlobalProxy() { Browser browser = browserType.launch(createLaunchOptions()); try { - browser.newContext(new Browser.NewContextOptions().withProxy(new Proxy().withServer("localhost:" + server.PORT))); + browser.newContext(new Browser.NewContextOptions().withProxy(new Proxy("localhost:" + server.PORT))); fail("did not throw"); } catch (PlaywrightException e) { assertTrue(e.getMessage().contains("Browser needs to be launched with the global proxy")); @@ -62,8 +62,8 @@ public class TestBrowserContextProxy extends TestBase { writer.write("Served by the proxy"); } }); - BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy(new Proxy() - .withServer("localhost:" + server.PORT))); + BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy( + new Proxy("localhost:" + server.PORT))); Page page = context.newPage(); page.navigate("http://non-existent.com/target.html"); assertEquals("Served by the proxy", page.title()); @@ -78,8 +78,8 @@ public class TestBrowserContextProxy extends TestBase { writer.write("Served by the proxy"); } }); - BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy(new Proxy() - .withServer("localhost:" + server.PORT))); + BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy( + new Proxy("localhost:" + server.PORT))); Page page = context.newPage(); page.navigate("http://non-existent.com/target.html"); page.navigate("http://non-existent-2.com/target.html"); @@ -95,8 +95,8 @@ public class TestBrowserContextProxy extends TestBase { writer.write("Served by the proxy"); } }); - BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy(new Proxy() - .withServer("localhost:" + server.PORT))); + BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy( + new Proxy("localhost:" + server.PORT))); Page page = context.newPage(); page.navigate("http://non-existent.com/target.html"); @@ -117,8 +117,8 @@ public class TestBrowserContextProxy extends TestBase { writer.write("Served by the proxy"); } }); - BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy(new Proxy() - .withServer("127.0.0.1:" + server.PORT))); + BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy( + new Proxy("127.0.0.1:" + server.PORT))); Page page = context.newPage(); page.navigate("http://non-existent.com/target.html"); @@ -141,8 +141,8 @@ public class TestBrowserContextProxy extends TestBase { writer.write("" + auth.get(0) + ""); } }); - BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy(new Proxy() - .withServer("localhost:" + server.PORT) + BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy( + new Proxy("localhost:" + server.PORT) .withUsername("user") .withPassword("secret"))); Page page = context.newPage(); @@ -164,8 +164,8 @@ public class TestBrowserContextProxy extends TestBase { writer.write("Served by the proxy"); } }); - BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy(new Proxy() - .withServer("127.0.0.1:" + server.PORT) + BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy( + new Proxy("127.0.0.1:" + server.PORT) // FYI: using long and weird domain names to avoid ATT DNS hijacking // that resolves everything to some weird search results page. // @@ -210,8 +210,8 @@ public class TestBrowserContextProxy extends TestBase { @Test void doesLaunchWithoutAPort() { - BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy(new Proxy() - .withServer("http://localhost"))); + BrowserContext context = browser.newContext(new Browser.NewContextOptions().withProxy( + new Proxy("http://localhost"))); context.close(); } } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestHar.java b/playwright/src/test/java/com/microsoft/playwright/TestHar.java index f3ed4c46..45d0ccc5 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestHar.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestHar.java @@ -43,8 +43,8 @@ public class TestHar extends TestBase { PageWithHar() throws IOException { harFile = Files.createTempFile("test-", ".har"); - context = browser.newContext(new Browser.NewContextOptions().withRecordHar(new RecordHar() - .withPath(harFile)).withIgnoreHTTPSErrors(true)); + context = browser.newContext(new Browser.NewContextOptions().withRecordHar( + new RecordHar(harFile)).withIgnoreHTTPSErrors(true)); page = context.newPage(); } @@ -74,7 +74,7 @@ public class TestHar extends TestBase { @Test void shouldThrowWithoutPath() { try { - browser.newContext(new Browser.NewContextOptions().withRecordHar(new RecordHar())); + browser.newContext(new Browser.NewContextOptions().withRecordHar(new RecordHar(null))); fail("did not throw"); } catch (PlaywrightException e) { assertTrue(e.getMessage().contains("recordHar.path: expected string, got undefined")); @@ -119,7 +119,7 @@ public class TestHar extends TestBase { Path userDataDir = Files.createTempDirectory("user-data-dir-"); BrowserContext context = browserType.launchPersistentContext(userDataDir, new BrowserType.LaunchPersistentContextOptions() - .withRecordHar(new RecordHar().withPath(harPath)).withIgnoreHTTPSErrors(true)); + .withRecordHar(new RecordHar(harPath)).withIgnoreHTTPSErrors(true)); Page page = context.pages().get(0); page.navigate("data:text/html,Hello"); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java index b7fd8179..2cdb9ef1 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java @@ -148,8 +148,8 @@ public class TestPageRoute extends TestBase { void shouldProperlyReturnNavigationResponseWhenURLHasCookies() { // Setup cookie. page.navigate(server.EMPTY_PAGE); - context.addCookies(asList(new BrowserContext.AddCookie() - .withUrl(server.EMPTY_PAGE).withName("foo").withValue("bar"))); + context.addCookies(asList(new BrowserContext.AddCookie("foo", "bar") + .withUrl(server.EMPTY_PAGE))); // Setup request interception. page.route("**/*", route -> route.resume()); Response response = page.reload(); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageScreenshot.java b/playwright/src/test/java/com/microsoft/playwright/TestPageScreenshot.java index 57e07b66..1b71f794 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageScreenshot.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageScreenshot.java @@ -44,7 +44,7 @@ public class TestPageScreenshot extends TestBase { page.setViewportSize(500, 500); page.navigate(server.PREFIX + "/grid.html"); byte[] screenshot = page.screenshot(new Page.ScreenshotOptions() - .withClip(new Clip().withX(50).withY(100).withWidth(150).withHeight(100))); + .withClip(new Clip(50, 100, 150, 100))); BufferedImage image = ImageIO.read(new ByteArrayInputStream(screenshot)); assertEquals(150, image.getWidth()); assertEquals(100, image.getHeight()); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestScreencast.java b/playwright/src/test/java/com/microsoft/playwright/TestScreencast.java index 6777590b..a6d09b3c 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestScreencast.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestScreencast.java @@ -30,7 +30,7 @@ public class TestScreencast extends TestBase { @Test void shouldExposeVideoPath(@TempDir Path videosDir) { BrowserContext context = browser.newContext(new Browser.NewContextOptions() - .withRecordVideo(new RecordVideo().withDir(videosDir).withSize(new Size(320, 240))) + .withRecordVideo(new RecordVideo(videosDir).withSize(new Size(320, 240))) .withViewport(320, 240)); Page page = context.newPage(); page.evaluate("() => document.body.style.backgroundColor = 'red'"); diff --git a/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java b/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java index ed1ea953..f8e628a9 100644 --- a/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java +++ b/tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java @@ -24,9 +24,11 @@ import com.google.gson.JsonObject; import java.io.*; import java.nio.file.FileSystems; import java.util.*; +import java.util.stream.Collectors; import static java.util.Arrays.asList; import static java.util.Collections.reverse; +import static java.util.stream.Collectors.toList; abstract class Element { final String jsonName; @@ -764,6 +766,11 @@ class Field extends Element { this.type = new TypeRef(this, jsonElement.getAsJsonObject().get("type")); } + boolean isRequired() { + return jsonElement.getAsJsonObject().has("required") && + jsonElement.getAsJsonObject().get("required").getAsBoolean(); + } + void writeTo(List output, String offset, String access) { writeJavadoc(output, offset, comment()); if (asList("Frame.waitForNavigation.options.url", @@ -1156,9 +1163,7 @@ class NestedClass extends TypeDefinition { f.writeGetter(output, bodyOffset); } } else { - if (parent.parent instanceof Field) { - writeConstructor(output, bodyOffset); - } + writeConstructor(output, bodyOffset); writeBuilderMethods(output, bodyOffset); if (asList("Browser.newContext.options", "Browser.newPage.options", @@ -1170,24 +1175,21 @@ class NestedClass extends TypeDefinition { } private void writeBuilderMethods(List output, String bodyOffset) { - if (parent.typeScope() instanceof NestedClass) { - output.add(bodyOffset + "public " + name + "() {"); - output.add(bodyOffset + "}"); - } for (Field f : fields) { - f.writeBuilderMethod(output, bodyOffset, name); + if (!f.isRequired()) { + f.writeBuilderMethod(output, bodyOffset, name); + } } } private void writeConstructor(List output, String bodyOffset) { - List args = new ArrayList<>(); - for (Field f : fields) { - args.add(f.type.toJava() + " " + f.name); + List requiredFields = fields.stream().filter(f -> f.isRequired()).collect(toList()); + if (requiredFields.isEmpty()) { + return; } + List args = requiredFields.stream().map(f -> f.type.toJava() + " " + f.name).collect(toList()); output.add(bodyOffset + "public " + name + "(" + String.join(", ", args) + ") {"); - for (Field f : fields) { - output.add(bodyOffset + " this." + f.name + " = " + f.name + ";"); - } + requiredFields.forEach(f -> output.add(bodyOffset + " this." + f.name + " = " + f.name + ";")); output.add(bodyOffset + "}"); }