-
-
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 30 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
load("//java:defs.bzl", "java_library") | ||
|
||
java_library( | ||
name = "print", | ||
srcs = glob(["**/*.java"]), | ||
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.
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. This file isn't needed at all |
||
visibility = [ | ||
"//visibility:public", | ||
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. This should only be visible to the |
||
], | ||
deps = [ | ||
"//java/client/src/org/openqa/selenium:core", | ||
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. I'd suggest that this package needs to be exported by |
||
] | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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 double getBottom() { | ||
return bottom; | ||
} | ||
|
||
public double getLeft() { | ||
return left; | ||
} | ||
|
||
public double getRight() { | ||
return right; | ||
} | ||
|
||
public void setTop(double top) { | ||
raju249 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.top = Require.positive("top", top, null); | ||
} | ||
|
||
public void setBottom(double bottom) { | ||
this.bottom = Require.positive("bottom", bottom, null); | ||
} | ||
|
||
public void setRight(double right) { | ||
this.right = Require.positive("right", right, null); | ||
} | ||
|
||
public void setLeft(double left) { | ||
this.left = Require.positive("left", left, null); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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; | ||
|
||
|
||
public class PageSize { | ||
|
||
private double height; | ||
private double width; | ||
|
||
public PageSize() { | ||
// Initialize with defaults | ||
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 = height; | ||
raju249 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public void setWidth(double width) { | ||
this.width = width; | ||
raju249 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// 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[] ranges) { | ||
this.pageRanges = Require.nonNull("pageRanges", ranges); | ||
} | ||
|
||
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.
Prefer adding a second
positive(String, double)
that delegates down to this three-param version. Usingnull
in code is generally Not A Great Idea, and it looks ugly.