来自 csv 文件的空手道 API 构建动态请求 XML,如果 csv 中的 resp 标签值为空,我不希望在请求中创建标签



我正在尝试从 csv 文件构建 xml,如果相应的标签值在 csv 中为空,我想从请求中删除 xml 标签。我有 2 个功能文件、1 个 xml 和 1 个 csv 文件。 下面是我的代码。

'''

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 !

一个独立的代码,您可以运行它,然后根据需要进行自定义:

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 |      |

最新更新