Thursday, July 2, 2015

SELENIUM : EASY TO VERIFY PDF FILE TEXT USING JAVA WEBDRIVER

In this post we will explain the procedure to verify PDF file content using java webdriver. As  some time we need to verify content of web application pdf file, opened in browser.
Use below code in your test scripts to get pdf file content.

 //get current urlpdf file url
 URL url = new URL(driver.getCurrentUrl());
                 
 //create buffer reader object
 BufferedInputStream fileToParse = new BufferedInputStream(url.openStream());
 PDFParser pdfParser = new PDFParser(fileToParse);
 pdfParser.parse();

 //save pdf text into strong variable
 String pdftxt = new PDFTextStripper().getText(pdfParser.getPDDocument());
                 
 //close PDFParser object
 pdfParser.getPDDocument().close();

After applying above code, you can store all pdf file content into “pdftxt” string variable. Now you can verify string by giving  in put.  As if you want to verify “Selenium Or Webdiver” text. Use below code.
                 
Assert.assertTrue(pdftxt.contains(“Selenium Or Webdiver”))

Hope this post will help  to verify web application PDF content.


Credits: roadtoautomation blog

No comments:

Post a Comment