Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Print page support in Java #8991

Merged
merged 32 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
44955d5
Add PrintOptions class and supporting classes for page margin and size
raju249 Dec 22, 2020
0c05ee0
Add license string in margin and page classes
raju249 Dec 22, 2020
cc3e987
Call execute to print page
raju249 Dec 28, 2020
29ca25e
Fix bazel build file
raju249 Dec 28, 2020
ee8e898
Add bazel build file to printoptions
raju249 Dec 28, 2020
b6595aa
Remove deps from printoptions build bazel file
raju249 Dec 28, 2020
f330eef
Add Test class and return json for print options
raju249 Dec 30, 2020
79d8735
Add assertion to test with magic string
raju249 Dec 30, 2020
0e052bd
Add test for multiple print options
raju249 Dec 30, 2020
2fa568a
Rename file with chrome specific tests
raju249 Dec 31, 2020
1926c59
Add PrintPage tests for firefox
raju249 Dec 31, 2020
ca15415
Add PageSize tests
raju249 Jan 4, 2021
163ebab
Add PageMarginTests
raju249 Jan 4, 2021
4ccccf4
Add PrintOptions class tests
raju249 Jan 4, 2021
2dfcf10
Fix code smells
raju249 Jan 5, 2021
369b07d
Add a print package inside print class
raju249 Jan 6, 2021
6735f3c
Fix chrome and firefox orientation test for print command
raju249 Jan 6, 2021
14bca93
Add new line in @After
raju249 Jan 7, 2021
38f8368
Remove chrome test
raju249 Jan 8, 2021
d6980c0
Add pdf class and PrintsPage interface
raju249 Jan 12, 2021
191af19
Fix unwanted changes
raju249 Jan 12, 2021
41e0c35
Change method signature
raju249 Jan 12, 2021
4a7dde4
Add overloaded version of Require.positive for double
raju249 Jan 12, 2021
fc8578e
Fix with suggestions for Print classess
raju249 Jan 13, 2021
c80bddf
Rename printoptions package to print
raju249 Jan 13, 2021
ce4b38d
Require nonNull is Print methods
raju249 Jan 15, 2021
aa897c9
Add PrintPage test in root level
raju249 Jan 18, 2021
eb4fb3f
Change temporarily to Object class and remove PrintPage interface
raju249 Jan 18, 2021
d37abc6
Change integer type in NodeStatus
raju249 Jan 18, 2021
d3a0356
Revert to independent Interface class
raju249 Jan 18, 2021
dc8a584
Fix import error and remove print BUILD.bazel files
raju249 Jan 19, 2021
ce1ea4a
Add a comment for page size and change pageRanges setter
raju249 Jan 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this empty line.

]),
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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer adding a second positive(String, double) that delegates down to this three-param version. Using null in code is generally Not A Great Idea, and it looks ugly.

} 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussion in the selenium-tlc channel, we're leaning towards immutable data-types. We can remove the setters in a follow-up PR, since this review has already gone on a while and that's a new change

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;
shs96c marked this conversation as resolved.
Show resolved Hide resolved
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the methods do a cast and assume the type is right. You can do return new Pdf((String) result);

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