Skip to content

Commit

Permalink
Add Print page support in Java (#8991)
Browse files Browse the repository at this point in the history
  • Loading branch information
raju249 authored Jan 19, 2021
1 parent 87db0cb commit 626f908
Show file tree
Hide file tree
Showing 16 changed files with 545 additions and 1 deletion.
2 changes: 2 additions & 0 deletions java/client/src/org/openqa/selenium/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ java_export(
"mobile/*.java",
"net/*.java",
"virtualauthenticator/*.java",
"print/*.java",

]),
hides = [
"org.openqa.selenium.interactions.internal",
Expand Down
31 changes: 31 additions & 0 deletions java/client/src/org/openqa/selenium/Pdf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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;

public class Pdf {

private final String base64EncodedPdf;

public Pdf(String base64EncodedPdf) {
this.base64EncodedPdf = base64EncodedPdf;
}

public String getContent() {
return base64EncodedPdf;
}
}
23 changes: 23 additions & 0 deletions java/client/src/org/openqa/selenium/PrintsPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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;

import org.openqa.selenium.print.PrintOptions;

public interface PrintsPage {
Pdf print(PrintOptions printOptions) throws WebDriverException;
}
15 changes: 15 additions & 0 deletions java/client/src/org/openqa/selenium/internal/Require.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ public static int positive(String argName, Integer number, String message) {
return number;
}

public static double positive(String argName, double number, String message) {
if (number <= 0) {
if (message == null) {
throw new IllegalArgumentException(argName + " must be greater than 0");
} else {
throw new IllegalArgumentException(message);
}
}
return number;
}

public static double positive(String argName, double number) {
return positive(argName, number, null);
}

public static int positive(String argName, Integer number) {
return positive(argName, number, null);
}
Expand Down
67 changes: 67 additions & 0 deletions java/client/src/org/openqa/selenium/print/PageMargin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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.print;

import org.openqa.selenium.internal.Require;

public class PageMargin {
private double top;
private double bottom;
private double left;
private double right;

public PageMargin() {
this.top = 1.0;
this.bottom = 1.0;
this.left = 1.0;
this.right = 1.0;
}

public double getTop() {
return top;
}

public void setTop(double top) {
this.top = Require.positive("top", top);
}

public double getBottom() {
return bottom;
}

public void setBottom(double bottom) {
this.bottom = Require.positive("bottom", bottom);
}

public double getLeft() {
return left;
}

public void setLeft(double left) {
this.left = Require.positive("left", left);
}

public double getRight() {
return right;
}

public void setRight(double right) {
this.right = Require.positive("right", right);
}

}
48 changes: 48 additions & 0 deletions java/client/src/org/openqa/selenium/print/PageSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.print;


import org.openqa.selenium.internal.Require;

public class PageSize {

private double height;
private double width;

public PageSize() {
// Initialize with defaults. A4 paper size defaults in cms.
this.height = 21.59;
this.width = 27.94;
}
public double getHeight() {
return height;
}

public double getWidth() {
return width;
}

public void setHeight(double height) {
this.height = Require.positive("height", height);
}

public void setWidth(double width) {
this.width = Require.positive("width", width);
}
}
101 changes: 101 additions & 0 deletions java/client/src/org/openqa/selenium/print/PrintOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// 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.print;

import org.openqa.selenium.internal.Require;

public class PrintOptions {

public enum Orientation {
Portrait,
Landscape
}
private Orientation orientation = Orientation.Portrait;
private double scale = 1.0;
private boolean background = false;
private boolean shrinkToFit = true;
private PageSize pageSize = new PageSize();
private PageMargin pageMargin = new PageMargin();
private String[] pageRanges;

public Orientation getOrientation() {
return this.orientation;
}

public void setOrientation(Orientation orientation) {
this.orientation = Require.nonNull("orientation", orientation);
}

public String[] getPageRanges() {
return this.pageRanges;
}

public void setPageRanges(String firstRange, String ... ranges) {
Require.nonNull("pageRanges", firstRange);
this.pageRanges = new String[ranges.length + 1]; // Need to add all ranges and the initial range too.

this.pageRanges[0] = firstRange;

for (int i = 1; i < ranges.length; i++) {
this.pageRanges[i] = ranges[i - 1];
}
}

public void setBackground(boolean background) {
this.background = Require.nonNull("background", background);
}

public boolean getBackground() {
return this.background;
}

public void setScale(double scale) {
if (scale < 0.1 || scale > 2) {
throw new IllegalArgumentException("Scale value should be between 0.1 and 2");
}
this.scale = scale;
}

public double getScale() {
return this.scale;
}

public boolean getShrinkToFit() {
return this.shrinkToFit;
}

public void setShrinkToFit(boolean value) {
this.shrinkToFit = Require.nonNull("value", value);
}

public void setPageSize(PageSize pageSize) {
this.pageSize = Require.nonNull("pageSize", pageSize);
}

public void setPageMargin(PageMargin margin) {
this.pageMargin = Require.nonNull("margin", margin);
}

public PageSize getPageSize() {
return this.pageSize;
}

public PageMargin getPageMargin() {
return this.pageMargin;
}
}
6 changes: 6 additions & 0 deletions java/client/src/org/openqa/selenium/remote/DriverCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openqa.selenium.Point;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.interactions.Sequence;
import org.openqa.selenium.print.PrintOptions;

import java.time.Duration;
import java.util.Collection;
Expand Down Expand Up @@ -227,6 +228,11 @@ static CommandPayload SET_ALERT_VALUE(String keysToSend) {

String SET_TIMEOUT = "setTimeout";

String PRINT_PAGE = "printPage";
static CommandPayload PRINT_PAGE(PrintOptions options) {
return new CommandPayload(PRINT_PAGE, ImmutableMap.of("options", options));
}

@Deprecated
static CommandPayload SET_IMPLICIT_WAIT_TIMEOUT(long time, TimeUnit unit) {
return new CommandPayload(
Expand Down
13 changes: 12 additions & 1 deletion java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.logging.Logs;
import org.openqa.selenium.logging.NeedsLocalLogs;
import org.openqa.selenium.print.PrintOptions;
import org.openqa.selenium.Pdf;
import org.openqa.selenium.PrintsPage;
import org.openqa.selenium.remote.internal.WebElementToJsonConverter;
import org.openqa.selenium.virtualauthenticator.Credential;
import org.openqa.selenium.virtualauthenticator.HasVirtualAuthenticator;
Expand Down Expand Up @@ -87,7 +90,7 @@
@Augmentable
public class RemoteWebDriver implements WebDriver, JavascriptExecutor, HasInputDevices,
HasCapabilities, Interactive, TakesScreenshot,
HasVirtualAuthenticator {
HasVirtualAuthenticator, PrintsPage {

// TODO(dawagner): This static logger should be unified with the per-instance localLogs
private static final Logger logger = Logger.getLogger(RemoteWebDriver.class.getName());
Expand Down Expand Up @@ -324,6 +327,14 @@ public <X> X getScreenshotAs(OutputType<X> outputType) throws WebDriverException
}
}

@Override
public Pdf print(PrintOptions printOptions) throws WebDriverException {
Response response = execute(DriverCommand.PRINT_PAGE(printOptions));

Object result = response.getValue();
return new Pdf((String) result);
}

@Override
public WebElement findElement(By locator) {
if (locator instanceof By.StandardLocator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import static org.openqa.selenium.remote.DriverCommand.SET_TIMEOUT;
import static org.openqa.selenium.remote.DriverCommand.SUBMIT_ELEMENT;
import static org.openqa.selenium.remote.DriverCommand.UPLOAD_FILE;
import static org.openqa.selenium.remote.DriverCommand.PRINT_PAGE;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -154,6 +155,8 @@ public W3CHttpCommandCodec() {
defineCommand(GET_ALERT_TEXT, get(alert + "/text"));
defineCommand(SET_ALERT_VALUE, post(alert + "/text"));

defineCommand(PRINT_PAGE, post(sessionId + "/print"));

defineCommand(UPLOAD_FILE, post(sessionId + "/se/file"));

defineCommand(GET_ACTIVE_ELEMENT, get(sessionId + "/element/active"));
Expand Down
3 changes: 3 additions & 0 deletions java/client/test/org/openqa/selenium/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ SMALL_TESTS = [
"ProxyTest.java",
"RequireTest.java",
"WebDriverExceptionTest.java",
"print/PrintOptionsTest.java",
"print/PageMarginTest.java",
"print/PageSizeTest.java"
]

java_test_suite(
Expand Down
Loading

0 comments on commit 626f908

Please sign in to comment.