Hi,
.After using Instream also i am getting the error Reason:
groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.io.File(java.io.BufferedInputStream)
But i am unable to print the Content of the PDF.
These are the logs.
hi,
use this as it is and give argument as String path where your pdf file locate
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
public List<String> openPfdFile(String pdfFile){
List<String> lines = new ArrayList<>()
//PDDocument pdfDocument = PDDocument.load(new File("C:/xxxx/xxxx/Desktop/data/file2.pdf"))
PDDocument pdfDocument = PDDocument.load(new File(pdfFile))
pdfDocument.getClass();
if (!pdfDocument.isEncrypted()) {
PDFTextStripperByArea pdfTextStripperByArea = new PDFTextStripperByArea();
pdfTextStripperByArea.setSortByPosition(Boolean.TRUE);
PDFTextStripper pdfTextStripper = new PDFTextStripper();
String pdfFileInText = pdfTextStripper.getText(pdfDocument);
lines = pdfFileInText.split("\\r?\\n");
for (String line : lines) {
System.out.println(line);
}
}
return lines
}
I am using this code in my test case itself do i have to use with custom Keyword?
Because in my Test case it is not recognizing the entire code if i use first 2 lines:
public List openPfdFile(String value1){
List lines = new ArrayList<>()
no needed to use custom keyword
all code could be in test script window too, but it’s coming messy that way
the trick is there that method will return String List
content is splitted here to array
lines = pdfFileInText.split("\r?\n");
Hi, i am using below code:
When i hit the Preview Button it switch to next window and in the String Value1 the URL is saved . that URL i am passing in
WebUI.click(findTestObject(‘Object Repository/New_Order/Page_Quoting Workbench (omsendSalesOrder)/input_Preview_SO’))
WebUI.switchToWindowIndex(1)
String value1 = WebUI.getUrl()
WebUI.comment(value1)
//verify the PDF Content//
public List openPfdFile(String value1){
List lines = new ArrayList<>()
URL url = new URL(value1);
InputStream inStream= url.openStream();
BufferedInputStream fileParse = new BufferedInputStream(inStream);
//PDDocument document = null;
//document = PDDocument.load(fileParse);
//PDDocument pdfDocument = PDDocument.load(new File(“C:/Users/RAgrawal/Downloads/quote.pdf”))
PDDocument pdfDocument = PDDocument.load(new File(value1))
pdfDocument.getClass();
if (!pdfDocument.isEncrypted()) {
PDFTextStripperByArea pdfTextStripperByArea = new PDFTextStripperByArea();
pdfTextStripperByArea.setSortByPosition(Boolean.TRUE);
PDFTextStripper pdfTextStripper = new PDFTextStripper();
String pdfFileInText = pdfTextStripper.getText(pdfDocument);
lines = pdfFileInText.split("\\r?\\n");
for (String line : lines) {
System.out.println(line);
}
}
return lines
}
WebUI.delay(10)
WebUI.switchToWindowIndex(0)
WebUI.delay(5)
not_run: WebUI.switchToWindowUrl(‘’)
WebUI.click(findTestObject(‘Object Repository/New_Order/Page_Quoting Workbench (omsendSalesOrder)/input_sendEmail’))
String emailmsg = WebUI.getText(findTestObject('Object Repository/Page_Billing Workbench/li_Email sent to '))
WebUI.comment(emailmsg)
But i am not getting anything in console related to that Particular code:
Here are the Logs:
and this variable value1 value is path where the .pdf file is downloaded?
like this
C:/Users/RAgrawal/Downloads/quote.pdf
It is not gettting Download…It opens in new Window…I did settings manually in my Chrome Browser on Desktop but still it is opening in next window…
print the value1
ok,
got it, wait a bit
TESTCASE:
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.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
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.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
String value1 = "http://www.vandevenbv.nl/dynamics/modules/SFIL0200/view.php?fil_Id=5515"
def pdf = CustomKeywords.'com.pdf.reader.ReadPdfFromBrowser.PdfReaderUtil'(value1)
def lines = pdf.split("\\r?\\n");
for (String line : lines) {
System.out.println(line);
}
KEYWORD:
package com.pdf.reader
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.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable
import java.io.BufferedInputStream;
import java.io.File;
import java.io.RandomAccessFile;
import java.net.URL;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ReadPdfFromBrowser {
WebDriver driver;
PDDocument pdDoc;
@Keyword
public String PdfReaderUtil(String html){
String pdfFileInText = "";
driver=new ChromeDriver();
driver.get(html);
Thread.sleep(5000);
URL url = new URL(driver.getCurrentUrl());
BufferedInputStream fileToParse = new BufferedInputStream(
url.openStream());
pdDoc = PDDocument.load(fileToParse);
pdDoc.getClass();
if (!pdDoc.isEncrypted()) {
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripper tStripper = new PDFTextStripper();
pdfFileInText = tStripper.getText(pdDoc);
}
driver.close();
return pdfFileInText;
}
}
Hi,
Here is the thing, The moment it clicks Preview Button it switch to Window index 1 and there the PDF gets Open…
I saw in your custom Keyword you gave driver = new chrome driver
This is what its doing it closes the current browser and try to relogin again with different test case.
I want to Perform the Activity when it switches to Window 1.
Can you please help me again…I really Appreciate your Help.
hi,
here is my opinion
when pdf window is open get the url as you are already done
not needed to switch windows up and down
did you tried with my suggestion?
once more with the road…
WebUI.openBrowser('')
WebUI.navigateToUrl('http://www.testingdiaries.com/selenium-webdriver-read-pdf-content/')
WebUI.click(findTestObject('Page_SeleniumWebDriverRead-PDF-Co/a_this-location'))//click pdf link button
WebUI.delay(5)
WebUI.switchToWindowIndex(1) //pdf file url here
url = WebUI.getUrl() //get url
println ("***DEBUG URL*** "+url)
String pdfContent = readPdfFile(url)
//String pdfContent = CustomKeywords.'readPdfFile.verifyPdfContent.readPdfFile'(url)
Assert.assertTrue(pdfContent.contains('Open the setting.xml, you can see it is like this:'))
Assert.assertTrue(pdfContent.contains('Please add the following sentence in setting.xml before'))
Assert.assertTrue(pdfContent.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')
WebUI.switchToWindowIndex(0) //back to main window
WebUI.closeBrowser()
public String readPdfFile(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);
return pdfText;
}
that’s all what I can do at the this moment
Hi, Thanks for the response really appreciate your effort…
I have use this code what you gave me
WebUI.click(findTestObject(‘Object Repository/New_Order/Page_Quoting Workbench (omsendSalesOrder)/input_Preview_SO’))
WebUI.switchToWindowIndex(1)
String value1 = WebUI.getUrl()
WebUI.comment(value1)
public String readPdfFile(String value1){
URL TestURL = new URL(value1);
BufferedInputStream bis = new BufferedInputStream(TestURL.openStream());
PDDocument doc = PDDocument.load(bis);
String pdfText = new PDFTextStripper().getText(doc);
doc.close();
bis.close();
println(pdfText);
return pdfText;
}
String pdfContent = readPdfFile(value1)
//def pdf = CustomKeywords.‘com.pdf.reader.ReadPdfFromBrowser.PdfReaderUtil’(value1)
Assert.assertTrue(pdfContent.contains(‘Local’))
I got the error: End of File Exepected Line
Whenever you have time if you can help me in resolving i will be thankful to you…
Thanks for the Help
hello,
copy that url from address bar to variable
String var1 = “url copied from address bar”
call function passing argument var1
readPdfFile(var1)
//print here var1
println ("DEBUG var1 "+var1)
AND
move the function before or after tests lines
now it’s in middle of your test script
you can also use Katalon Debug mode
set you breakpoint (click or double click in a grey area next to line numbers)
URL TestURL = new URL(value1);