Skip to content

Commit

Permalink
[bidi][java] Update the capture screenshot APIs to include all parame…
Browse files Browse the repository at this point in the history
…ters and remove scroll parameter (#13743)

Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
  • Loading branch information
pujagani and diemol authored Mar 27, 2024
1 parent 5b60724 commit a179a98
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.bidi.browsingcontext;

import java.util.HashMap;
import java.util.Map;

public class BoxClipRectangle extends ClipRectangle {
private final Map<String, Object> map = new HashMap<>();

public BoxClipRectangle(double x, double y, double width, double height) {
super(Type.BOX);
map.put("x", x);
map.put("y", y);
map.put("width", width);
map.put("height", height);
}

public Map<String, Object> toMap() {
map.put("type", super.getType().toString());

return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ public String captureScreenshot() {
}));
}

public String captureScreenshot(CaptureScreenshotParameters parameters) {
Map<String, Object> params = new HashMap<>();
params.put(CONTEXT, id);
params.putAll(parameters.toMap());

return this.bidi.send(
new Command<>(
"browsingContext.captureScreenshot",
params,
jsonInput -> {
Map<String, Object> result = jsonInput.read(Map.class);
return (String) result.get("data");
}));
}

public String captureBoxScreenshot(double x, double y, double width, double height) {
return this.bidi.send(
new Command<>(
Expand Down Expand Up @@ -258,20 +273,14 @@ public String captureElementScreenshot(String elementId) {
CONTEXT,
id,
"clip",
Map.of(
"type",
"element",
"element",
Map.of("sharedId", elementId),
"scrollIntoView",
false)),
Map.of("type", "element", "element", Map.of("sharedId", elementId))),
jsonInput -> {
Map<String, Object> result = jsonInput.read(Map.class);
return (String) result.get("data");
}));
}

public String captureElementScreenshot(String elementId, boolean scrollIntoView) {
public String captureElementScreenshot(String elementId, String handle) {
return this.bidi.send(
new Command<>(
"browsingContext.captureScreenshot",
Expand All @@ -280,12 +289,7 @@ public String captureElementScreenshot(String elementId, boolean scrollIntoView)
id,
"clip",
Map.of(
"type",
"element",
"element",
Map.of("sharedId", elementId),
"scrollIntoView",
scrollIntoView)),
"type", "element", "element", Map.of("sharedId", elementId, "handle", handle))),
jsonInput -> {
Map<String, Object> result = jsonInput.read(Map.class);
return (String) result.get("data");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi.browsingcontext;

import java.util.HashMap;
import java.util.Map;

public class CaptureScreenshotParameters {

public enum Origin {
VIEWPORT("viewport"),
DOCUMENT("document");

private final String value;

Origin(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}
}

private final Map<String, Object> map = new HashMap<>();

public CaptureScreenshotParameters origin(Origin origin) {
map.put("origin", origin.toString());
return this;
}

public CaptureScreenshotParameters imageFormat(String type) {
map.put("format", Map.of("type", type));
return this;
}

public CaptureScreenshotParameters imageFormat(String type, double quality) {
map.put("format", Map.of("type", type, "quality", quality));
return this;
}

public CaptureScreenshotParameters clipRectangle(ClipRectangle clipRectangle) {
map.put("clip", clipRectangle.toMap());
return this;
}

public Map<String, Object> toMap() {
return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.bidi.browsingcontext;

import java.util.Map;

public abstract class ClipRectangle {
enum Type {
ELEMENT("element"),
BOX("box");

private final String value;

Type(String value) {
this.value = value;
}

@Override
public String toString() {
return value;
}
}

private Type type;

ClipRectangle(Type type) {
this.type = type;
}

Type getType() {
return type;
}

public abstract Map<String, Object> toMap();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.bidi.browsingcontext;

import java.util.HashMap;
import java.util.Map;

public class ElementClipRectangle extends ClipRectangle {
private final Map<String, Object> map = new HashMap<>();

public ElementClipRectangle(String sharedId) {
super(Type.ELEMENT);
map.put("sharedId", sharedId);
}

public ElementClipRectangle(String sharedId, String handle) {
super(Type.ELEMENT);
map.put("sharedId", sharedId);
map.put("handle", handle);
}

public Map<String, Object> toMap() {
map.put("type", super.getType().toString());

return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.junit.jupiter.api.BeforeEach;
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;
Expand Down Expand Up @@ -384,18 +383,26 @@ void canCaptureScreenshot() {
@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(CHROME)
void canCaptureScreenshotOfViewport() {
void canCaptureScreenshotWithAllParameters() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());

driver.get(appServer.whereIs("coordinates_tests/simple_page.html"));
WebElement element = driver.findElement(By.id("box"));

Rectangle elementRectangle = element.getRect();

ClipRectangle clipRectangle =
new BoxClipRectangle(elementRectangle.getX(), elementRectangle.getY(), 5, 5);

CaptureScreenshotParameters parameters = new CaptureScreenshotParameters();
// TODO: Add test to test the type and quality
// parameters.imageFormat("image/png", 0.5);

String screenshot =
browsingContext.captureBoxScreenshot(
elementRectangle.getX(), elementRectangle.getY(), 5, 5);
browsingContext.captureScreenshot(
parameters
.origin(CaptureScreenshotParameters.Origin.DOCUMENT)
.clipRectangle(clipRectangle));

assertThat(screenshot.length()).isPositive();
}
Expand All @@ -404,15 +411,17 @@ void canCaptureScreenshotOfViewport() {
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(CHROME)
void canCaptureElementScreenshot() {
void canCaptureScreenshotOfViewport() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());

driver.get(appServer.whereIs("formPage.html"));
driver.get(appServer.whereIs("coordinates_tests/simple_page.html"));
WebElement element = driver.findElement(By.id("box"));

WebElement element = driver.findElement(By.id("checky"));
Rectangle elementRectangle = element.getRect();

String screenshot =
browsingContext.captureElementScreenshot(((RemoteWebElement) element).getId());
browsingContext.captureBoxScreenshot(
elementRectangle.getX(), elementRectangle.getY(), 5, 5);

assertThat(screenshot.length()).isPositive();
}
Expand All @@ -421,18 +430,15 @@ void canCaptureElementScreenshot() {
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(CHROME)
@NotYetImplemented(FIREFOX)
void canScrollAndCaptureElementScreenshot() {
Dimension dimension = new Dimension(300, 300);
driver.manage().window().setSize(dimension);
void canCaptureElementScreenshot() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());

driver.get(appServer.whereIs("formPage.html"));

WebElement element = driver.findElement(By.id("checkbox-with-label"));
WebElement element = driver.findElement(By.id("checky"));

String screenshot =
browsingContext.captureElementScreenshot(((RemoteWebElement) element).getId(), true);
browsingContext.captureElementScreenshot(((RemoteWebElement) element).getId());

assertThat(screenshot.length()).isPositive();
}
Expand Down

0 comments on commit a179a98

Please sign in to comment.