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

问题描述 投票:0回答:2
  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.我一直在寻找很多,但没有得到正确的,并且也是使用soapUi的groovy脚本的新手,请指导我在soapUi中逐步执行程序。
groovy soapui cdata
2个回答
0
投票

这也与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} <SessionId>${#TestCase#SESSION_ID}</SessionId>
  • 如果以下步骤是groovy脚本,则使用以下之一: context.expand('${#TestCase#SESSION_ID}')context.testCase.getPropertyValue('SESSION_ID')testRunner.testCase.getPropertyValue('SESSION_ID')

1
投票

为此,您可以使用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>
© www.soinside.com 2019 - 2024. All rights reserved.