Skip to content

Commit a179a98

Browse files
pujaganidiemol
andauthored
[bidi][java] Update the capture screenshot APIs to include all parameters and remove scroll parameter (#13743)
Co-authored-by: Diego Molina <[email protected]>
1 parent 5b60724 commit a179a98

File tree

6 files changed

+233
-29
lines changed

6 files changed

+233
-29
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.openqa.selenium.bidi.browsingcontext;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
public class BoxClipRectangle extends ClipRectangle {
23+
private final Map<String, Object> map = new HashMap<>();
24+
25+
public BoxClipRectangle(double x, double y, double width, double height) {
26+
super(Type.BOX);
27+
map.put("x", x);
28+
map.put("y", y);
29+
map.put("width", width);
30+
map.put("height", height);
31+
}
32+
33+
public Map<String, Object> toMap() {
34+
map.put("type", super.getType().toString());
35+
36+
return map;
37+
}
38+
}

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,21 @@ public String captureScreenshot() {
230230
}));
231231
}
232232

233+
public String captureScreenshot(CaptureScreenshotParameters parameters) {
234+
Map<String, Object> params = new HashMap<>();
235+
params.put(CONTEXT, id);
236+
params.putAll(parameters.toMap());
237+
238+
return this.bidi.send(
239+
new Command<>(
240+
"browsingContext.captureScreenshot",
241+
params,
242+
jsonInput -> {
243+
Map<String, Object> result = jsonInput.read(Map.class);
244+
return (String) result.get("data");
245+
}));
246+
}
247+
233248
public String captureBoxScreenshot(double x, double y, double width, double height) {
234249
return this.bidi.send(
235250
new Command<>(
@@ -258,20 +273,14 @@ public String captureElementScreenshot(String elementId) {
258273
CONTEXT,
259274
id,
260275
"clip",
261-
Map.of(
262-
"type",
263-
"element",
264-
"element",
265-
Map.of("sharedId", elementId),
266-
"scrollIntoView",
267-
false)),
276+
Map.of("type", "element", "element", Map.of("sharedId", elementId))),
268277
jsonInput -> {
269278
Map<String, Object> result = jsonInput.read(Map.class);
270279
return (String) result.get("data");
271280
}));
272281
}
273282

