From 37e5de729fcb37a7adda61ac6128c7661ae04067 Mon Sep 17 00:00:00 2001 From: ephung01 Date: Thu, 4 Feb 2021 12:23:34 -0500 Subject: [PATCH] test: add new test 'Test Browser Context Viewport' (#255) --- .../playwright/TestBrowserContextBasic.java | 8 +- .../TestBrowserContextViewport.java | 119 ++++++++++++++++++ .../java/com/microsoft/playwright/Utils.java | 7 ++ 3 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 playwright/src/test/java/com/microsoft/playwright/TestBrowserContextViewport.java diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextBasic.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextBasic.java index 882f138c..283a6be2 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextBasic.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextBasic.java @@ -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)); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextViewport.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextViewport.java new file mode 100644 index 00000000..a7329189 --- /dev/null +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextViewport.java @@ -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 size = (Map) 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(); +// } + +} diff --git a/playwright/src/test/java/com/microsoft/playwright/Utils.java b/playwright/src/test/java/com/microsoft/playwright/Utils.java index b8f91e0c..e9ae6180 100644 --- a/playwright/src/test/java/com/microsoft/playwright/Utils.java +++ b/playwright/src/test/java/com/microsoft/playwright/Utils.java @@ -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")); + } }