Saurabh
Saurabh

Reputation: 940

Karate API Tests - Using xml instead of json

I am able to use json file to have my credentials parsed in the feature file. eg:

 * def credentials = read('classpath:credentials.json')
 * header Authorization = call read('classpath:basic-auth.js') { username: '#(credentials.user)', password: '#(credentials.pwd)' }

Here is the credentials json file:

{
  user: 'abc',
  pwd: 'def'
}

However, When I try using an XML file instead, I am not able to parse it through:

Credentials xml file:

<?xml version="1.0" encoding="UTF-8" ?>
 <credentials>  
   <user>abc</user>
   <pwd>def</pwd>
 </credentials>

I changed the feature file as:

 * def credentials = read('classpath:credentials.xml')
 * header Authorization = call read('classpath:basic-auth.js') { username: '#(<credentials><user></credentials>)', password: '#(<credentials><pwd></credentials>)' }

Do i need to make any changes in the way I am parsing the xml ? Any suggestions would be appreciated. Thanks in advance!!

Upvotes: 2

Views: 2443

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Embedded expressions have to use the 'dot notation'. The good news is that this can operate on XML, so try this:

* def creds = read('classpath:credentials.xml')
* header Authorization = call read('classpath:basic-auth.js') { username: '#(creds.credentials.user)', password: '#(creds.credentials.pwd)' }

Within Karate I would advise sticking to JSON as far as possible unless you are forced to use XML because you are re-using something external to your project or testing SOAP or XML payloads. But if you really are doing a lot of XML handling, refer to this set of examples for ideas.

edited - since I missed the extra XML root credentials in the expressions.

Upvotes: 3

Related Questions