3.20.  Page Numbers as Objectives

Overview

It is sometimes useful to check if a generated PDF document has exactly one page. Or maybe you want to ensure that a document has less than 6 pages, because otherwise you have to pay higher postage. PDFUnit provides suitable tags:

<!-- Tags to verify page numbers: -->

<hasNumberOfPages />
<hasLessPages than=".."  (required)
/>
<hasMorePages than=".."  (required)
/>

Examples

You can check the number of pages like this:

<testcase name="hasNumberOfPages">
  <assertThat testDocument="format/format_Letter-Portrait.pdf">
    <hasNumberOfPages>1</hasNumberOfPages>
  </assertThat>
</testcase>

Tests are also possible with a minimum or maximum number of pages.

<testcase name="hasNumberOfPagesLessThan">
  <assertThat testDocument="format/format_multiple-formats-on-individual-pages.pdf">
    <hasLessPages than="6" /> <!-- The document has 5 pages. -->
  </assertThat>
</testcase>
<testcase name="hasMorePagesThan">
  <assertThat testDocument="format/format_multiple-formats-on-individual-pages.pdf">
    <hasMorePages than="2" /> <!-- The document has 5 pages. -->
  </assertThat>
</testcase>

The values for upper- and lower limits are exclusive.

Both limits can be checked with one test:

<!-- 
  Validating that a document has a number of pages in a allowed range.
-->
<testcase name="hasNumberOfPages_InRange">
  <assertThat testDocument="format/format_multiple-formats-on-individual-pages.pdf">
    <hasMorePages than="2" />
    <hasLessPages than="8" />
  </assertThat>
</testcase>

Don't omit tests with page numbers just because you might think they are too simple. Experience shows that you can find errors in the context of a primitive test that you would not have found without the test.