Forum Discussion

Department_of_T's avatar
Department_of_T
Contributor
11 years ago

Contains Group Assertion

I need to add an assertion that returns true if the element and its value exists in the response and this value matches a JDBC response. But i need the assertion to pass if the element is missing and the response back from JDBC is null (empty string value). Quite often a valid scenario is that some elements are missing from the response.

I tried doing this with a Conditional Group Assertion
1)Contains assertion to verify the element exists
2)XPATH assertion to verify value matches the JBDC call.

Question:
1)How can i edit the XPATH assertion for the boolean to return a blank value eg " " which matches the value of JDBC instead of returning false?
2)If there an easier way to do this assertion without grouping the assertions?
-------------------

CONTAINS ASSERTION:
(?s).*<postalAddress>.*<nationalPostalAddress>.*<firstAddressLine>.*


XPATH EXPRESSION
declare namespace ns0='http://transport.wa.gov.au/apo-ws/';
boolean(//ns0:findVehicleRegistrationRenewalResponse[1]/findVehicleRegistrationRenewalResponse[1]/postalAddress[1]/nationalPostalAddress[1]/firstAddressLine[1])

XPATH EXPECTED RESULT
${FindPersonCurrentVehOwner - JDBC#ResponseAsXml#//Results[1]/ResultSet[1]/Row[1]/POSADDRLINE1[1]}


--PART OF RESPONSE--
<residentialAddress>
<firstAddressLine>233 ROYAL ST</firstAddressLine>
<secondAddressLine/>
<postcode>6060</postcode>
<suburb>YOKINE</suburb>
<stateOrTerritoryCode>WA</stateOrTerritoryCode>
</residentialAddress>
<postalAddress>
<nationalPostalAddress/>
<overseasPostalAddress>
<firstAddressLine>10/79 BESSELL AVENUE</firstAddressLine>
<secondAddressLine/>
<postcode>6152</postcode>
<townCity>COMO</townCity>
<countryCode>AU</countryCode>
</overseasPostalAddress>
</postalAddress>
  • Hi, its a very similar problem but i am not sure how to use XPATH with the Not Exists and Matches statement that references the a JDBC call.
    eg For SUBSEQUENT_GIVEN_NAMES (which is sometimes returned)
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    Looking that the error, the value that you are returning as "LEE" in the second part of the matches comparison should be in quotes.
    Try that update to your assertion.

    Thanks,

    Giscard
    SmartBear Support
  • I think i had the code in the wrong place. Now i have placed the code in the top part 'XPATH EXPRESSION' and it works when the JDBC result is NOT null. How can i ensure the assert does not throw an error message when the value of the JDBC is null ''

    I tried this but it didnt work.
    concat(//${FindPersonCurrentVehOwner - JDBC#ResponseAsXml#//Results[1]/ResultSet[1]/Row[1]/SUBSEQUENT_GIVEN_NAMES[1]},"")

    declare namespace ns0='http://transport.wa.gov.au/apo-ws/';
    not(exists(//ns0:findVehicleRegistrationRenewalResponse[1]/findVehicleRegistrationRenewalResponse[1]/subsequentGivenNames[1]/text()) )
    or
    (exists(//ns0:findVehicleRegistrationRenewalResponse[1]/findVehicleRegistrationRenewalResponse[1]/subsequentGivenNames[1]) and
    contains(//ns0:findVehicleRegistrationRenewalResponse[1]/findVehicleRegistrationRenewalResponse[1]/subsequentGivenNames[1]/text(),${FindPersonCurrentVehOwner - JDBC#ResponseAsXml#//Results[1]/ResultSet[1]/Row[1]/SUBSEQUENT_GIVEN_NAMES[1]}))


    ERROR MESSAGE WHEN RESULT FROM JDBC IS NULL
    XPathContains assertion failed for path [declare namespace ns0='http://transport.wa.gov.au/apo-ws/'; not(exists(//ns0:findVehicleRegistrationRenewalResponse[1]/findVehicleRegistrationRenewalResponse[1]/subsequentGivenNames[1]/text()) ) or (exists(//ns0:findVehicleRegistrationRenewalResponse[1]/findVehicleRegistrationRenewalResponse[1]/subsequentGivenNames[1]) and contains(//ns0:findVehicleRegistrationRenewalResponse[1]/findVehicleRegistrationRenewalResponse[1]/subsequentGivenNames[1]/text(),${FindPersonCurrentVehOwner - JDBC#ResponseAsXml#//Results[1]/ResultSet[1]/Row[1]/SUBSEQUENT_GIVEN_NAMES[1]} ))] : RuntimeException:net.sf.saxon.trans.XPathException: XPath syntax error at char 131 on line 5 in {...bsequentGivenNames[1]/text(...}: Node constructor expressions are allowed only in XQuery, not in XPath
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    I believe the XPath assertion has limitations in respect to handling the null responses.
    I suggest you use a script assertion. Attached is a project that mimics the JDBC response with a Properties TestStep and compares it with a response from a MockService. The responses are identical except that second to last node of the JDBC response is empty. In the TestCase, there is a request to the MockService and that step has the Script Assertion. Both responses are parsed into an XmlSlurper each that are used to do the assertion. I think you should be able to apply that solution to your project.

    Regards,

    Giscard
    SmartBear Support