feat: viewport size
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<String, String> 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<Server.Request> 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<Page> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user