Skip to content

Commit 6c6b562

Browse files
committed
[java][bidi] Add print command
1 parent 966cb9f commit 6c6b562

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
import org.openqa.selenium.json.Json;
3333
import org.openqa.selenium.json.JsonInput;
3434
import org.openqa.selenium.json.TypeToken;
35+
import org.openqa.selenium.print.PageMargin;
36+
import org.openqa.selenium.print.PageSize;
37+
import org.openqa.selenium.print.PrintOptions;
3538

3639
public class BrowsingContext {
3740

@@ -310,6 +313,20 @@ public void activate() {
310313
this.bidi.send(new Command<>("browsingContext.activate", Map.of(CONTEXT, id)));
311314
}
312315

316+
public String print(PrintOptions printOptions) {
317+
Map<String, Object> printOptionsParams = printOptions.toMap();
318+
printOptionsParams.put(CONTEXT, id);
319+
320+
return this.bidi.send(
321+
new Command<>(
322+
"browsingContext.print",
323+
printOptionsParams,
324+
jsonInput -> {
325+
Map<String, Object> result = jsonInput.read(Map.class);
326+
return (String) result.get("data");
327+
}));
328+
}
329+
313330
public void close() {
314331
// This might need more clean up actions once the behavior is defined.
315332
// Specially when last tab or window is closed.

java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.openqa.selenium.environment.webserver.AppServer;
4242
import org.openqa.selenium.environment.webserver.NettyAppServer;
4343
import org.openqa.selenium.environment.webserver.Page;
44+
import org.openqa.selenium.print.PrintOptions;
4445
import org.openqa.selenium.remote.RemoteWebElement;
4546
import org.openqa.selenium.testing.JupiterTestBase;
4647
import org.openqa.selenium.testing.NotYetImplemented;
@@ -478,6 +479,25 @@ void canSetViewportWithDevicePixelRatio() {
478479
assertThat(newDevicePixelRatio).isEqualTo(5);
479480
}
480481

482+
@Test
483+
@NotYetImplemented(SAFARI)
484+
@NotYetImplemented(IE)
485+
void canPrintPage() {
486+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
487+
488+
driver.get(appServer.whereIs("formPage.html"));
489+
PrintOptions printOptions = new PrintOptions();
490+
491+
String printPage = browsingContext.print(printOptions);
492+
493+
assertThat(printPage.length()).isPositive();
494+
// Comparing expected PDF is a hard problem.
495+
// As long as we are sending the parameters correctly it should be fine.
496+
// Trusting the browsers to do the right thing.
497+
// Hence, just checking if the response is base64 encoded string.
498+
assertThat(printPage).contains("JVBER");
499+
}
500+
481501
private String alertPage() {
482502
return appServer.create(
483503
new Page()

0 commit comments

Comments
 (0)