Isabelle T.
Isabelle T.

Reputation: 53

Karate xml bodyPath not being matched in mock: scenario match evaluation failed

This is what the request looks like:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
   ...
    <SOAP-ENV:Header>
        <ns2:Security SOAP-ENV:mustUnderstand="1">
            <ns2:UsernameToken>
            ...
            </ns2:UsernameToken>
        </ns2:Security>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:customerQualificationRequest>
            <ns1:header>
                ...
            </ns1:header>
            <ns1:creditApplication>
            ...
                <ns1:lastName>Shopping</ns1:lastName>
        ...
      </ns1:creditApplication>
      </ns1:customerQualificationRequest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

And the mock:

  Scenario: pathMatches('<path>') && requestHeaders['SOAPAction'][0] == '<soapAction>' && bodyPath('/ns1:customerQualificationRequest/ns1:creditApplication/ns1:lastName') == 'Shopping'

It works if I just remove the bodyPath, but doesn't find a match with the bodyPath. I need to have several cases where the lastName is different as the answer would be different, so I need to match with that parameter. What am I doing wrong?

Upvotes: 1

Views: 512

Answers (2)

Isabelle T.
Isabelle T.

Reputation: 53

After struggling with it for a while, I found the answer. The following worked: && bodyPath('/Envelope/Body/customerQualificationRequest/creditApplication/lastName')

Upvotes: 1

Peter Thomas
Peter Thomas

Reputation: 58058

I do wish XML was easier. Here's something I just came up with. You can use Json-Path on XML, and sometimes it works better. Take this example (normal Karate test, not mock):

* def req =
"""
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
      <ns1:customerQualificationRequest>
        <ns1:creditApplication>
          <ns1:lastName>Shopping</ns1:lastName>
        </ns1:creditApplication>
      </ns1:customerQualificationRequest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
"""
* def temp = karate.get('$req..ns1:lastName')
* match temp == ['Shopping']

Which means this should work:

bodyPath('$..ns1:lastName').length > 0 && bodyPath('$..ns1:lastName')[0] == 'Shopping'

Since that is clunky, you can define a custom function in the Background:

Background:
* def getLastName = function(){ var temp = karate.get('$request..ns1:lastName'); return temp.length > 0 ? temp[0] : null }

And then:

getLastName() == 'Shopping'

Upvotes: 1

Related Questions