From a179a98be1fe10f16c1372cf2060c36cecbe1cd1 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Wed, 27 Mar 2024 15:35:09 +0530 Subject: [PATCH] [bidi][java] Update the capture screenshot APIs to include all parameters and remove scroll parameter (#13743) Co-authored-by: Diego Molina --- .../browsingcontext/BoxClipRectangle.java | 38 +++++++++++ .../bidi/browsingcontext/BrowsingContext.java | 32 +++++---- .../CaptureScreenshotParameters.java | 66 +++++++++++++++++++ .../bidi/browsingcontext/ClipRectangle.java | 49 ++++++++++++++ .../browsingcontext/ElementClipRectangle.java | 41 ++++++++++++ .../browsingcontext/BrowsingContextTest.java | 36 +++++----- 6 files changed, 233 insertions(+), 29 deletions(-) create mode 100644 java/src/org/openqa/selenium/bidi/browsingcontext/BoxClipRectangle.java create mode 100644 java/src/org/openqa/selenium/bidi/browsingcontext/CaptureScreenshotParameters.java create mode 100644 java/src/org/openqa/selenium/bidi/browsingcontext/ClipRectangle.java create mode 100644 java/src/org/openqa/selenium/bidi/browsingcontext/ElementClipRectangle.java diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/BoxClipRectangle.java b/java/src/org/openqa/selenium/bidi/browsingcontext/BoxClipRectangle.java new file mode 100644 index 0000000000000..925c22de7bdf8 --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/BoxClipRectangle.java @@ -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 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 toMap() { + map.put("type", super.getType().toString()); + + return map; + } +} diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java b/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java index e416b8f873cd3..79ff714d5ce1d 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java @@ -230,6 +230,21 @@ public String captureScreenshot() { })); } + public String captureScreenshot(CaptureScreenshotParameters parameters) { + Map params = new HashMap<>(); + params.put(CONTEXT, id); + params.putAll(parameters.toMap()); + + return this.bidi.send( + new Command<>( + "browsingContext.captureScreenshot", + params, + jsonInput -> { + Map 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<>( @@ -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 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", @@ -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 result = jsonInput.read(Map.class); return (String) result.get("data"); diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/CaptureScreenshotParameters.java b/java/src/org/openqa/selenium/bidi/browsingcontext/CaptureScreenshotParameters.java new file mode 100644 index 0000000000000..47415049a62a3 --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/CaptureScreenshotParameters.java @@ -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 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 toMap() { + return map; + } +} diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/ClipRectangle.java b/java/src/org/openqa/selenium/bidi/browsingcontext/ClipRectangle.java new file mode 100644 index 0000000000000..ede834507f4a4 --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/ClipRectangle.java @@ -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 toMap(); +} diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/ElementClipRectangle.java b/java/src/org/openqa/selenium/bidi/browsingcontext/ElementClipRectangle.java new file mode 100644 index 0000000000000..ef5d686e49f17 --- /dev/null +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/ElementClipRectangle.java @@ -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 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 toMap() { + map.put("type", super.getType().toString()); + + return map; + } +} diff --git a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java index 1d4667845ea28..d4a2295ac81dc 100644 --- a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java +++ b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java @@ -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; @@ -384,8 +383,7 @@ 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")); @@ -393,9 +391,18 @@ void canCaptureScreenshotOfViewport() { 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(); } @@ -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(); } @@ -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(); }