1

I am trying to assert a PDF by extracting the text from it and then checking whether the text I want is present in the extracted text or not.

The code extracts the PDF properly. The problem is, irrespective of whether the PDF extracted text contains my text or not, the assertion passes.

I am not sure why this is failing.

  public static boolean verifyPDFContent(String reqTextInPDF) throws IOException{

        PDDocument doc = PDDocument.load(new File("User/download/test.pdf"));
        PDFTextStripper pdfStripper = new PDFTextStripper();
        String text = pdfStripper.getText(doc);
        doc.close();
        System.out.println(text);
        Assert.assertTrue (text.equals (reqTextInPDF));
        return text.contains(reqTextInPDF);

    }

I call it through:

@Then("^I should verify$")
    public void iShouldVerify() throws Throwable {
        export_inspections.verifyPDFContent("z" );
    }
8
  • For starters, this code doesn't compile because it has unreachable code. Commented Jun 28, 2018 at 3:00
  • Sorry, I have edited it now. Commented Jun 28, 2018 at 3:03
  • I dont think the assertion is passing. Your execution is not even reaching the assertion. You have a return statement before the assertion call and thus rendering Assert.assertTrue() as dead code. Commented Jun 28, 2018 at 3:16
  • I have edited the code now. The assert fails even if the PDF has the text I specify. Commented Jun 28, 2018 at 3:20
  • 2
    No need to use wildcard, you can try with text.contains in the assert like Assert.assertTrue(text.contains(reqTextInPDF)); Commented Jun 28, 2018 at 6:15

1 Answer 1

1

You can do it this way:

String pdf = "some text contains z inside";
String pdf2 = "some text not contains inside";
System.out.println(pdf.contains("z")); // returns true
System.out.println(pdf2.contains("z")); // returns false

so all you need it is to assert if the statement returns true like this:

Assert.assertTrue("The pdf doesn't contain needed text", text.contains(reqTextInPDF));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.