1
0
mirror of synced 2026-08-04 14:36:55 +00:00

test: add new test 'Test Browser Context Viewport' (#255)

This commit is contained in:
ephung01
2021-02-04 12:23:34 -05:00
committed by GitHub
parent 811ca89fd4
commit 37e5de729f
3 changed files with 127 additions and 7 deletions
@@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test;
import java.io.OutputStreamWriter;
import java.util.List;
import static com.microsoft.playwright.Utils.verifyViewport;
import static org.junit.jupiter.api.Assertions.*;
public class TestBrowserContextBasic extends TestBase {
@@ -93,13 +94,6 @@ public class TestBrowserContextBasic extends TestBase {
assertEquals(1, browser.contexts().size());
}
static void verifyViewport(Page page, int width, int height) {
assertEquals(width, page.viewportSize().width());
assertEquals(height, page.viewportSize().height());
assertEquals(width, page.evaluate("window.innerWidth"));
assertEquals(height, page.evaluate("window.innerHeight"));
}
@Test
void shouldPropagateDefaultViewportToThePage() {
BrowserContext context = browser.newContext(new Browser.NewContextOptions().withViewport(456, 789));
@@ -0,0 +1,119 @@
package com.microsoft.playwright;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static com.microsoft.playwright.Utils.verifyViewport;
import static org.junit.jupiter.api.Assertions.*;
public class TestBrowserContextViewport extends TestBase {
@Test
void shouldGetTheProperDefaultViewPortSize() {
verifyViewport(page, 1280, 720);
}
@Test
void shouldSetTheProperViewportSize() {
verifyViewport(page, 1280, 720);
page.setViewportSize(123, 456);
verifyViewport(page,123, 456);
}
@Test
void shouldReturnCorrectOuterWidthAndOuterHeight() {
Map<String, Integer> size = (Map<String, Integer>) page.evaluate("() => {\n" +
" return {\n" +
" innerWidth: window.innerWidth,\n" +
" innerHeight: window.innerHeight,\n" +
" outerWidth: window.outerWidth,\n" +
" outerHeight: window.outerHeight,\n" +
" };\n" +
"}");
assertEquals(1280, size.get("innerWidth"));
assertEquals(720, size.get("innerHeight"));
assertTrue(size.get("outerWidth") >= size.get("innerWidth"));
assertTrue(size.get("outerHeight") >= size.get("innerHeight"));
}
@Test
void shouldEmulateDeviceWidth() {
verifyViewport(page, 1280, 720);
page.setViewportSize(200, 200);
assertEquals(200, page.evaluate("() => window.screen.width"));
assertEquals(true, page.evaluate("() => matchMedia('(min-device-width: 100px)').matches"));
assertEquals(false, page.evaluate("() => matchMedia('(min-device-width: 300px)').matches"));
assertEquals(false, page.evaluate("() => matchMedia('(max-device-width: 100px)').matches"));
assertEquals(true, page.evaluate("() => matchMedia('(max-device-width: 300px)').matches"));
assertEquals(false, page.evaluate("() => matchMedia('(device-width: 500px)').matches"));
assertEquals(true, page.evaluate("() => matchMedia('(device-width: 200px)').matches"));
page.setViewportSize(500, 500);
assertEquals(500, page.evaluate("() => window.screen.width"));
assertEquals(true, page.evaluate("() => matchMedia('(min-device-width: 400px)').matches"));
assertEquals(false, page.evaluate("() => matchMedia('(min-device-width: 600px)').matches"));
assertEquals(false, page.evaluate("() => matchMedia('(max-device-width: 400px)').matches"));
assertEquals(true, page.evaluate("() => matchMedia('(max-device-width: 600px)').matches"));
assertEquals(false, page.evaluate("() => matchMedia('(device-width: 200px)').matches"));
assertEquals(true, page.evaluate("() => matchMedia('(device-width: 500px)').matches"));
}
@Test
void shouldEmulateDeviceHeight() {
verifyViewport(page, 1280, 720);
page.setViewportSize(200, 200);
assertEquals(200, page.evaluate("() => window.screen.height"));
assertEquals(true, page.evaluate("() => matchMedia('(min-device-height: 100px)').matches"));
assertEquals(false, page.evaluate("() => matchMedia('(min-device-height: 300px)').matches"));
assertEquals(false, page.evaluate("() => matchMedia('(max-device-height: 100px)').matches"));
assertEquals(true, page.evaluate("() => matchMedia('(max-device-height: 300px)').matches"));
assertEquals(false, page.evaluate("() => matchMedia('(device-height: 500px)').matches"));
assertEquals(true, page.evaluate("() => matchMedia('(device-height: 200px)').matches"));
page.setViewportSize(500, 500);
assertEquals(500, page.evaluate("() => window.screen.height"));
assertEquals(true, page.evaluate("() => matchMedia('(min-device-height: 400px)').matches"));
assertEquals(false, page.evaluate("() => matchMedia('(min-device-height: 600px)').matches"));
assertEquals(false, page.evaluate("() => matchMedia('(max-device-height: 400px)').matches"));
assertEquals(true, page.evaluate("() => matchMedia('(max-device-height: 600px)').matches"));
assertEquals(false, page.evaluate("() => matchMedia('(device-height: 200px)').matches"));
assertEquals(true, page.evaluate("() => matchMedia('(device-height: 500px)').matches"));
}
@Test
void shouldEmulateAvailWidthAndAvailHeight() {
page.setViewportSize(500, 600);
assertEquals(500, page.evaluate("() => window.screen.availWidth"));
assertEquals(600, page.evaluate("() => window.screen.availHeight"));
}
@Test
void shouldNotHaveTouchByDefault() {
page.navigate(server.PREFIX + "/mobile.html");
assertEquals(false, page.evaluate("() => 'ontouchstart' in window"));
page.navigate(server.PREFIX + "/detect-touch.html");
assertEquals("NO", page.evaluate("() => document.body.textContent.trim()"));
}
@Test
void shouldSupportTouchWithNullViewport() {
Browser.NewContextOptions options = new Browser.NewContextOptions().withHasTouch(true);
options.viewport = null;
BrowserContext context = browser.newContext(options);
Page page = context.newPage();
page.navigate(server.PREFIX + "/mobile.html");
assertEquals(true, page.evaluate("() => 'ontouchstart' in window"));
context.close();
}
// TODO Wait for special constant implementation
// @Test
// void shouldReportNullViewPortSizeWhenGivenNullViewport() {
// Browser.NewContextOptions options = new Browser.NewContextOptions();
// options.viewport = null;
// BrowserContext context = browser.newContext(options);
// Page page = context.newPage();
// assertNull(page.viewportSize());
// context.close();
// }
}
@@ -122,4 +122,11 @@ class Utils {
throw new IllegalArgumentException("Unknown browser: " + browserName);
}
}
static void verifyViewport(Page page, int width, int height) {
assertEquals(width, page.viewportSize().width());
assertEquals(height, page.viewportSize().height());
assertEquals(width, page.evaluate("window.innerWidth"));
assertEquals(height, page.evaluate("window.innerHeight"));
}
}