3.33.  Text - Ordered Text


Sometimes it is necessary to check that certain text in a PDF document comes before other text. PDFUnit provides the following methods for such cases:

// Testing ordered text:
.hasText().first(text1, WhitespaceProcessing).then(text2)...

.hasText().inOrder(text1, text2, ...)
.hasText().inOrder(WhitespaceProcessing, text1, text2, ...)
.hasText().inOrder(WhitespaceProcessing, text[])

Example - hasText() ... first() ... then()

The following example checks the existence of three texts in order: first the title of chapter 5, then the body of chapter 5 and lastly the title of chapter 6. Validation is restricted to page 2:

public void hasTextInOrder_FirstThen() throws Exception {
  String filename = "documentUnderTest.pdf";
  String titleChapter5 = "Chapter 5";
  String textChapter5 = "Yours truly, Huck Finn";
  String titleChapter6 = "Chapter 6";
  PagesToUse page2 = PagesToUse.getPage(2);
  int leftX  =  18;  // in millimeter
  int upperY =  80;
  int width  = 180;
  int height = 100; 
  PageRegion regionWithoutHeaderAndFooter = new PageRegion(leftX, upperY, width, height);

Internally PDFUnit uses the method containing(..) to look for the text. Whitespaces are normalized prior to comparison, unless a whitespace-processing parameter is passed to the method first(expectedText, WhitespaceProcessing). All subsequent methods then() use the same kind of whitespace processing.

The method then(..) can be repeated multiple times.

Example - hasText().inOrder(..)

The next example fulfills the same test objective, but the expected texts are passed to a method as an array:

public void hasTextInOrder_WithStringArray() throws Exception {
  String filename = "documentUnderTest.pdf";
  String titleChapter2 = "Chapter 2";
  String textChapter2 = "Call me Ishmael";
  String titleChapter3 = "Chapter 3";

            .inOrder(titleChapter2, textChapter2, titleChapter3)

The order of the search string parameters must correspond to the order of the strings in the PDF document.

Again, PDFUnit uses the method contains(..) internally and whitespaces will be normalized.

Different whitespace handling can be achieved by giving the desired whitespace-processing as a parameter to the method inOrder(WhitespaceProcessing, expectedText[]).

Search strings can also be given to the methods as a string array.