From ab5cb63cc8d4d9d2344adc331acc84895a388ebc Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Fri, 2 Oct 2020 12:14:11 -0700 Subject: [PATCH] feat: viewport size --- .../microsoft/playwright/impl/PageImpl.java | 4 +- .../com/microsoft/playwright/TestPopup.java | 60 ++++++++++++++++--- .../java/com/microsoft/playwright/Utils.java | 30 ++++++++++ 3 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 lib/src/test/java/com/microsoft/playwright/Utils.java diff --git a/lib/src/main/java/com/microsoft/playwright/impl/PageImpl.java b/lib/src/main/java/com/microsoft/playwright/impl/PageImpl.java index 12133f81..edccd799 100644 --- a/lib/src/main/java/com/microsoft/playwright/impl/PageImpl.java +++ b/lib/src/main/java/com/microsoft/playwright/impl/PageImpl.java @@ -348,7 +348,9 @@ public class PageImpl extends ChannelOwner implements Page { @Override public void setViewportSize(ViewportSize viewportSize) { - + JsonObject params = new JsonObject(); + params.add("viewportSize", new Gson().toJsonTree(viewportSize).getAsJsonObject()); + sendMessage("setViewportSize", params); } @Override diff --git a/lib/src/test/java/com/microsoft/playwright/TestPopup.java b/lib/src/test/java/com/microsoft/playwright/TestPopup.java index c5266d60..09431b43 100644 --- a/lib/src/test/java/com/microsoft/playwright/TestPopup.java +++ b/lib/src/test/java/com/microsoft/playwright/TestPopup.java @@ -19,14 +19,12 @@ package com.microsoft.playwright; import org.junit.jupiter.api.*; import java.io.IOException; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import static com.microsoft.playwright.Page.LoadState.DOMCONTENTLOADED; +import static com.microsoft.playwright.Utils.mapOf; import static org.junit.jupiter.api.Assertions.*; public class TestPopup { @@ -111,9 +109,8 @@ public class TestPopup { @Test void should_inherit_extra_headers_from_browser_context() throws ExecutionException, InterruptedException { - Map headers = new HashMap<>(); - headers.put("foo", "bar"); - BrowserContext context = browser.newContext(new Browser.NewContextOptions().withExtraHTTPHeaders(headers)); + BrowserContext context = browser.newContext(new Browser.NewContextOptions() + .withExtraHTTPHeaders(mapOf("foo", "bar"))); Page page = context.newPage(); page.navigate(server.EMPTY_PAGE); Future requestPromise = server.waitForRequest("/dummy.html"); @@ -150,4 +147,53 @@ public class TestPopup { assertEquals("Woof-Woof", popup.get().title()); context.close(); } + + @Test + void should_inherit_touch_support_from_browser_context() { + BrowserContext context = browser.newContext(new Browser.NewContextOptions() + .setViewport().withWidth(400).withHeight(500).done() + .withHasTouch(true)); + Page page = context.newPage(); + page.navigate(server.EMPTY_PAGE); + Object hasTouch = page.evaluate("() => {\n" + + " const win = window.open('');\n" + + " return 'ontouchstart' in win;\n" + + "}"); + context.close(); + assertEquals(true, hasTouch); + } + + @Test + void should_inherit_viewport_size_from_browser_context() { + BrowserContext context = browser.newContext(new Browser.NewContextOptions() + .setViewport().withWidth(400).withHeight(500).done()); + Page page = context.newPage(); + page.navigate(server.EMPTY_PAGE); + Object size = page.evaluate("() => {\n" + + " const win = window.open('about:blank');\n" + + " return { width: win.innerWidth, height: win.innerHeight };\n" + + "}"); + context.close(); + assertEquals(mapOf("width", 400, "height", 500), size); + } + + @Test + void should_use_viewport_size_from_window_features() { + BrowserContext context = browser.newContext(new Browser.NewContextOptions() + .setViewport().withWidth(700).withHeight(700).done()); + Page page = context.newPage(); + page.navigate(server.EMPTY_PAGE); + Deferred popupEvent = page.waitForPopup(); + Object size = page.evaluate("() => {\n" + + " const win = window.open(window.location.href, 'Title', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=300,top=0,left=0');\n" + + " return { width: win.innerWidth, height: win.innerHeight };\n" + + "}"); + Page popup = popupEvent.get(); + popup.setViewportSize(new Page.ViewportSize().withWidth(500).withHeight(400)); + popup.waitForLoadState(); + Object resized = popup.evaluate("() => ({ width: window.innerWidth, height: window.innerHeight })"); + context.close(); + assertEquals(mapOf("width", 600, "height", 300), size); + assertEquals(mapOf("width", 500, "height", 400), resized); + } } diff --git a/lib/src/test/java/com/microsoft/playwright/Utils.java b/lib/src/test/java/com/microsoft/playwright/Utils.java new file mode 100644 index 00000000..2d61e906 --- /dev/null +++ b/lib/src/test/java/com/microsoft/playwright/Utils.java @@ -0,0 +1,30 @@ +/** + * 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; + +import java.util.HashMap; +import java.util.Map; + +class Utils { + static Map mapOf(Object... entries) { + Map result = new HashMap(); + for (int i = 0; i + 1 < entries.length; i += 2) { + result.put(entries[i], entries[i + 1]); + } + return result; + } +}