Skip to content

Commit cbf6ce6

Browse files
committed
[java][bidi] Add browsing context's capture box screenshot command
1 parent 99c8f99 commit cbf6ce6

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,24 @@ public String captureScreenshot() {
226226
}));
227227
}
228228

229+
public String captureBoxScreenshot(double x, double y, double width, double height) {
230+
return this.bidi.send(
231+
new Command<>(
232+
"browsingContext.captureScreenshot",
233+
ImmutableMap.of(CONTEXT, id,
234+
"clip", ImmutableMap.of(
235+
"type", "viewport",
236+
"x", x,
237+
"y", y,
238+
"width", width,
239+
"height", height
240+
)),
241+
jsonInput -> {
242+
Map<String, Object> result = jsonInput.read(Map.class);
243+
return (String) result.get("data");
244+
}));
245+
}
246+
229247
public void close() {
230248
// This might need more clean up actions once the behavior is defined.
231249
// Specially when last tab or window is closed.

java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
2121
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
22+
import static org.junit.jupiter.api.Assertions.fail;
2223
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
2324
import static org.openqa.selenium.testing.Safely.safelyCall;
2425
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
@@ -28,10 +29,13 @@
2829
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
2930

3031
import java.util.List;
32+
3133
import org.junit.jupiter.api.AfterEach;
3234
import org.junit.jupiter.api.BeforeEach;
3335
import org.junit.jupiter.api.Test;
3436
import org.openqa.selenium.By;
37+
import org.openqa.selenium.Rectangle;
38+
import org.openqa.selenium.WebElement;
3539
import org.openqa.selenium.WindowType;
3640
import org.openqa.selenium.bidi.BiDiException;
3741
import org.openqa.selenium.environment.webserver.AppServer;
@@ -74,7 +78,7 @@ void canCreateAWindow() {
7478
@NotYetImplemented(EDGE)
7579
void canCreateAWindowWithAReferenceContext() {
7680
BrowsingContext browsingContext =
77-
new BrowsingContext(driver, WindowType.WINDOW, driver.getWindowHandle());
81+
new BrowsingContext(driver, WindowType.WINDOW, driver.getWindowHandle());
7882
assertThat(browsingContext.getId()).isNotEmpty();
7983
}
8084

@@ -93,7 +97,7 @@ void canCreateATab() {
9397
@NotYetImplemented(EDGE)
9498
void canCreateATabWithAReferenceContext() {
9599
BrowsingContext browsingContext =
96-
new BrowsingContext(driver, WindowType.TAB, driver.getWindowHandle());
100+
new BrowsingContext(driver, WindowType.TAB, driver.getWindowHandle());
97101
assertThat(browsingContext.getId()).isNotEmpty();
98102
}
99103

@@ -358,6 +362,32 @@ void canCaptureScreenshot() {
358362
assertThat(screenshot.length()).isPositive();
359363
}
360364

365+
@Test
366+
@NotYetImplemented(SAFARI)
367+
@NotYetImplemented(IE)
368+
@NotYetImplemented(CHROME)
369+
void canCaptureScreenshotOfViewport() {
370+
String expectedBase64EncodedImage =
371+
"iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAVklEQVQYV2P8z8AARIQB41BQOAvomScE/MIElGdcCFS4B"
372+
+ "8hYTEAx3NdLgApjkRRbA9mrgRgUdrdBJsKC5x6Qo4yksBbIbkLiwxW+BwoKIUnUAdmNSHwAe44dOkRcP14AAAAASUVORK5CYII=";
373+
374+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
375+
376+
driver.get(appServer.whereIs("coordinates_tests/simple_page.html"));
377+
WebElement element = driver.findElement(By.id("box"));
378+
379+
Rectangle elementRectangle = element.getRect();
380+
381+
String screenshot = browsingContext.captureBoxScreenshot(
382+
elementRectangle.getX(),
383+
elementRectangle.getY(),
384+
5,
385+
5);
386+
387+
assertThat(screenshot.length()).isPositive();
388+
assertThat(screenshot).isEqualTo(expectedBase64EncodedImage);
389+
}
390+
361391
private String alertPage() {
362392
return appServer.create(
363393
new Page()

0 commit comments

Comments
 (0)