How to verify corresponding PDF content in Katalon

Hi Community!

I am trying to test PDF content using Katalon but I am not sure how to achieve this.

My test steps would be -

  1. Click on a link (of PDF file)
  2. PDF will open in a new browser window
  3. The PDF will have a table something like the following

I want to verify if the outcome of corresponding rule is correct. The expected outcome will be stored somewhere else(Test data file).
For example -
Expected outcome of CR001 - GREEN (Scenario Passed)
Expected outcome of CR002 - GREEN (But actual - YELLOW, so failed)
I am able to open and parse the PDF content using apache PdfBox. Can someone please help me in verifying the corresponding rule outcome.

hi,

if needed to find out outcome as YELLOW here snip for it

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 org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;

import com.testautomationguru.utility.PDFUtil;

import com.kms.katalon.core.util.KeywordUtil
import java.util.regex.Matcher;
import java.util.regex.Pattern;


KeywordUtil logger = new KeywordUtil()

String pdfFilePath = System.getProperty("user.dir")+"\\pdfFiles\\pdfcontent.pdf";

String text = ""
PDDocument document = PDDocument.load(new File(pdfFilePath));
if (!document.isEncrypted()) {
	PDFTextStripper stripper = new PDFTextStripper();
	text = stripper.getText(document);
	System.out.println("Text:" + text);
}
document.close();

def lines = text.split("(\r\n|\r|\n)", -1);

String pattern = '^.*Person ([\\S\\s])([\\S\\s])([\\S\\s]+)';
String searchString = "YELLOW"
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
for(String line:lines){
	Matcher m = r.matcher(line);
	if (m.find( )) {
	   System.out.println("Found value: " + m.group(0) );
	   System.out.println("Found value: " + m.group(1) );
	   System.out.println("Found value: " + m.group(2) );
	   System.out.println("Found value: " + m.group(3) );
	   if (m.group(3).replaceAll("\\s","").equals(searchString)){
		   logger.markWarning("value is wrong should be GREEN")
	   }
	}else {
	   System.out.println("NO MATCH");
	}
}

Hi @Timo_Kuisma1,

Thank you for your answer but this is not exactly what I am trying to achieve. I am trying to test all ‘Rule’ with corresponding ‘Outcome’. I am not sure, whether your code will be able to achieve this.
I think, the best solution would be to convert the PDF file to HTML and then find the corresponding result.

hi,
my example will use regex to find out column Outcome content and will verify it with given search string, note it’s only example not the answer what you are looking for, but if you have coding skills then you can take some piece of code and implement your own code.
Still wonder why should convert to HTML, not needed at all

@Timo_Kuisma1
Hi thank you and I think you are correct. I do not need to convert it to html. Also, I am not so good at coding but I will dig deeper into your example and try to use it as a reference in my code. If I am unable to do so, now I know who can help me on that :wink:

Thanks again!

hello,

will do it to you tomorrow :slight_smile:

hi,
here

import static com.google.common.collect.MapDifference.ValueDifference;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.MapDifference
import com.google.common.collect.Maps
import com.google.common.collect.MapDifference.*;
import com.kms.katalon.core.util.KeywordUtil


KeywordUtil logger = new KeywordUtil()

String pdfFilePath = System.getProperty("user.dir")+"\\pdfFiles\\pdfcontent.pdf";

String text = ""
PDDocument document = PDDocument.load(new File(pdfFilePath));
if (!document.isEncrypted()) {
	PDFTextStripper stripper = new PDFTextStripper();
	text = stripper.getText(document);
}
document.close();

def lines = text.split("(\r\n|\r|\n)", -1);

//regex pattern to find out Rule and Outcome
String pattern = '([CR\\d]+) * Description ([\\S\\s]) Person ([\\S\\s])([\\S\\s]+)';
String rule = ""
String outcome = ""
Map<String, String> rulesOutcomes = new HashMap<>();

// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
for(String line:lines){
	Matcher m = r.matcher(line);
	if (m.find( )) {
	   System.out.println("Found value: " + m.group(1) ); //rule
	   rule = m.group(1).replaceAll("\\s","")
	   System.out.println("Found value: " + m1.group(4) ); //outcome
	   outcome = m.group(4).replaceAll("\\s","")
	   rulesOutcomes.put(rule, outcome)
	}else {
	   System.out.println("NO MATCH");
	}
}

//define map where are expected key and value pair
Map<String, String> expectedValues = new HashMap<>();
expectedValues.put("CR001","GREEN")
expectedValues.put("CR002","GREEN")

//compare maps
assertFalse("Maps should be unequal", MapDiffUtil.validateEqual(
	rulesOutcomes, expectedValues, "map1", "map2"));
	
/**
 * Map comparison with detailed log messages
 */
public class MapDiffUtil {
	
	private static KeywordUtil logg = new KeywordUtil()

	private static final Logger log =
			LoggerFactory.getLogger(MapDiffUtil.class);

	public static <K, V> boolean validateEqual(
			Map<K, V> map1, Map<K, V> map2,
			String map1Name, String map2Name) {

		final MapDifference<K, V> diff = Maps.difference(map1, map2);

		if (diff.areEqual()) {
			def error = "Maps "+map1Name+" and "+map2Name+" contain exactly the same name/value pairs"
			logg.markWarning(error)
			//log.info("Maps '{}' and '{}' contain exactly the same name/value pairs", map1Name, map2Name);
			return true;

		} else {
			logKeys(diff.entriesOnlyOnLeft(), map1Name, map2Name);
			logKeys(diff.entriesOnlyOnRight(), map2Name, map1Name);
			logEntries(diff.entriesDiffering(), map1Name, map2Name);
			return false;
		}
	}

	private static <K, V> void logKeys(
			Map<K, V> mapSubset, String n1, String n2) {
		if (not(mapSubset.isEmpty())) {
			logg.markWarning("Keys found in "+n1+" but not in "+n2+" : "+mapSubset.keySet());
			//log.error("Keys found in {} but not in {}: {}",n1, n2, mapSubset.keySet());
		}
	}

	private static <K, V> void logEntries(
			Map<K, ValueDifference<V>> differing,
			String n1, String n2) {
		if (not(differing.isEmpty())) {
			logg.markWarning("Differing values found {key="+n1+"-value,"+n2+"-value}: "+differing);
			//log.error("Differing values found {key={}-value,{}-value}: {}",	n1, n2, differing);
		}
	}

	private static boolean not(boolean b) {
		return !b;
	}
}
	


2020-03-08 11:57:42.914 WARN  com.kms.katalon.core.util.KeywordUtil    - Differing values found {key=map1-value,map2-value}: [CR002:(YELLOW, GREEN)]