Forum Discussion

mrechberger's avatar
mrechberger
New Contributor
3 years ago
Solved

Groovy script - Sum two values of REST response and pass the sum on to the next request

Hi!

I'm currently utilizing ReadyAPI for functional tests of our API.
I am already using "simple" property transfer / Get Data to pass on a value from one test step REST response to the next REST request like:
${TestStep1#Response#$['ABC']['Array'][0]['Price']['value']}

 

Now what I want to achieve is passing on the sum of two values of a response to the next request.
E.g. let's say Test Step 1 Response gives me the following data:

 

Now I want to pass on the sum of the prices (1.4 + 2 = 3.4) to a field of the next request.

 

I was told by Smartbear Support that this can be achieved using Groovy scripts but they do not offer customer support for scripts and I was referred to this Community form.


As I am totally new to Groovy Script and not sure about how to use it for my goal which isn't complex at all but again, I have never used Groovy Script before, I am asking if someone could help of an example of how this would look like?
Maybe also with an example if the values were returned in string format and I would need to parse them to Double first?

 

Thank you!

  • Hi,

     

    Groovy script is a subset of Java and is really, really useful to learn.  There's loads of on-line resources for using Groovy scripts.

     

    In terms of what you want, here is the script.  Create a Groovy script step, called "Add - Groovy Script" after your web service call and paste in the below....

     

    def valueOne = "4";  // ${TestStep1#Response#$['ABC']['Array'][0]['Price']['value']};
    def valueTwo = "8.2";  // ${TestStep1#Response#$['ABC']['Array'][1]['Price']['value']};
    
    try {
    	def valueOneDouble = Double. parseDouble(valueOne);
    	def valueTwoDouble = Double. parseDouble(valueTwo);
    
    	def answer = valueOneDouble + valueTwoDouble;
    	log.info ("My Sum is ${valueOneDouble} + ${valueTwoDouble} = ${answer}");
    
    	def answerString = String.valueOf(answer);
    
            // Put the answer into a custom property for the test case.
    	testRunner.testCase.setPropertyValue( "mySum", answerString );
            // or simply return it....
            return answerString;
    	
    } catch (NumberFormatException e) {
    //the parseDouble failed and you need to handle it here.
    }

     

    Note : for example purposes, I've hard-coded values one and two and commented how you'd access them according to your question.

     

    To use the answer in a subsequent step, you have options.  You're already using Property Transfer, but I never have so I don't know if you can transfer the answer from a groovy script.

     

    My example puts the answer into a custom property (like a global variable) at the test case level.  You can then access this from another step.  Look up custom properties on the SmartBear site.

     

    You can also 'pull' the answer from the Groovy step directly into the subsequent step.  If you go the step where you want the answer and edit the payload.  When you want the value inserting, type...

    ${Add - Groovy Script#result}

    When ReadyAPI is about to call the 'subsequent' step, it will then call the Groovy script and replace the ${} stuff with the actual value.

  • ChrisAdams's avatar
    ChrisAdams
    3 years ago

    Hi,

    Try wrapping context.expand around your variable.

    E.g.

    def value = ${CheckContingentStartTimes#Response#$['ContingentStartTimes']['ContingentStartTime'][0]['Price']['value']};

    becomes....

    def value = context.expand('${CheckContingentStartTimes#Response#$['ContingentStartTimes']['ContingentStartTime'][0]['Price']['value']}}');

     

    But, if you have ReadyAPI then there is a better way....

     

    In the Groovy window, put the cursor near the top and right-click.  In the context menu, there is an option called 'Get Data'.  Click it.  You'll then get another window which will let you navigate through your test structure and actually select the value you want from the service call's response.  It will then insert the correct value to access.

7 Replies

  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 3

    Hi,

     

    Groovy script is a subset of Java and is really, really useful to learn.  There's loads of on-line resources for using Groovy scripts.

     

    In terms of what you want, here is the script.  Create a Groovy script step, called "Add - Groovy Script" after your web service call and paste in the below....

     

    def valueOne = "4";  // ${TestStep1#Response#$['ABC']['Array'][0]['Price']['value']};
    def valueTwo = "8.2";  // ${TestStep1#Response#$['ABC']['Array'][1]['Price']['value']};
    
    try {
    	def valueOneDouble = Double. parseDouble(valueOne);
    	def valueTwoDouble = Double. parseDouble(valueTwo);
    
    	def answer = valueOneDouble + valueTwoDouble;
    	log.info ("My Sum is ${valueOneDouble} + ${valueTwoDouble} = ${answer}");
    
    	def answerString = String.valueOf(answer);
    
            // Put the answer into a custom property for the test case.
    	testRunner.testCase.setPropertyValue( "mySum", answerString );
            // or simply return it....
            return answerString;
    	
    } catch (NumberFormatException e) {
    //the parseDouble failed and you need to handle it here.
    }

     

    Note : for example purposes, I've hard-coded values one and two and commented how you'd access them according to your question.

     

    To use the answer in a subsequent step, you have options.  You're already using Property Transfer, but I never have so I don't know if you can transfer the answer from a groovy script.

     

    My example puts the answer into a custom property (like a global variable) at the test case level.  You can then access this from another step.  Look up custom properties on the SmartBear site.

     

    You can also 'pull' the answer from the Groovy step directly into the subsequent step.  If you go the step where you want the answer and edit the payload.  When you want the value inserting, type...

    ${Add - Groovy Script#result}

    When ReadyAPI is about to call the 'subsequent' step, it will then call the Groovy script and replace the ${} stuff with the actual value.

    • mrechberger's avatar
      mrechberger
      New Contributor

      Hi ChrisAdams 

       

      Thank you very much for your help.

      Unfortunately when trying to run the script as a test step, I am receiving the following error:

      Here's the minimalistic script I configured:

      def value = ${CheckContingentStartTimes#Response#$['ContingentStartTimes']['ContingentStartTime'][0]['Price']['value']};
      
      log.info("${value}");

       

      It seems to have an issue with how I adressed the field?
      Though this notation is working when using it as a field in the request using the Get Data - dialog.

      Thanks in advance for your assitance.
      Best regards,
      Markus

       

      • ChrisAdams's avatar
        ChrisAdams
        Champion Level 3

        Hi,

        Try wrapping context.expand around your variable.

        E.g.

        def value = ${CheckContingentStartTimes#Response#$['ContingentStartTimes']['ContingentStartTime'][0]['Price']['value']};

        becomes....

        def value = context.expand('${CheckContingentStartTimes#Response#$['ContingentStartTimes']['ContingentStartTime'][0]['Price']['value']}}');

         

        But, if you have ReadyAPI then there is a better way....

         

        In the Groovy window, put the cursor near the top and right-click.  In the context menu, there is an option called 'Get Data'.  Click it.  You'll then get another window which will let you navigate through your test structure and actually select the value you want from the service call's response.  It will then insert the correct value to access.