-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changes from all commits
44955d5
0c05ee0
cc3e987
29ca25e
ee8e898
b6595aa
f330eef
79d8735
0e052bd
2fa568a
1926c59
ca15415
163ebab
4ccccf4
2dfcf10
369b07d
6735f3c
14bca93
38f8368
d6980c0
191af19
41e0c35
4a7dde4
fc8578e
c80bddf
ce4b38d
aa897c9
eb4fb3f
d37abc6
d3a0356
dc8a584
ce1ea4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} |
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer adding a second |
||
} 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); | ||
} | ||
|
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
} |
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); | ||
} | ||
} |
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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()); | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
@Override | ||
public WebElement findElement(By locator) { | ||
if (locator instanceof By.StandardLocator) { | ||
|
There was a problem hiding this comment.
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.