Let's assume you have a some programs that generates PDF documents and you want to make sure that the programs do what they should. Further expect that your test document has exactly one page and contains the greeting “Thank you for using our services.” and the value “30.34 Euro” for the total bill. Then you check these requirements with PDFUnit as follows:
use strict;
use utf8;
use warnings;
use PDF::PDFUnit;
use Test::Exception;
use Test::More;
my $resources_dir = ...; # let it point to the base folder of your test data
my $pdfUnderTest = "$resources_dir/quickstart/quickstartDemo_de.pdf";
#
# Testcase 'has one page'
#
lives_ok {
AssertThat
->document($pdfUnderTest)
->hasNumberOfPages(1)
;
} "has one page";
lives_ok {
my $expectedGreeting = "Thank you for using our services.";
AssertThat->document($pdfUnderTest)
->restrictedTo(LAST_PAGE)
->hasText()
->containing($expectedGreeting)
;
} "has greeting";
#
# Testcase 'has expected charge'
#
lives_ok {
my $upperLeftX = 172; # in millimeter
my $upperLeftY = 178;
my $width = 20;
my $height = 9;
my $regionCharge = PageRegion->new($upperLeftX, $upperLeftY, $width, $height);
AssertThat->document($pdfUnderTest)
->restrictedTo(LAST_PAGE)
->restrictedTo($regionCharge)
->hasText()
->containing('29.89') # Let's see an error message. Correct value: 30.34
;
} "has expected charge";
diag $@->getMessage() if $@;
done_testing();
The statements diag $@->getMessage() if $@
makes error messages
from PDFUnit-Java visible. For the intended error in the last test case the
error message looks as follows:
# Failed test 'has expected charge' # at C:/.../examples_quickstart-de.t line 67. # died: main::com::pdfunit::errors::PDFUnitValidationException=HASH(0x4055938) # Page(s) [1] of 'C:\...\quickstartDemo_de.pdf' # not containing the expected text: '29,89 Euro'. Found: '30,34 Euro'.
That's it. The following chapters describe the features, typical test scenarios and problems when testing PDF documents.