Search code examples
groovysoapuicdata

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


  1. This is my response from 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. How to extract SessionID element which is inside CDATA using groovy script and use it in another request like

      <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 have been searching lot but didnt get the right one,and also am new to groovy script using soapUi , pls guide me stepwise procedure in soapUi to implement .


Solution

  • This is also most similar to albciff's answer, but with little variant (using reusable closure to parse).

    Here is the Script Assertion for the first request step. This will avoid additional groovy script step in the test case.

    Please follow the appropriate comments inline:

    Script Assertion:

    /**
     * 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)
    

    In the following test steps, you can use the saved SESSION_ID in the following ways:

    • If the following step is request step (REST, SOAP, HTTP, JDBC etc), then use property expansion ${#TestCase#SESSION_ID} like <SessionId>${#TestCase#SESSION_ID}</SessionId>
    • If the following step is groovy script, then use one of the below:
      context.expand('${#TestCase#SESSION_ID}') or
      context.testCase.getPropertyValue('SESSION_ID') or
      testRunner.testCase.getPropertyValue('SESSION_ID')