PDFUnit-XML - Typical Examples
PDFUnit-XML can test visible and invisible parts of a PDF document. Text from a PDF page can be processed as text or as a rendered image.
Furthermore, it is possible to compare many properties of a test PDF document against a reference document.
The following examples show only a small part of the capabilities. The manual contains detailed examples of all types of tests. It is available online or can be downloaded as PDF.
Contents on individual PDF pages
<testcase name="hasText_OnIndividualPages">
<assertThat testDocument="&pdfdir;/content/diverseContentOnMultiplePages.pdf">
<hasText onPage="2">
<containing>lorem ipsum</containing>
</hasText>
</assertThat>
</testcase>
Contents in a page region
The following example shows how to define and use a page region. Within the region the text can be checked with all available functions to compare normal text.
<testcase name="hasText_OnFirstPage_InRegion">
<assertThat testDocument="&pdfdir;/content/diverseContentOnMultiplePages.pdf">
<hasText on="FIRST_PAGE" >
<inRegion upperLeftX="17" upperLeftY="45" width="60" height="9" >
<startingWith>lorem</startingWith>
<containing>ipsum</containing>
<endingWith>est laborum.</endingWith>
</inRegion>
</hasText>
</assertThat>
</testcase>
Contents in form fields
Forms are often part of a process chain. So it has to be ensured that the generated form fields are correct. PDFUnit can check many properties of the fields and also the content.
<testcase name="hasFields_WithoutTextOverflow_Fieldname"
errorExpected="YES"
>
<assertThat testDocument="&pdfdir;/acrofields/fieldSizeAndText.pdf">
<hasField withName="Textfield, too much text, multiline:">
<withoutTextOverflow />
</hasField>
</assertThat>
</testcase>
Too much text in a form field?
A form field can contain so much text that it no longer fits
into the field after rendering by a PDF reader.
However, we can test for this situation.
The PDF document used in the following example has such a field with
too much text. So a test failure is expected.
<testcase name="hasFields_WithoutTextOverflow_Fieldname"
errorExpected="YES"
>
<assertThat testDocument="&pdfdir;/acrofields/fieldSizeAndText.pdf">
<hasField withName="Textfield, too much text, multiline:">
<withoutTextOverflow />
</hasField>
</assertThat>
</testcase>
Compare rendered PDF pages with a reference
PDFUnit can compare both the text, as well as a rendered page with a reference document. The following example shows such a comparison which additionally is limited to a section of the first page.
<testcase name="haveSameAppearance_OnFirstPage_InRegion">
<assertThat testDocument="&pdfdir;/test/test.pdf"
masterDocument="&pdfdir;/master/master.pdf"
>
<haveSameAppearance on="FIRST_PAGE">
<inRegion upperLeftX="20" upperLeftY="20" width="180" height="40" />
</haveSameAppearance>
</assertThat>
</testcase>
More examples
The manual contains detailed examples for all types of tests. It is available online or can be downloaded as PDF.