如何删除请求中的 SOAP 可选元素

How to remove SOAP Optional elements in the request

使用空手道,我想建立一个 soap 请求。 SOAP Request中有Optional元素,如何根据Scenario Outline: Example?

分享了一个例子,以供讨论。如果已经有示例代码,请分享,谢谢。

Feature: SOAP request to get Customer Details

  Background:
    Given url 'http://localhost:8080/CustomerService_V2_Ws'

  Scenario Outline:
    * def removeElement =
    """
     function(parameters, inputXml) {
         if (parameters.city = null)
             karate.remove(inputXml, '/Envelope/Header/AutHeader/ClientContext/city');
         if (parameters.zipcode = null)
             karate.remove(inputXml, '/Envelope/Header/AutHeader/ClientContext/zipcode');
         return inputXml;
     }
    """
    * def inputXml = read('soap-request.xml')
    * def updatedXml = removeElement(parameters,inputXml)
    Given request updatedXml
    When soap action ''
    Then status <http_code>

    Examples:
      | CustomerId | ZipCode | City     |
      | 001        | null    | null     |
      | 002        | 41235   | null     |
      | 003        | null    | New York |


**Contents of "soap-request.xml"**
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <wsc1:AutHeader xmlns:wsc1="http://example.com/ws/WSCommon_v22">
         <wsc1:SourceApplication>ABC</wsc1:SourceApplication>
         <wsc1:DestinationApplication>SoapUI</wsc1:DestinationApplication>
         <wsc1:Function>CustomerService.readDetails</wsc1:Function>
         <wsc1:Version>2</wsc1:Version>
         <wsc1:ClientContext>
            <wsc1:customerid>10000</wsc1:customerid>
            <!--Optional:-->
            <wsc1:zipcode>11111</wsc1:zipcode>
            <!--Optional:-->
            <wsc1:city>xyz</wsc1:city>
         </wsc1:ClientContext>
      </wsc1:AutHeader>
   </soapenv:Header>
   <soapenv:Body />
</soapenv:Envelope>

是的,我的建议是使用 embedded expressions。当表达式以 2 个哈希符号为前缀时,这将是 "delete if null",这是为您的确切用例设计的。这是一个例子:

Scenario: set / remove xml chunks using embedded expressions
* def phone = '123456'
# this will remove the <acc:phoneNumberSearchOption> element
* def searchOption = null
* def search = 
"""
<acc:getAccountByPhoneNumber>
    <acc:phoneNumber>#(phone)</acc:phoneNumber>
    <acc:phoneNumberSearchOption>##(searchOption)</acc:phoneNumberSearchOption>        
</acc:getAccountByPhoneNumber>
"""
* match search ==
"""
<acc:getAccountByPhoneNumber>
    <acc:phoneNumber>123456</acc:phoneNumber>
</acc:getAccountByPhoneNumber>
"""

请注意,这里还有更多示例:xml.feature