Home > OS >  How to set attribute value of element xml in a variable in karate?
How to set attribute value of element xml in a variable in karate?

Time:01-27

I am trying to set attribute value of element xml in a variable in karate, as following:

order.xml file
 <Task>   
   <PrimaryTask ref="S2"></PrimaryTask> 
 </Task>

From the above xml example, I will get attribute value of @ref and set it into a variable with name is "act_ref", as following:

Scenario: Sample Code for checking
  Given def order = read('../order.xml')
  * def act_ref = get order //..//Task/PrimaryTask/@ref
  * print 'the value of act_ref is:', act_ref
  And xml act_TaskInfo = get order //Request/SearchBy/Tasks/Task[@subjectID=act_ref]

And log here:

13:50:40.346 [ForkJoinPool-1-worker-3] INFO com.intuit.karate - [print] the value of act_primary_subject_ref is: S2  
TC1_TC12.feature:12 - xpath does not exist: //Request/SearchBy/Tasks/Task[@taskID=act_ref] on client_order

You can see that the variable "act_ref" in path "//Request/SearchBy/Tasks/Task[@taskID=act_ref]" can not be called correct value. So please help me if you know that how to set attribute value of element xml in a variable in karate? Thanks!

My expected for that: //Request/SearchBy/Tasks/Task[@taskID='S2'], 'S2' is value of act_ref.

CodePudding user response:

Refer the docs, use the karate.xmlPath() API: https://github.com/karatelabs/karate#advanced-xpath

Here is a short example:

* def attrName = 'S2'
* def payload = <Task><PrimaryTask ref="S2">foo</PrimaryTask></Task>
* def value = karate.xmlPath(payload, "//*[@ref='"   attrName   "']")
* assert value == 'foo'
  •  Tags:  
  • Related