Subitha
Subitha

Reputation: 51

Karate Xml response filtering

In my karate tests, i have Xml response like the following

<wkfcnf:workflow xmlns:wkfcnf="http://test.com/ri/configuration" name="test" status="ACTIVE" >
  <protocols>
    <protocol name="protocol1" uri="https://test/url/2"/>
    <protocol name="protocol2" uri="https://test/url/2"/>
  </protocols>
  <stages>
    <stage name="stage1" uri="https://stage/1"/>
   <stage name="stage2" uri="https://stage/1"/>
  </stages>
</wkfcnf:workflow>

I want to get the protocol Uri based on the protocol name.

Following is the code I have tried. But not working as expected.

* json prepProtocol = $response/workflow/protocols/protocol[(@.name='protocol1')]

Any help on this is appreciated.

Thanks Subitha

Upvotes: 1

Views: 57

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

XPath can be tricky. This worked for me:

* def prepProtocol = $response//protocol[@name='protocol2']/@uri
* match prepProtocol == 'https://test/url/2'

EDIT: if you need to use a variable:

* def name = 'protocol2'
* def prepProtocol = karate.xmlPath(response, "//protocol[@name='" + name + "']/@uri")
* match prepProtocol == 'https://test/url/2'

And you can get a little more elegant like this:

* def prepProtocol = karate.xmlPath(response, `//protocol[@name='${name}']/@uri`)

Upvotes: 1

Related Questions