From 6c6b5622b90dd52b696e1512fc811b7985741af1 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Wed, 11 Oct 2023 16:39:08 +0530 Subject: [PATCH] [java][bidi] Add print command --- .../bidi/browsingcontext/BrowsingContext.java | 17 ++++++++++++++++ .../browsingcontext/BrowsingContextTest.java | 20 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java b/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java index a4d3864597644..303ac17292302 100644 --- a/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java +++ b/java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java @@ -32,6 +32,9 @@ import org.openqa.selenium.json.Json; import org.openqa.selenium.json.JsonInput; import org.openqa.selenium.json.TypeToken; +import org.openqa.selenium.print.PageMargin; +import org.openqa.selenium.print.PageSize; +import org.openqa.selenium.print.PrintOptions; public class BrowsingContext { @@ -310,6 +313,20 @@ public void activate() { this.bidi.send(new Command<>("browsingContext.activate", Map.of(CONTEXT, id))); } + public String print(PrintOptions printOptions) { + Map printOptionsParams = printOptions.toMap(); + printOptionsParams.put(CONTEXT, id); + + return this.bidi.send( + new Command<>( + "browsingContext.print", + printOptionsParams, + jsonInput -> { + Map result = jsonInput.read(Map.class); + return (String) result.get("data"); + })); + } + public void close() { // This might need more clean up actions once the behavior is defined. // Specially when last tab or window is closed. diff --git a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java index 02dbd1e3ed03e..6fdcf880ccead 100644 --- a/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java +++ b/java/test/org/openqa/selenium/bidi/browsingcontext/BrowsingContextTest.java @@ -41,6 +41,7 @@ import org.openqa.selenium.environment.webserver.AppServer; import org.openqa.selenium.environment.webserver.NettyAppServer; import org.openqa.selenium.environment.webserver.Page; +import org.openqa.selenium.print.PrintOptions; import org.openqa.selenium.remote.RemoteWebElement; import org.openqa.selenium.testing.JupiterTestBase; import org.openqa.selenium.testing.NotYetImplemented; @@ -478,6 +479,25 @@ void canSetViewportWithDevicePixelRatio() { assertThat(newDevicePixelRatio).isEqualTo(5); } + @Test + @NotYetImplemented(SAFARI) + @NotYetImplemented(IE) + void canPrintPage() { + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); + + driver.get(appServer.whereIs("formPage.html")); + PrintOptions printOptions = new PrintOptions(); + + String printPage = browsingContext.print(printOptions); + + assertThat(printPage.length()).isPositive(); + // Comparing expected PDF is a hard problem. + // As long as we are sending the parameters correctly it should be fine. + // Trusting the browsers to do the right thing. + // Hence, just checking if the response is base64 encoded string. + assertThat(printPage).contains("JVBER"); + } + private String alertPage() { return appServer.create( new Page()