13.6.  Single and Double Quotation Marks inside Strings

Different Types of Quotes

Important Notice: The term Quote has different meanings as the following picture shows:

“English“ and „German“ style quotation marks do not disturb the execution of tests. But during the creation of a test you could have problems typing them with your editor. Hint: copy the required quotation marks from a word-processor or an exisiting PDF document and paste them into your file.

The "programmers double quotes" need special attention because they are used as string delimiters in Java. The following paragraphs and examples go into detail about this. They are all based on the following document:

Valid Examples

'Single-Quotes', “English“, and „German“ quotation marks within strings cause no problems. But "Double-Quotes" have to be escaped with a backslash:

@Test
public void hasText_SingleQuotes() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  String expected = "Example 1: 'single quotes'";
  AssertThat.document(filename)
            .restrictedTo(FIRST_PAGE)
            .hasText()
            .containing(expected)
  ;
}
@Test
public void hasText_GermanDoubleQuotes() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  String expected = "Example 2: „German double quotes“";
  AssertThat.document(filename)
            .restrictedTo(FIRST_PAGE)
            .hasText()
            .containing(expected)
  ;
}
@Test
public void hasText_EnglishDoubleQuotes() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  String expected = "Example 3: “English double quotes“";
  AssertThat.document(filename)
            .restrictedTo(FIRST_PAGE)
            .hasText()
            .containing(expected)
  ;
}
@Test
public void hasText_ProgrammersDoubleQuotes() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  String expected = "Example 4: \"programmers double quotes\"";
  AssertThat.document(filename)
            .restrictedTo(FIRST_PAGE)
            .hasText()
            .containing(expected)
  ;
}
@Test
public void hasText_DoubleAndSingleQuotes_1() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  String expected = "Example 5: \"double quotes outside, 'single quotes' inside\"";
  AssertThat.document(filename)
            .restrictedTo(FIRST_PAGE)
            .hasText()
            .containing(expected)
  ;
}
@Test
public void hasText_DoubleAndSingleQuotes_2() throws Exception {
  String filename = "documentUnderTest.pdf";
  String expected = "Example 6: 'single quotes outside, \"double quotes\" inside'";
  
  AssertThat.document(filename)
            .restrictedTo(FIRST_PAGE)
            .hasText()
            .containing(expected)
  ;
}
@Test
public void matchingRegex_DoubleQuotes() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  AssertThat.document(filename)
            .restrictedTo(FIRST_PAGE)
            .hasText()
            .matchingRegex(".*\"double.*\".*")
  ;
}

Quotes in XPath

Strings which are used as XPath expressions must not contain both single and double quotes in any combination. This condition is not fulfilled in the following example:

@Test
public void matchingXPath_DoubleQuotes_1() throws Exception {
  String filename = "documentUnderTest.pdf";
  String xpath = "count(//Title[.='\"Double Quote\"-Chapter']) = 1";
  XPathExpression xpathExpression = new XPathExpression(xpath);
  
  AssertThat.document(filename)
            .hasBookmarks()
            .matchingXPath(xpathExpression)
  ;
}

The error can only be avoided when you improve the XPath-expression like this:

@Test
public void matchingXPath_DoubleQuotes_2() throws Exception {
  String filename = "documentUnderTest.pdf";
  String xpath1 = "count(//Title[contains(., 'Double Quote')]) = 1";
  String xpath2 = "count(//Title[contains(., 'Chapter')]) = 4";
  XPathExpression xpathExpression1 = new XPathExpression(xpath1);
  XPathExpression xpathExpression2 = new XPathExpression(xpath2);
  
  AssertThat.document(filename)
            .hasBookmarks()
            .matchingXPath(xpathExpression1)
            .matchingXPath(xpathExpression2)
  ;
}