如何从 cdata 响应中提取参数并在 soapUi 中使用 groovy 在另一个请求中使用它

how to pull the parameter from cdata response and use it in another request using groovy in soapUi

  1. 这是我对 soapUI 的回复
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
          <SearchAirFaresResponse xmlns="http://www.sample.com/xchange">
           <SearchAirFaresResult>
            <![CDATA[
             <FareSearchResponse>
               <MasterDetails>
                  <CurrencyCode>INR</CurrencyCode>
                  <RecStNo>1</RecStNo>
                  <SessionID>5705b1a6-95ac-486c-88a1f90f85e57590</SessionID>
                </MasterDetails>
                </FareSearchResponse>
            ]]>
        </SearchAirFaresResult>
     </SearchAirFaresResponse>
   </soap:Body>
</soap:Envelope>
  1. 如何使用 groovy 脚本提取 CDATA 中的 SessionID 元素并在另一个请求中使用它,例如

      <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <soap:Body>
       <xch:GetMoreFares>
        <xch:sGetMoreFare>
       <![CDATA[
        <MoreFlights>
        <MasterDetails>
            <NoOfResult Index="1">40</NoOfResult>
            <BranchId>1</BranchId>
            <SessionId>5705b1a6-95ac-486c-88a1f90f85e57590</SessionId>
        </MasterDetails>
        <Journey>DOM</Journey>
        <ResponseType>XML</ResponseType>
        <SearchType>OW</SearchType>
        </MoreFlights>
        ]]>
        </xch:sGetMoreFare>
      </soap:Body>
    </soap:Envelope>
    

    3.I 搜索了很多但没有找到合适的,而且我也是使用 soapUi 的 groovy 脚本的新手,请指导我在 soapUi 中逐步执行程序。

为此,您可以使用 Groovy testStep,在其中获取 SOAP testStep,您可以在其中获得所需 sessionID 的响应并使用XmlSlurper 解析响应并获取 CDATA 值。请注意,XmlSlurperCDATA 视为 String,因此您必须再次解析它。最后将返回值保存为TestSuiteTestCase级别(在示例中我使用TestCase):

// get your first testStep by its name
def tr = testRunner.testCase.getTestStepByName('Test Request')
// get your response
def response = tr.getPropertyValue('response')
// parse the response and find the node with CDATA content
def xml = new XmlSlurper().parseText(response)
def cdataContent = xml.'**'.find { it.name() == 'SearchAirFaresResponse' }
// XmlSlurper treat CDATA as String so you've to parse
// its content again
def cdata = new XmlSlurper().parseText(cdataContent.toString())
// finally get the SessionID node content
def sessionId = cdata.'**'.find { it.name() == 'SessionID' }

// now save this value at some level (for example testCase) in
// order to get it later
testRunner.testCase.setPropertyValue('MySessionId',sessionId.toString())

然后稍微改变你的第二个 testStep 以使用 property expansion 在你的第二个请求中获取 MySessionId 属性 作为 ${#TestCase#MySessionId}:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <soap:Body>
   <xch:GetMoreFares>
    <xch:sGetMoreFare>
   <![CDATA[
    <MoreFlights>
    <MasterDetails>
        <NoOfResult Index="1">40</NoOfResult>
        <BranchId>1</BranchId>
        <SessionId>${#TestCase#MySessionId}</SessionId>
    </MasterDetails>
    <Journey>DOM</Journey>
    <ResponseType>XML</ResponseType>
    <SearchType>OW</SearchType>
    </MoreFlights>
    ]]>
    </xch:sGetMoreFare>
  </soap:Body>
</soap:Envelope>

这也与 albciff 的答案最相似,但变化不大(使用可重用闭包来解析)。

这是第一个请求步骤的 Script Assertion。这将避免测试用例中的额外 groovy 脚本步骤。

请关注内联的适当评论:

脚本断言:

/**
 * This is Script Assertion for the first
 * request step which extracts cdata from response,
 * then sessionId from extracted cdata. And
 * Assigns the value at test case level, so that
 * it can be used in the rest of the test steps of
 * the same test case.
 * However, it is possible to store the sessionId
 * at Suite level as well if you want use the sessionId
 * across the test cases too. 
 * 
 * */

/**
* Closure to search for certain element data
* input parameters
* xml data, element name to search
**/
def searchData = { data, element ->
   def parsedData = new XmlSlurper().parseText(data)
   parsedData?.'**'.find { it.name() == element} as String
}

//Assert the response
assert context.response, "Current step response is empty or null"

//Get the cdata part which is inside element "SearchAirFaresResult"
def cData = searchData(context.response,'SearchAirFaresResult')
//Assert CDATA part is not null
assert cData, "Extracted CDATA of the response is empty or null"

//Get the SessionID from cdata
def sessionId = searchData(cData, 'SessionID')
//Assert sessionId is not null or empty
assert sessionId, "Session Id is empty or null"
log.info "Session id of response  $sessionId1" 

//Set the session to test case level custom property
//So, that it can be used for the rest of the steps 
//in the same test case
context.testCase.setPropertyValue('SESSION_ID', sessionId)

在接下来的测试步骤中,您可以通过以下方式使用保存的SESSION_ID

  • 如果接下来的步骤是请求步骤(REST、SOAP、HTTP、JDBC等),那么使用property expansion ${#TestCase#SESSION_ID} like <SessionId>${#TestCase#SESSION_ID}</SessionId>
  • 如果以下步骤是 groovy 脚本,则使用以下之一:
    context.expand('${#TestCase#SESSION_ID}')
    context.testCase.getPropertyValue('SESSION_ID')
    testRunner.testCase.getPropertyValue('SESSION_ID')