vikki wiggi vikki wiggi - 4 months ago 31
Groovy Question

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="" xmlns:xsi="" xmlns:xsd="">
<SearchAirFaresResponse xmlns="">

  1. How to extract SessionID element which is inside CDATA using groovy script and use it in another request like

    <soap:Envelope xmlns:soap="" xmlns:xsi="" xmlns:xsd="">
    <NoOfResult Index="1">40</NoOfResult>

    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 .

Rao Rao

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 { == 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" "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