Testing PDF using Selenium in headless mode

361 views
Skip to first unread message

Techno Park

unread,
Feb 22, 2021, 4:31:22 AM2/22/21
to seleniu...@googlegroups.com
Could anyone give me a hand here? I need to verify the contents of a pdf file. So, I click on a button to open a pdf file which opens in a new tab. And, I need to verify the contents. I have given below the sample code which works in 'non-headless' mode (Chrome), but not in 'headless' which is what I want.  I use Java and the plugin used is pdfBox.  The error I get is "  org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died."  The error points to this line of code --> URL url = new URL(webDriver.getCurrentUrl());
     
    webDriver.get("https://irl.umsl.edu/oer/24/"); 
//Below link opens in a new tab        
webDriver.findElement(By.xpath("//a[normalize-space()='Download']")).click();
ArrayList<String> tabs = new ArrayList<String>(webDriver.getWindowHandles());
webDriver.switchTo().window(tabs.get(1));
URL url = new URL(webDriver.getCurrentUrl());
InputStream in = url.openStream();
BufferedInputStream bf = new BufferedInputStream(in);
PDDocument doc = PDDocument.load(bf);
String content = new PDFTextStripper().getText(doc);
Assert.assertTrue(content.contains("Intercultural Communication"));

Andrei Solntsev

unread,
Feb 22, 2021, 4:09:56 PM2/22/21
to seleniu...@googlegroups.com
Hi Techno Park!

Let me recommend you library "pdf-test" which allows you to parse and verify PDF documents.

It's API is very simple:

  @Test
  public void canAssertThatPdfContainsText() {
    File file = $("a#report").download(); // just download the file using Selenium or Selenide methods
    PDF pdf = new PDF(file);
    assertThat(pdf, containsText("50 Quick Ideas to Improve your User Stories"));
  }


Andrei Solntsev


пн, 22 февр. 2021 г. в 11:31, Techno Park <sunil.does...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAOTG%2BFCV7cVAYznyg1QA%3DSWOYcgNDFtSO37%2BUDUFQek2oiA3mg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages