how to verify pdf text in selenium TestNG?
To verify PDF text in Selenium TestNG, you can use the Apache PDFBox library. Here are the steps to verify PDF text in Selenium TestNG:
- Add the Apache PDFBox dependency to your project. You can do this by adding the following dependency to your pom.xml file:
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.24</version> </dependency>
- Open the PDF file using the PDFBox library. You can do this using the following code:
PDDocument document = PDDocument.load(new File("path/to/pdf/file"));
- Extract the text from the PDF document using the PDFBox library. You can do this using the following code:
PDFTextStripper pdfStripper = new PDFTextStripper(); String text = pdfStripper.getText(document);
- Verify the text using TestNG assertions. For example, you can use the following code to verify that the PDF text contains a specific string:
Assert.assertTrue(text.contains("expected text"));
- Close the PDF document using the PDFBox library. You can do this using the following code:
document.close();
Here is an example test method that verifies the text in a PDF file:
Dhaval Shah public void testVerifyPdfText() throws IOException { PDDocument document = PDDocument.load(new File("path/to/pdf/file")); PDFTextStripper pdfStripper = new PDFTextStripper(); String text = pdfStripper.getText(document); Assert.assertTrue(text.contains("expected text")); document.close(); }
Note that you may need to handle exceptions that can occur while opening or reading the PDF file.
For More Blogs
Author: Ghulam Nabi
Leave a comment