Skip to content

Commit 11647cc

Browse files
[java] Fixing options handling for print command
Prior to this change, when executing e.g. a `RemoteWebDriver#print` command, the print options passed to the method were passed through to the chromedriver instance, but serialized in a wrong format, rendering them uneffective. The issues were: - Options were wrapped in an `options` key when chromedriver expected them to be at the top-level of the parameters dict (see [here](https://github.com/chromium/chromium/blob/main/chrome/test/chromedriver/window_commands.cc#L2246)) - Page size options were put into a `pageSize` key, when chromedriver expected it to be a `page` key (see [here](https://github.com/chromium/chromium/blob/main/chrome/test/chromedriver/window_commands.cc#L539)) Also, using the `PageSize` class supplied by selenium was not possible, due to defaulting to constant page sizes, not allowing the consumer to override them. This was addressed by adding a second constructor allowing to specify height/width.
1 parent 74f6344 commit 11647cc

4 files changed

Lines changed: 44 additions & 3 deletions

File tree

java/src/org/openqa/selenium/print/PageMargin.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public PageMargin() {
3030
this.right = 1.0;
3131
}
3232

33+
public PageMargin(double top, double bottom, double left, double right) {
34+
this.top = top;
35+
this.bottom = bottom;
36+
this.left = left;
37+
this.right = right;
38+
}
39+
3340
public double getTop() {
3441
return top;
3542
}

java/src/org/openqa/selenium/print/PageSize.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public PageSize() {
2828
this.height = 21.59;
2929
this.width = 27.94;
3030
}
31+
32+
public PageSize(double height, double width) {
33+
this.height = height;
34+
this.width = width;
35+
}
3136
public double getHeight() {
3237
return height;
3338
}

java/src/org/openqa/selenium/print/PrintOptions.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,24 @@
1919

2020
import org.openqa.selenium.internal.Require;
2121

22+
import java.util.HashMap;
23+
import java.util.Map;
24+
2225
public class PrintOptions {
2326

2427
public enum Orientation {
25-
PORTRAIT,
26-
LANDSCAPE
28+
PORTRAIT("portrait"),
29+
LANDSCAPE("landscape");
30+
31+
private final String serialFormat;
32+
Orientation(String serialFormat) {
33+
this.serialFormat = serialFormat;
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return serialFormat;
39+
}
2740
}
2841
private Orientation orientation = Orientation.PORTRAIT;
2942
private double scale = 1.0;
@@ -98,4 +111,20 @@ public PageSize getPageSize() {
98111
public PageMargin getPageMargin() {
99112
return this.pageMargin;
100113
}
114+
115+
public Map<String, Object> toMap() {
116+
final Map<String, Object> options = new HashMap<>(7);
117+
options.put("page", getPageSize());
118+
options.put("orientation", getOrientation().toString());
119+
options.put("scale", getScale());
120+
options.put("shrinkToFit", getShrinkToFit());
121+
options.put("background", getBackground());
122+
final String[] effectivePageRanges = getPageRanges();
123+
if (effectivePageRanges != null) {
124+
options.put("effectivePageRanges", effectivePageRanges);
125+
}
126+
options.put("margin", getPageMargin());
127+
128+
return options;
129+
}
101130
}

java/src/org/openqa/selenium/remote/DriverCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static CommandPayload SET_ALERT_VALUE(String keysToSend) {
275275

276276
String PRINT_PAGE = "printPage";
277277
static CommandPayload PRINT_PAGE(PrintOptions options) {
278-
return new CommandPayload(PRINT_PAGE, ImmutableMap.of("options", options));
278+
return new CommandPayload(PRINT_PAGE, options.toMap());
279279
}
280280

281281
@Deprecated

0 commit comments

Comments
 (0)