Skip to content

Commit

Permalink
[bidi][java] Add set viewport command
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani authored Oct 10, 2023
1 parent 3dbb37c commit 2ef7031
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Long> newViewportSize =
(List<Long>)
((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<Long> newViewportSize =
(List<Long>)
((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()
Expand Down

0 comments on commit 2ef7031

Please sign in to comment.