Created
September 10, 2020 14:46
-
-
Save muditlambda/02494f1cceb5916cd27a893e0fd160a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Automation; | |
import java.io.BufferedInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import org.apache.pdfbox.pdmodel.PDDocument; | |
import org.apache.pdfbox.text.PDFTextStripper; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import org.testng.Assert; | |
import org.testng.annotations.AfterTest; | |
import org.testng.annotations.BeforeTest; | |
import org.testng.annotations.Test; | |
public class PdfHandling { | |
WebDriver driver = null; | |
@BeforeTest | |
public void setUp() { | |
//specify the location of the driver | |
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Shalini\\Downloads\\Driver\\chromedriver.exe"); | |
//instantiate the driver | |
driver = new ChromeDriver(); | |
} | |
@Test | |
public void verifyContentInPDf() { | |
//specify the url of the pdf file | |
String url ="http://www.pdf995.com/samples/pdf.pdf"; | |
driver.get(url); | |
try { | |
String pdfContent = readPdfContent(url); | |
Assert.assertTrue(pdfContent.contains("The Pdf995 Suite offers the following features")); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@AfterTest | |
public void tearDown() { | |
driver.quit(); | |
} | |
public static String readPdfContent(String url) throws IOException { | |
URL pdfUrl = new URL(url); | |
InputStream in = pdfUrl.openStream(); | |
BufferedInputStream bf = new BufferedInputStream(in); | |
PDDocument doc = PDDocument.load(bf); | |
int numberOfPages = getPageCount(doc); | |
System.out.println("The total number of pages "+numberOfPages); | |
String content = new PDFTextStripper().getText(doc); | |
doc.close(); | |
return content; | |
} | |
public static int getPageCount(PDDocument doc) { | |
//get the total number of pages in the pdf document | |
int pageCount = doc.getNumberOfPages(); | |
return pageCount; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment