Skip to content

Commit

Permalink
[java][bidi] Add print command
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Oct 11, 2023
1 parent 966cb9f commit 6c6b562
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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

public String print(PrintOptions printOptions) {
Map<String, Object> printOptionsParams = printOptions.toMap();
printOptionsParams.put(CONTEXT, id);

return this.bidi.send(
new Command<>(
"browsingContext.print",
printOptionsParams,
jsonInput -> {
Map<String, Object> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 6c6b562

Please sign in to comment.