Actually when you open a .PDF page, e.g: http://192.168.1.2/yourFile.pdf, the browser is served as a PDF viewer instead of an actual webpage. Therefore you can’t “spy” or “record” anything on this PDF file due to this fact.
Hi Vinh,
Please disregard my request for your email (I am guessing you’re pretty busy). I have created a text file with my questions. When time allows please review my questions.
Q2: Please see above solution as this is the action you need for this question
Q3: It is not incorrect somehow. You need to add @Keyword annotation to it, so it will be:
@Keyword
def ReadPDF(String PDFURL)
{
URL TestURL = new URL(PDFURL);
BufferedInputStream TestFile = new BufferedInputStream(TestURL.openStream());
PDFParser TestPDF = new PDFParser(TestFile); //getting "Groovy:unable to resolve class PDFParser" error displays (might be due to not knowing where to reference JAR files in Katalon?)
TestPDF.parse();
String TestText = new PDFTextStripper().getText(TestPDF.getPDDocument()); //getting "Groovy:unable to resolve class PDFParser error" (same as above)
Assert.assertTrue(TestText.contains("Open the setting.xml, you can see it is like this"));
}
Q4: After the above custom keyword, just call it and pass in your PDF URL as an input of this keyword
I am now seeing the following error when trying to use the custom readPDF () keyword:
*See attached files
Test Cases/Check PDFs FAILED because (of) groovy.lang.GroovyRuntimeException:
Could not find matching constructor for: org.apache.pdfbox.pdfparser.PDFParser(java.io.BufferedInputStream)
tools.readPDFs.ReadPDF:30
tools.readPDFs.invokeMethod:0
Test Cases/Check PDFs.run:31
First let me say thanks so much for your help; I’ve learned a lot so far…
I removed the ‘public class’ as you recommended but I am still seeing the “matching constructor” error (not sure what I am doing wrong (have you been able to get this to work)):
I created a brand new simple project & did as you recommended:
Added the custom @Keyword, (see attached keyword screen shot)
Added test case that calls the custom keyword (see attached testCase screen shot)
Result: Test Cases/Check PDFs FAILED because (of) groovy.lang.GroovyRuntimeException: Could not find matching constructor for: org.apache.pdfbox.pdfparser.PDFParser(java.io.BufferedInputStream)
Hi Vinh,
I did some more research and was able to get the pdfbox to work.
When I print the result to the console I am NOW seeing the PDF contents.
Keyword used:
package tools
import org.apache.pdfbox.pdfparser.PDFParser
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.text.PDFTextStripper
import com.kms.katalon.core.annotation.Keyword
@Keyword
def ReadPDF(String PDFURL)
{
URL TestURL = new URL(PDFURL);
BufferedInputStream bis = new BufferedInputStream(TestURL.openStream());
PDDocument doc = PDDocument.load(bis);
String pdfText = new PDFTextStripper().getText(doc);
doc.close();
bis.close();
println(pdfText);
}
However I am not having any luck “Verifying Text Present” using “Assert.assertTrue(pdfText.contains(‘Open the setting.xml, you can see it is like this’))”
How should I validate the text “Open the setting.xml, you can see it is like this” exists in the read in PDF?
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.Keys as Keys
import tools.pdfReader2 as pdfReader2
WebUI.openBrowser('')
WebUI.navigateToUrl('http://www.testingdiaries.com/selenium-webdriver-read-pdf-content/')
WebUI.waitForPageLoad(3)
WebUI.click(findTestObject('PDFchecks/Page_Selenium WebDriver Read PDF Co/a_this location'))
WebUI.waitForPageLoad(3)
CustomKeywords.'tools.pdfReader2.ReadPDF'('http://www.axmag.com/download/pdfurl-guide.pdf')
String Assert = ''
Assert.assertTrue(pdfText.contains('Open the setting.xml, you can see it is like this'))
This now works with the following (although I am wondering how to validate text from the “PDF” in the actual test case)…
Thanks Vinh for pointing me in the right direction to solve this issue
package tools
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.text.PDFTextStripper
import org.testng.Assert
import com.kms.katalon.core.annotation.Keyword
//######################################
//ADDING the ReadPDF() CUSTOM KEYWORD TO ANY PROJECT
//First add the pdfbox JAR file to the test project
//1. Open Apache PDFBox | Download
//2. Download the latest pdfbox JAR file (e.g., pdfbox-2.0.8.jar)
//3. Start Katalon & select “Project > Settings > External Libraries”
//4. Click Add & add the pdfbox-2.0.8.jar libraries to the project & click Apply
//5. Add the following ReadPDF() method custom keyword
//6. After adding the custom keyword press CTRL + SHIFT + O to add the needed libraries
//######################################
//USAGE:
// Call the keyword & pass the PDF URL as the keyword’s input
// Use Assert.assertTrue() to verify the results on the PDF
//######################################
@Keyword
def ReadPDF(String PDFURL)
{
URL TestURL = new URL(PDFURL);
BufferedInputStream bis = new BufferedInputStream(TestURL.openStream());
PDDocument doc = PDDocument.load(bis);
String pdfText = new PDFTextStripper().getText(doc);
doc.close();
bis.close();
println(pdfText);
Assert.assertTrue(pdfText.contains(“Open the setting.xml, you can see it is like this:”));
Assert.assertTrue(pdfText.contains(“Please add the following sentence in setting.xml before”));
Assert.assertTrue(pdfText.contains(“You can see that I have modified the setting.xml, and if open the file in IE, it is like this:”));
println “PDF IS GOOD TO GO…\r”;
}
Hi friend,
I am getting the below error while validating the pdf file. Could you please help me out how to resolve this.
Test Cases/Working_Folder/pdfReader FAILED because (of) org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: org/apache/fontbox/FontBoxFont
Thanks,
Abhishek
Hi friend,
I am getting the below error while validating the pdf file. Could you please help me out how to resolve this.
Test Cases/Working_Folder/pdfReader FAILED because (of) org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: org/apache/fontbox/FontBoxFont
Thanks,
Abhishek
This now works with the following (although I am wondering how to validate text from the “PDF” in the actual test case)…
Thanks Vinh for pointing me in the right direction to solve this issue
package tools
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.text.PDFTextStripper
import org.testng.Assert
import com.kms.katalon.core.annotation.Keyword
//######################################
//ADDING the ReadPDF() CUSTOM KEYWORD TO ANY PROJECT
//First add the pdfbox JAR file to the test project
//1. Open Apache PDFBox | Download
//2. Download the latest pdfbox JAR file (e.g., pdfbox-2.0.8.jar)
//3. Start Katalon & select “Project > Settings > External Libraries”
//4. Click Add & add the pdfbox-2.0.8.jar libraries to the project & click Apply
//5. Add the following ReadPDF() method custom keyword
//6. After adding the custom keyword press CTRL + SHIFT + O to add the needed libraries
//######################################
//USAGE:
// Call the keyword & pass the PDF URL as the keyword’s input
// Use Assert.assertTrue() to verify the results on the PDF
//######################################
@Keyword
def ReadPDF(String PDFURL)
{
URL TestURL = new URL(PDFURL);
BufferedInputStream bis = new BufferedInputStream(TestURL.openStream());
PDDocument doc = PDDocument.load(bis);
String pdfText = new PDFTextStripper().getText(doc);
doc.close();
bis.close();
println(pdfText);
Assert.assertTrue(pdfText.contains(“Open the setting.xml, you can see it is like this:”));
Assert.assertTrue(pdfText.contains(“Please add the following sentence in setting.xml before”));
Assert.assertTrue(pdfText.contains(“You can see that I have modified the setting.xml, and if open the file in IE, it is like this:”));
println “PDF IS GOOD TO GO…\r”;
}
Hi Carlos,
Sorry for the last response… I am not sure why you are seeing that error.
When you use the sample project does that work?
The DNS error may be due to some setup on your side.
Any other Katalon folks have any ideas?