XPathΒΆ

XPath assertions can be evaluated against payloads of both request and response variables offered by the Touchstone Rules-Engine. Both XPath assertions and JsonPath assertions run significantly faster than FHIRPath assertions. Separate XPath and JSONPath expressions are needed though for XML and JSON content while only one FHIRPath expression is needed for both XML and JSON content.

  • assertXPathContains(xpath, expectedValue)

    • Asserts that the evaluated value of the provided xpath expression contains the provided expectedValue.

    • Example:

      // Asserts that the value of the family element of the second entry contains 'Chalmers'
      response.assertXPathContains("entry[2]/resource/Patient/name/family", "Chalmers")
      
    • Equivalent to each of these:

      • assertXPathContains("entry[2]/resource/Patient/name/family", "Chalmers", response)
        
      • // xPath, expectedValue, and operator can be passed as parameters from test script.
        assertXPath("entry[2]/resource/Patient/name/family", "Chalmers", "contains", response)
        
      • def family = response.getXPathValue("entry[2]/resource/Patient/name/family")
        
        assert family.contains("Chalmers"): "The actual value \""+family+"\" did not contain the expected value
           \"Chalmers\" for \"entry[2]/resource/Patient/name/family\" in response."
        
      • def family = response.xPath("entry[2]/resource/Patient/name/family").value
        
        assert contains("Chalmers", family): "The actual value \""+family+"\" did not contain the expected value
           \"Chalmers\"  for \"entry[2]/resource/Patient/name/family\" in response."
        

        Notice how the value of XPath evaluation can be stored in a variable. This is useful when you want to develop more complicated rule scripts where the assertions involve multiple comparisons.

  • assertXPathNotContains(xpath, expectedValue)

    • Asserts that the evaluated value of the provided xpath expression does not contain the provided expectedValue.

    • Example:

      // Asserts that the value of the family element of the second entry contains 'Chalmers'
      response.assertXPathNotContains("entry[2]/resource/Patient/name/family", "Chalmers")
      
    • Equivalent to each of these:

      • assertXPathNotContains("entry[2]/resource/Patient/name/family", "Chalmers", response)
        
      • // xPath, expectedValue, and operator can be passed as parameters from test script.
        assertXPath("entry[2]/resource/Patient/name/family", "Chalmers", "notContains", response)
        
      • def family = response.getXPathValue("entry[2]/resource/Patient/name/family")
        
        assert !family.contains("Chalmers"): "The actual value \""+family+"\" contained the expected value
           \"Chalmers\" for \"entry[2]/resource/Patient/name/family\" with operator 'notContains' in response."
        
      • def family = response.xPath("entry[2]/resource/Patient/name/family").value
        
        assert notContains("Chalmers", family): "The actual value \""+family+"\" contained the expected value
           \"Chalmers\"  for \"entry[2]/resource/Patient/name/family\" with operator 'notContains' in response."
        
  • assertXPathEmpty(xpath)

    • Asserts that the evaluated value of the provided xpath expression is absent or empty.

    • Example:

      response.assertXPathEmpty("entry[1]/resource/Patient/photo/title")
      
    • Equivalent to these:

      assertXPathEmpty("entry[1]/resource/Patient/photo/title", response)
      
      response.fhirPath("entry[1]/resource/Patient/photo/title").empty();
      
      def title = response.getXPathValue("entry[1]/resource/Patient/photo/title")
      
      assert !title: "Expected title to be absent but was present in response"
      
      def title = response.xPath("entry[1]/resource/Patient/photo/title").value
      
      assert empty(title): "Expected title to be absent but was present in response"
      
  • assertXPathNotEmpty(xpath)

    • Asserts that the evaluated value of the provided xpath expression is present and not empty.

    • Example:

      response.assertXPathNotEmpty("entry[1]/resource/Patient/birthDate")
      
    • Equivalent to these:

      assertXPathNotEmpty("entry[1]/resource/Patient/birthDate", response)
      
      response.xPath("entry[1]/resource/Patient/birthDate").notEmpty();
      
      def title = response.getXPathValue("entry[1]/resource/Patient/birthDate")
      
      assert title: "Expected birthDate to be absent but was present in response"
      
      def title = response.xPath("entry[1]/resource/Patient/birthDate").value
      
      assert notEmpty(title): "Expected birthDate to be absent but was present in response"
      
  • assertXPathEquals(xpath, expectedValue)

    • Asserts that the evaluated value of the provided xpath expression matches the provided expectedValue.

    • Example:

      // Asserts that the value of the family element of the first entry is 'Chalmers'
      response.assertXPathEquals("entry[1]/resource/Patient/name/family", "Chalmers")
      
    • Equivalent to each of these:

      • assertXPathEquals("entry[1]/resource/Patient/name/family", "Chalmers", response)
        
      • // xPath, expectedValue, and operator can be passed as parameters from test script.
        assertXPath("entry[1]/resource/Patient/name/family", "Chalmers", "equals", response)
        
      • def family = response.getXPathValue("entry[1]/resource/Patient/name/family")
        
        assert family.equals("Chalmers"): "The actual value \""+family+"\" did not match the expected value
        \"Chalmers\" for \"entry[1]/resource/Patient/name/family\" in response."
        
      • def family = response.xPath("entry[1]/resource/Patient/name/family").getValue()
        
        assert family == "Chalmers": "The actual value \""+family+"\" did not match the expected value
           \"Chalmers\" for \"entry[1]/resource/Patient/name/family\" in response."
        
      • def family = response.xPath("entry[1]/resource/Patient/name/family").value
        
        assert equals("Chalmers", family): "The actual value \""+family+"\" did not match the expected value
           \"Chalmers\"  for \"entry[1]/resource/Patient/name/family\" in response."
        
  • assertXPathNotEquals(xpath, expectedValue)

    • Asserts that the evaluated value of the provided xpath expression does not match the provided expectedValue.

    • Example:

      // Asserts that values of the family element of the first entry is not 'Chalmers'
      response.assertXPathNotEquals("entry[1]/resource/Patient/name/family", "Chalmers")
      
    • Equivalent to each of these:

      • assertXPathNotEquals("entry[1]/resource/Patient/name/family", "Chalmers", response)
        
      • // xPath, expectedValue, and operator can be passed as parameters from test script.
        assertXPath("entry[1]/resource/Patient/name/family", "Chalmers", "notEquals", response)
        
      • def family = response.getXPathValue("entry[1]/resource/Patient/name/family")
        
        assert !family.equals("Chalmers"): "The actual value \""+family+"\" matched
           the expected value \"Chalmers\" for \"entry[1]/resource/Patient/name/family\" with operator
              'notEquals' in response."
        
      • def family = response.xPath("entry[1]/resource/Patient/name/family").getValue()
        
        assert family != "Chalmers": "The actual value \""+family+"\" matched
           the expected value \"Chalmers\" for \"entry[1]/resource/Patient/name/family\" with operator
              'notEquals' in response."
        
      • def family = response.xPath("entry[1]/resource/Patient/name/family").value
        
        assert notEquals("Chalmers", family): "The actual value \""+family+"\" matched
           the expected value \"Chalmers\" for \"entry[1]/resource/Patient/name/family\" with operator
              'notEquals' in response."
        
  • assertXPathGreaterThan(xpath, expectedValue)

    • Asserts that the evaluated value of the provided xpath expression is greater than the provided expectedValue.

    • Example:

      // Asserts that resource id of the second entry is greater than 1100 in the response
      response.assertXPathGreaterThan("entry[2]/resource/Patient/id", 1100)
      
    • Equivalent to each of these:

      • assertXPathGreaterThan("entry[2]/resource/Patient/id", 1100, response)
        
      • // xPath, expectedValue, and operator can be passed as parameters from test script.
        assertXPath("entry[2]/resource/Patient/id", "1100", "greaterThan", response)
        
      • def id = response.getXPathValue("entry[2]/resource/Patient/id")
        
        assert id.toInteger() > 1100: "Expected \"entry[2]/resource/Patient/id\" to be greater than 5000
            but was "+id+" in response"
        
      • def id = response.xPath("entry[2]/resource/Patient/id").getValue()
        
        assert (id as Integer) > 1100: "Expected \"entry[2]/resource/Patient/id\" to be greater than 5000
           but was "+id+" in response"
        
      • def id = response.xPath("entry[2]/resource/Patient/id").value
        
        assert greaterThan(1100, id): "Expected \"entry[2]/resource/Patient/id\" to be greater than 5000
           but was "+id+" in response"
        
  • assertXPathLessThan(xpath, expectedValue)

    • Asserts that the evaluated value of the provided xpath expression is less than the provided expectedValue.

    • Example:

      // Asserts that resource id of the first entry is less than 1100 in the response
      response.assertXPathLessThan("entry[1]/resource/Patient/id", 1100)
      
    • Equivalent to each of these:

      • assertXPathLessThan("entry[1]/resource/Patient/id", 1100, response)
        
      • // xPath, expectedValue, and operator can be passed as parameters from test script.
        assertXPath("entry[1]/resource/Patient/id", "1100", "lessThan", response)
        
      • def id = response.getXPathValue("entry[1]/resource/Patient/id")
        
        assert id.toInteger() < 1100: "Expected \"entry[1]/resource/Patient/id\" to be less than 5000
            but was "+id+" in response"
        
      • def id = response.xPath("entry[1]/resource/Patient/id").getValue()
        
        assert (id as Integer) < 1100: "Expected \"entry[1]/resource/Patient/id\" to be less than 5000
           but was "+id+" in response"
        
      • def id = response.xPath("entry[1]/resource/Patient/id").value
        
        assert lessThan(1100, id): "Expected \"entry[1]/resource/Patient/id\" to be less than 5000
           but was "+id+" in response"
        
  • assertXPathIn(xpath, expectedValues)

    • Asserts that the evaluated value of the provided xpath expression is one of the provided expectedValues where each value is separated by a comma.

    • Example:

      // Asserts that resource id of the second entry is either 1100 or 1101 or 1102
      response.assertXPathIn("entry[2]/resource/Patient/id", "1100,1101,1102")
      
    • Equivalent to each of these:

      • assertXPathIn("entry[2]/resource/Patient/id", "1100,1101,1102", response)
        
      • // xPath, expectedValue, and operator can be passed as parameters from test script.
        assertXPath("entry[2]/resource/Patient/id", "1100,1101,1102", "in", response)
        
      • def id = response.getXPathValue("entry[2]/resource/Patient/id")
        
        assert id in ["1100", "1101", "1102"]: "Expected one of the values in [1100, 1101, 1102]
           for \"entry[2]/resource/Patient/id\" but encountered \""+id+"\" in response."
        
      • def id = response.xPath("entry[2]/resource/Patient/id").value
        
        assert isIn("1100,1101,1102", id): "Expected one of the values in [1100, 1101, 1102] for
           \"entry[2]/resource/Patient/id\" but encountered \""+id+"\" in response."
        
  • assertXPathNotIn(xpath, expectedValues)

    • Asserts that the evaluated value of the provided xpath expression is none of the provided expectedValues where each value is separated by a comma.

    • Example:

      // Asserts that resource id of the second entry is either 1100 or 1101 or 1102
      response.assertXPathNotIn("entry[2]/resource/Patient/id", "1100,1101,1102")
      
    • Equivalent to each of these:

      • assertXPathNotIn("entry[2]/resource/Patient/id", "1100,1101,1102", response)
        
      • // xPath, expectedValue, and operator can be passed as parameters from test script.
        assertXPath("entry[2]/resource/Patient/id", "1100,1101,1102", "notIn", response)
        
      • def id = response.getXPathValue("entry[2]/resource/Patient/id")
        
        assert !(id in ["1100", "1101", "1102"]): "Expected none of the values in [1100, 1101, 1102]
           for \"entry[2]/resource/Patient/id\" but encountered \""+id+"\" with operator 'notIn' in response."
        
      • def id = response.xPath("entry[2]/resource/Patient/id").value
        
        assert isNotIn("1100,1101,1102", id): "Expected none of the values in [1100, 1101, 1102] for
           \"entry[2]/resource/Patient/id\" but encountered \""+id+"\" with operator 'notIn' in response."