274-
public String captureElementScreenshot(String elementId, boolean scrollIntoView) {
283+
public String captureElementScreenshot(String elementId, String handle) {
275284
return this.bidi.send(
276285
new Command<>(
277286
"browsingContext.captureScreenshot",
@@ -280,12 +289,7 @@ public String captureElementScreenshot(String elementId, boolean scrollIntoView)
280289
id,
281290
"clip",
282291
Map.of(
283-
"type",
284-
"element",
285-
"element",
286-
Map.of("sharedId", elementId),
287-
"scrollIntoView",
288-
scrollIntoView)),
292+
"type", "element", "element", Map.of("sharedId", elementId, "handle", handle))),
289293
jsonInput -> {
290294
Map<String, Object> result = jsonInput.read(Map.class);
291295
return (String) result.get("data");
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.bidi.browsingcontext;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
public class CaptureScreenshotParameters {
24+
25+
public enum Origin {
26+
VIEWPORT("viewport"),
27+
DOCUMENT("document");
28+
29+
private final String value;
30+
31+
Origin(String value) {
32+
this.value = value;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return value;
38+
}
39+
}
40+
41+
private final Map<String, Object> map = new HashMap<>();
42+
43+
public CaptureScreenshotParameters origin(Origin origin) {
44+
map.put("origin", origin.toString());
45+
return this;
46+
}
47+
48+
public CaptureScreenshotParameters imageFormat(String type) {
49+
map.put("format", Map.of("type", type));
50+
return this;
51+
}
52+
53+
public CaptureScreenshotParameters imageFormat(String type, double quality) {
54+
map.put("format", Map.of("type", type, "quality", quality));
55+
return this;
56+
}
57+
58+
public CaptureScreenshotParameters clipRectangle(ClipRectangle clipRectangle) {
59+
map.put("clip", clipRectangle.toMap());
60+
return this;
61+
}
62+
63+
public Map<String, Object> toMap() {
64+
return map;
65+
}
66+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.openqa.selenium.bidi.browsingcontext;
18+
19+
import java.util.Map;
20+
21+
public abstract class ClipRectangle {
22+
enum Type {
23+
ELEMENT("element"),
24+
BOX("box");
25+
26+
private final String value;
27+
28+
Type(String value) {
29+
this.value = value;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return value;
35+
}
36+
}
37+
38+
private Type type;
39+
40+
ClipRectangle(Type type) {
41+
this.type = type;
42+
}
43+
44+
Type getType() {
45+
return type;
46+
}
47+
48+
public abstract Map<String, Object> toMap();
49+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.openqa.selenium.bidi.browsingcontext;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
public class ElementClipRectangle extends ClipRectangle {
23+
private final Map<String, Object> map = new HashMap<>();
24+
25+
public ElementClipRectangle(String sharedId) {
26+
super(Type.ELEMENT);
27+
map.put("sharedId", sharedId);
28+
}
29+
30+
public ElementClipRectangle(String sharedId, String handle) {
31+
super(Type.ELEMENT);
32+
map.put("sharedId", sharedId);
33+
map.put("handle", handle);
34+
}
35+
36+
public Map<String, Object> toMap() {
37+
map.put("type", super.getType().toString());
38+
39+
return map;
40+
}
41+
}

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.junit.jupiter.api.BeforeEach;
3535
import org.junit.jupiter.api.Test;
3636
import org.openqa.selenium.By;
37-
import org.openqa.selenium.Dimension;
3837
import org.openqa.selenium.JavascriptExecutor;
3938
import org.openqa.selenium.Rectangle;
4039
import org.openqa.selenium.WebElement;
@@ -384,18 +383,26 @@ void canCaptureScreenshot() {
384383
@Test
385384
@NotYetImplemented(SAFARI)
386385
@NotYetImplemented(IE)
387-
@NotYetImplemented(CHROME)
388-
void canCaptureScreenshotOfViewport() {
386+
void canCaptureScreenshotWithAllParameters() {
389387
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
390388

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

394392
Rectangle elementRectangle = element.getRect();
395393

394+
ClipRectangle clipRectangle =
395+
new BoxClipRectangle(elementRectangle.getX(), elementRectangle.getY(), 5, 5);
396+
397+
CaptureScreenshotParameters parameters = new CaptureScreenshotParameters();
398+
// TODO: Add test to test the type and quality
399+
// parameters.imageFormat("image/png", 0.5);
400+
396401
String screenshot =
397-
browsingContext.captureBoxScreenshot(
398-
elementRectangle.getX(), elementRectangle.getY(), 5, 5);
402+
browsingContext.captureScreenshot(
403+
parameters
404+
.origin(CaptureScreenshotParameters.Origin.DOCUMENT)
405+
.clipRectangle(clipRectangle));
399406

400407
assertThat(screenshot.length()).isPositive();
401408
}
@@ -404,15 +411,17 @@ void canCaptureScreenshotOfViewport() {
404411
@NotYetImplemented(SAFARI)
405412
@NotYetImplemented(IE)
406413
@NotYetImplemented(CHROME)
407-
void canCaptureElementScreenshot() {
414+
void canCaptureScreenshotOfViewport() {
408415
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
409416

410-
driver.get(appServer.whereIs("formPage.html"));
417+
driver.get(appServer.whereIs("coordinates_tests/simple_page.html"));
418+
WebElement element = driver.findElement(By.id("box"));
411419

412-
WebElement element = driver.findElement(By.id("checky"));
420+
Rectangle elementRectangle = element.getRect();
413421

414422
String screenshot =
415-
browsingContext.captureElementScreenshot(((RemoteWebElement) element).getId());
423+
browsingContext.captureBoxScreenshot(
424+
elementRectangle.getX(), elementRectangle.getY(), 5, 5);
416425

417426
assertThat(screenshot.length()).isPositive();
418427
}
@@ -421,18 +430,15 @@ void canCaptureElementScreenshot() {
421430
@NotYetImplemented(SAFARI)
422431
@NotYetImplemented(IE)
423432
@NotYetImplemented(CHROME)
424-
@NotYetImplemented(FIREFOX)
425-
void canScrollAndCaptureElementScreenshot() {
426-
Dimension dimension = new Dimension(300, 300);
427-
driver.manage().window().setSize(dimension);
433+
void canCaptureElementScreenshot() {
428434
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
429435

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

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

434440
String screenshot =
435-
browsingContext.captureElementScreenshot(((RemoteWebElement) element).getId(), true);
441+
browsingContext.captureElementScreenshot(((RemoteWebElement) element).getId());
436442

437443
assertThat(screenshot.length()).isPositive();
438444
}

0 commit comments

Comments
 (0)