From 2ef70312e8785d7db74526669a64abc2f48cebc8 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Tue, 10 Oct 2023 15:15:10 +0530 Subject: [PATCH] [bidi][java] Add set viewport command --- .../bidi/browsingcontext/BrowsingContext.java | 27 ++++++++++++ .../browsingcontext/BrowsingContextTest.java | 44 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java b/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java index 200c60987f49a..abc90d5bfc035 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java @@ -288,6 +288,33 @@ public String captureElementScreenshot(String elementId, boolean scrollIntoView) })); } + public void setViewport(double width, double height) { + Require.positive("Viewport width", width); + Require.positive("Viewport height", height); + + this.bidi.send( + new Command<>( + "browsingContext.setViewport", + Map.of(CONTEXT, id, "viewport", Map.of("width", width, "height", height)))); + } + + public void setViewport(double width, double height, double devicePixelRatio) { + Require.positive("Viewport width", width); + Require.positive("Viewport height", height); + Require.positive("Device pixel ratio.", devicePixelRatio); + + this.bidi.send( + new Command<>( + "browsingContext.setViewport", + Map.of( + CONTEXT, + id, + "viewport", + Map.of("width", width, "height", height), + "devicePixelRatio", + devicePixelRatio))); + } + public void close() { // This might need more clean up actions once the behavior is defined. // Specially when last tab or window is closed. diff --git a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java index 0c56211c8fb90..c065eeb52e4d6 100644 --- a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java +++ b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java @@ -33,6 +33,7 @@ import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.Dimension; +import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Rectangle; import org.openqa.selenium.WebElement; import org.openqa.selenium.WindowType; @@ -418,6 +419,49 @@ void canScrollAndCaptureElementScreenshot() { assertThat(screenshot.length()).isPositive(); } + @Test + @NotYetImplemented(SAFARI) + @NotYetImplemented(IE) + void canSetViewport() { + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); + driver.get(appServer.whereIs("formPage.html")); + + browsingContext.setViewport(250, 300); + + List newViewportSize = + (List) + ((JavascriptExecutor) driver) + .executeScript("return [window.innerWidth, window.innerHeight];"); + + assertThat(newViewportSize.get(0)).isEqualTo(250); + assertThat(newViewportSize.get(1)).isEqualTo(300); + } + + @Test + @NotYetImplemented(SAFARI) + @NotYetImplemented(IE) + @NotYetImplemented(CHROME) + @NotYetImplemented(FIREFOX) + void canSetViewportWithDevicePixelRatio() { + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); + driver.get(appServer.whereIs("formPage.html")); + + browsingContext.setViewport(250, 300, 5); + + List newViewportSize = + (List) + ((JavascriptExecutor) driver) + .executeScript("return [window.innerWidth, window.innerHeight];"); + + assertThat(newViewportSize.get(0)).isEqualTo(250); + assertThat(newViewportSize.get(1)).isEqualTo(300); + + Long newDevicePixelRatio = + (Long) ((JavascriptExecutor) driver).executeScript("return window.devicePixelRatio"); + + assertThat(newDevicePixelRatio).isEqualTo(5); + } + private String alertPage() { return appServer.create( new Page()