Reputation: 33
I am trying to build xml from csv file and wanted to remove the xml tags from request if the respective tag value is blank in the csv. I have 2 feature files, 1 xml and 1 csv file. Below is my code.
'''
request.xml:
<ROUTE>
<Name>
<FirstName></FirstName>
<LastName></LastName>
</Name>
</ROUTE>
getQuoteTest.feature:
@tag
Feature: Get Quote Test
Scenario Outline:
* call read('classpath:getQuote.feature') {'FirstName':"<FirstName>",'LastName':"<LastName>"}
Examples:
| read('classpath:TestData.csv') |
CSV file:
FirstName LastName
Matt Chat
(blank) John
Shane Bond
Andrew (blank)
getQuote.feature
@ignore
Feature: Get Quote
Background:
Scenario:
* xml req = read('classpath:request.xml')
* set req/ROUTE/Name/FirstName = FirstName
* set req/ROUTE/Name/LastName = LastName
Given request req
When method POST
Then status 200
And print response
'''
When FirstName is blank, I don't want <FirstName></FirstName> tags to be present in my request xml. It would be great if I can get the exact code.
Thanks in advance !
Upvotes: 1
Views: 698
Reputation: 1096
A self contained code which you can run and then customize for your needs:
Feature: Conditional Example
Background:
Scenario Outline:
* def req =
"""
<ROUTE>
<Name>
<FirstName><FN></FirstName>
<LastName><LN></LastName>
</Name>
</ROUTE>
"""
* xml req2 = req
* if (req2.ROUTE.Name.LastName == null) karate.remove("req2","//LastName")
* if (req2.ROUTE.Name.FirstName == null) karate.remove("req2","//FirstName")
* print req2
Examples:
| FN | LN |
| Matt | Chat |
| | John |
| Shane | Bond |
| Andrew | |
Upvotes: 1