Forum Discussion

BouncyBall's avatar
BouncyBall
New Contributor
7 years ago

Passing a value in a Groovy request and catching the response

Hey,

 

I am really having trouble with this simple groovy script. I have a SOAP service that takes a single parameter and I just want to pass the parameter as a variable and catch the response. The request outputs the correct structure with the value I passed but the response is always empty.

 

I passed the parameter in the SOAP request as ${x} and wrote some code like this :

 

def durp = context.x = "000000000009006087"

def request = context.expand( '${RetrieveEquipmentModelDetail#Request}')
log.info request.toString()
//log.info context.toString()
def response = context.expand( '${$request#Response}')
//log.info context.toString()
log.info response.toString()

5 Replies

  • If you are needing to get your response use this instead

     

    def response = context.expand( '${RetrieveEquipmentModelDetail#Response}')

  • BouncyBall's avatar
    BouncyBall
    New Contributor

    Didn't work yet. I turned the groovy code into this:

    context.x = "000000000009006087"
    
    def request = context.expand( '${RetrieveEquipmentModelDetail#Request}')
    log.info request.toString()
    def response = context.expand( '${RetrieveEquipmentModelDetail#Response}')
    log.info responseSAPxml.toString()

    The request shows that the context.x value is indeed inserted in the request. Also, I tried actually entering the value manually and running the request and then it returns the correct response but it does not happen when I do it in my code. I think the request is being sentbut in the catching of the response something goes wrong. It's always a blank reponse as if the value in the request was blank. It doesn't feel like I am catching the response from the request.

  • Lucian's avatar
    Lucian
    Community Hero

    Hi, let's start from the beginning.

     

    When you send a SOAP request the response is automatically saved in a custom propertySketch.png

     

    This can simply be referenced later from a groovy script as:

    context.expand( '${TestStepName#Response}')

     Be aware that the test step name is case sensitive.

     

    Anyways.. if it happens for you to receive only some headers with no body then the response will appear empty. You can still get the raw response though you will need to use:

     

    new String(testRunner.testCase.testSteps["TestStepName"].testRequest.response.rawResponseData)
  • BouncyBall's avatar
    BouncyBall
    New Contributor

    Hey,

     

    Thanks for the response.

    I checked in the custom properties what the request value is and it is ${x}. So it seems that when my script runs, he does not use the values I provide.

    Also my script fails if I don't run the SOAP request manually first. But if I then run the script, it will always take the result of the manual SOAP request execution. It's like the script doesn't trigger a new request but just keeps working on the manually run request.

     

    My SOAP request looks like this (left the SOAP header out for privacy reasons):

    <soapenv:Header/>
       <soapenv:Body>
          <tran:RetrieveEquipmentModelDetail>
             <!--Optional:-->
             <tran:equipmentModelIds>
                <!--Zero or more repetitions:-->
                <equ:EquipmentModelId>
                   <!--Optional:-->
                   <equ:Id>${x}</equ:Id>
                </equ:EquipmentModelId>
             </tran:equipmentModelIds>
          </tran:RetrieveEquipmentModelDetail>
       </soapenv:Body>
    </soapenv:Envelope>

    My script is this:

    context.x = "000000000099007809"
    def response = context.expand( '${RetrieveEquipmentModelDetail#Response}')
    log.info response

    There is not other step involved. It's just the SOAP request and a Groovy script.

  • nmrao's avatar
    nmrao
    Champion Level 3
    BouncyBall,

    What is your use case?
    Can you not use custom property and use it with the help of property expansion.