diff --git a/src/main/java/com/testautomationguru/utility/PDFUtil.java b/src/main/java/com/testautomationguru/utility/PDFUtil.java index 7cbb928..c014232 100644 --- a/src/main/java/com/testautomationguru/utility/PDFUtil.java +++ b/src/main/java/com/testautomationguru/utility/PDFUtil.java @@ -443,9 +443,9 @@ private boolean comparePdfByImage(String file1, String file2, int startPage, int int pgCount1 = this.getPageCount(file1); int pgCount2 = this.getPageCount(file2); - - if(pgCount1!=pgCount2){ - logger.warning("files page counts do not match - returning false"); + + if(!isPartialComparisonPossible(pgCount1, pgCount2, endPage)){ + logger.warning("partial comparison not possible - returning false"); return false; } @@ -457,6 +457,23 @@ private boolean comparePdfByImage(String file1, String file2, int startPage, int return this.convertToImageAndCompare(file1, file2, this.startPage, this.endPage); } + + private boolean isPartialComparisonPossible(int pageCountFile1, int pageCountFile2, int endPage) { + + if (pageCountFile1 != pageCountFile2) { + logger.warning("files page counts do not match"); + boolean isPartialComparisonPossible = pageCountFile1 >= endPage && pageCountFile2 >= endPage; + if (isPartialComparisonPossible) { + logger.warning("partial comparison possible"); + return true; + } else { + return false; + } + } else { + return true; + } + } + private boolean convertToImageAndCompare(String file1, String file2, int startPage, int endPage) throws IOException{ boolean result = true; diff --git a/src/test/java/com/testautomationguru/utility/PDFUtilTest.java b/src/test/java/com/testautomationguru/utility/PDFUtilTest.java index 524f4ee..ee0bf87 100644 --- a/src/test/java/com/testautomationguru/utility/PDFUtilTest.java +++ b/src/test/java/com/testautomationguru/utility/PDFUtilTest.java @@ -105,6 +105,26 @@ public void comparePDFImageModeDiffSpecificPage() throws IOException { Assert.assertTrue(result); } + @Test(priority = 11) + public void comparePDFImageModePartial() throws IOException { + String file1 = getFilePath("image-compare-partial/sample1.pdf"); + String file2 = getFilePath("image-compare-partial/sample2.pdf"); + pdfutil.setCompareMode(CompareMode.VISUAL_MODE); + + boolean result = pdfutil.compare(file1, file2, 1, 1); + Assert.assertTrue(result); + } + + @Test(priority = 12) + public void comparePDFImageModePartialNotPossible() throws IOException { + String file1 = getFilePath("image-compare-partial/sample1.pdf"); + String file2 = getFilePath("image-compare-partial/sample2.pdf"); + pdfutil.setCompareMode(CompareMode.VISUAL_MODE); + + boolean result = pdfutil.compare(file1, file2, 1, 2); + Assert.assertFalse(result); + } + private String getFilePath(String filename) { return new File(getClass().getClassLoader().getResource(filename).getFile()).getAbsolutePath(); } diff --git a/src/test/resources/image-compare-partial/sample1.pdf b/src/test/resources/image-compare-partial/sample1.pdf new file mode 100644 index 0000000..e3addac Binary files /dev/null and b/src/test/resources/image-compare-partial/sample1.pdf differ diff --git a/src/test/resources/image-compare-partial/sample2.pdf b/src/test/resources/image-compare-partial/sample2.pdf new file mode 100644 index 0000000..838d8db Binary files /dev/null and b/src/test/resources/image-compare-partial/sample2.pdf differ