【问题标题】:how to pull the parameter from cdata response and use it in another request using groovy in soapUi如何从cdata响应中提取参数并在另一个请求中使用soapUi中的groovy
【发布时间】:2016-10-09 21:43:41
【问题描述】:
  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中逐步执行。

【问题讨论】:

  • 看起来您收到了不同类型的回复,您想从两个回复中提取sid?顺便说一句,第一个响应似乎格式不正确,并且是 searchResponse 真正嵌套的。你能提供正确的xml吗?更新问题。
  • 实际上我需要像我在第 2 节中提到的第二个 xml 结构一样发送请求
  • 您是否尝试过官方文档中的任何内容? soapui.org/functional-testing/working-with-cdata.html

标签: groovy soapui cdata


【解决方案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 在您的第二个请求中以 ${#TestCase#MySessionId} 获取 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>

【讨论】:

  • 您好,我如何从上述示例中选择属性名称“索引”,我尝试通过提供 xpath 但最终出现错误。
【解决方案2】:

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

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

请按照相应的 cmets inline:

脚本断言:

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

【讨论】:

  • 您好,我如何从上面提到的示例中选择属性名称“Index”,我尝试通过提供 xpath 但最终出现错误。
  • @vikkiwiggi,第一个回复中没有Index,对吧?上面的脚本是从响应中获取数据。如果您有其他问题,介意提出一个新问题吗?
  • 假设 40 在响应中,那么我该怎么做,我已经使用你之前的步骤来获取索引属性,但是能够得到ly 节点值不是属性值
猜你喜欢
  • 1970-01-01
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多