selenide / selenide

Concise UI Tests with Java!
http://selenide.org
MIT License
1.81k stars 571 forks source link

Emulate css print media type #1391

Open asolntsev opened 3 years ago

asolntsev commented 3 years ago

Got feature idea "emulate css print media type" command so we wouldn't have to use AutoIT for that :) Helpful for doing print testing

asolntsev commented 2 years ago

I realized there is an interface org.openqa.selenium.PrintsPage (implemented at least by Chrome, Firefox and Safari).

  @Test
  void printPdf() throws IOException {
    open("https://ru.selenide.org");
    PrintsPage driver = (PrintsPage) WebDriverRunner.getWebDriver();
    Pdf pdf = driver.print(new PrintOptions());
    byte[] bytes = Base64.getDecoder().decode(pdf.getContent());
    File out = new File("/Users/andrei/projects/selenide/some.pdf");
    FileUtils.writeByteArrayToFile(out, bytes);
    assertThat(out).exists();
  }

But how it would be convenient to make assertions? There is library pdf-test for testing PDFs, but it doesn't check layout, only the content.

UPD this mode works only in "old" headless mode (chrome argument --headless). But in "new" headless mode (chrome argument --headless=new), it throws an exception:

WebDriverException: unknown error: PrintToPDF is only supported in headless mode