Reading PDF in Chrome Browser Tab and verify text

Hi as part of our proof of concept for Katalon we need to prove we can verify text on a generated PDF which opens in a new tab.
The URL of the PDF is variable each time the document is generated:
I’ve tried using the switch to new tab and then get.url however the issue is I don’t know how to convert the PDF into text then use the verify text function.
Below is the current test case code I have:
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 static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
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 com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import com.github.javafaker.Faker as Faker
import org.apache.pdfbox.pdmodel.PDDocument as PDDocument
import org.apache.pdfbox.text.PDFTextStripper as PDFTextStripper
import org.apache.pdfbox.text.PDFTextStripperByArea as PDFTextStripperByArea
import com.kms.katalon.core.util.KeywordUtil as KeywordUtil
import java.io.BufferedInputStream as BufferedInputStream
import java.io.File as File
import java.io.RandomAccessFile as RandomAccessFile
import java.net.URL as URL

Faker faker = new Faker(new Locale(‘en-GB’))

WebUI.waitForElementPresent(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_SelectSPIDs/input_BusinessName’),
0)

WebUI.setText(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_SelectSPIDs/input_BusinessName’), faker.instance().company().name())

WebUI.setText(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_SelectSPIDs/input_TelephoneNumber’), ‘01’ + faker.instance().number().digits(
9))

WebUI.setText(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_SelectSPIDs/input_ContactName’), faker.instance().name().fullName())

WebUI.setText(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_SelectSPIDs/input_ContactEmail’), faker.instance().internet().safeEmailAddress())

WebUI.setText(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_SelectSPIDs/input_SelectSPIDs’), SPIDValue)

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_SelectSPIDs/select_TopSPID’, [(‘variable’) : SPIDValue]))

WebUI.waitForElementClickable(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_QuoteDetails/button_PreviewQuote’),
0)

custName = WebUI.getText(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_SelectSPIDs/text_CustomerName’))

println(custName)

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/Common/button_Next’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_QuoteDetails/input_SentToCustomer’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_QuoteDetails/button_AcceptQuote’))

WebUI.verifyElementText(findTestObject(‘Page_Everflow Eclipse_Quote/Common/alert_TextValue’), ‘Quote has been accepted by the customer.’)

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/Common/button_Next’))

WebUI.waitForElementVisible(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_BillingDetails/input_RegNumberValue’),
0)

WebUI.setText(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_BillingDetails/input_RegNumberValue’), custName)

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_BillingDetails/input_SPIDAddressAsBilling’))

WebUI.setText(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_BillingDetails/input_BillingEmail’), faker.instance().internet().safeEmailAddress())

WebUI.setText(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_BillingDetails/input_BankName’), ‘Test Bank PLC’)

WebUI.setText(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_BillingDetails/input_AccountNumber’), ‘11112222’)

WebUI.setText(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_BillingDetails/input_SortCode’), ‘112233’)

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/Common/button_Next’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/input_ContractSentToCustomer’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/input_UseVerbalContract’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/button_Download PDF’))

WebUI.switchToWindowIndex(‘1’)

WebUI.getUrl()

PDF Check will need to happen here

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/a_OpenVerbalScript’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/input_Obtain1stVerbalConfirmation’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/input_Obtain2ndVerbalConfirmation’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/input_Obtain3rdVerbalConfirmation’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/input_Obtain4thVerbalConfirmation’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/input_Obtain5thVerbalConfirmation’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/input_ObtainSpellingConfirmation’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/input_ReceiveVerbalDDConfirmation’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/input_YesRequiresVAT’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/button_Confirm Contract’))

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_GenerateContract/button_AcceptQuoteAndContract’))

This is not something I’ve ever done before and we only have a few days left of our trial licence to prove this so any help will be appreciated

Thanks

@christian

You should download the file and use PDFBox to extract text then verify the contains.

Hi,

Thanks for the reply.

The issue with downloading the document is that we are planning to run this across multiple machines so we are not always sure of the download location and each PDF that opens has a dynamic name which is generated at creation.

I think I almost have a way of reading it in browser using the PDFbox you mentioned after reading some other pages.

I think my issue now is when I click the pdf it opens in a new tab but I need to grab the URL once the document has finished loading (The first url on the new tab is a Pdf loading url then it changes to a dynamic url which is different everytime.

Here is the code I have in the test case

WebUI.click(findTestObject(‘Page_Everflow Eclipse_Quote/QuoteGenerator_QuoteDetails/button_DownloadPDF’))

WebUI.switchToWindowIndex(1)

WebUI.enableSmartWait()

Path = WebUI.getUrl()

CustomKeywords.‘com.database.QuotePDF.ReadPDF’(Path)

And in my groovy for the PDF reader:

package com.database

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);
}