Forum Discussion

ravienu's avatar
ravienu
Occasional Contributor
3 years ago
Solved

passing result or variable value from Groovy to test step

Dear Team,

I have query below to achieve one of the requirements. I am using ReadyAPI with proper license.

 

I would like to pass Goovy script result as one request body(XML/JSON) parameter for API. I have written a Groovy script and test step under it. But dont know how to specify groovy result as body request parameter.

 

For example:

def today = new Date()
today.format("dd/MM/yyyy")

//Add some random number to Date field and store it in some variable like cutom_date

 

Now  I would like to pass custom_date as request body(XML) parameter for API step under the groovy script  as below

........
........
<app_date>?</app_date>
.......
.......

cutom_date from groovy script should come and sit at ? place above.

 

Can anyone help on this please ?

11 Replies

  • jamesjack's avatar
    jamesjack
    Occasional Visitor

    import groovy.json.JsonSlurper

    testStep = testRunner.testCase.testSteps["YourApiRequestStep"]

    def Response = testStep.getProperty("response").value;

    def someFieldYouWantToSave = ""


    if (Response == null) {
    log.error('No Response found.');
    }
    else {
    def jSlurper = new JsonSlurper();
    def json = jSlurper.parseText(Response);
    if (json.get("theFieldFromTheResponse") == null){
    log.error "TheFieldFromTheResponse not found in response. Please execute the teststep and try again"
    } else { omegle 
    someFieldYouWantToSave = json.get("theFieldFromTheResponse").toString()

    // YOUR LOGIC HERE FOR MODIFYING THE "someFieldYouWantToSave" value

    //SAVE THE FIELD
    testRunner.testCase.setPropertyValue("someFieldYouWantToSave", someFieldYouWantToSave)
    }
    } Thank you omegle chat 

    • ravienu's avatar
      ravienu
      Occasional Contributor

      Hi nmrao ,

       

      Thank you for your reply.

       

      I have got the code snippet to get current date from payload directly by searching in the form and it worked. But my query is different as I have replied to Richi, keeping here as well.

       

      Sorry If I am not clear in my question.

      I dont want to send just current date to <app_date> element in payload. I want to get current date and then modify the date field to +/- few days with custom code and then send that changed date to payload.

       

      In other words, If I have one variable value in my groovy script, like employeeNumber = 2314, How I can pass this to payload element <employee_id>. ?

       

      Thank you very much for your help.

       

      Regards,

      Ravi.

      • nmrao's avatar
        nmrao
        Champion Level 3
        The link provided in the previous reply has the java 8 date which can included the request itself as per the question.
  • richie's avatar
    richie
    Community Hero
    Hey ravienu,

    Am i understanding correctly you have a payload with an <app_date> element and you want it to be populated with the current date no matter when you execute your test (so the value is automatically updated to todays date)?

    If the answer is "yes", you dont need a separate groovy script step, you can use dynamic scripting to do this.

    For example, if you had an element <attribute>?</attribute> and you wanted it to be dynamically populated with a UUID/GUID value, i would replace the ? in my attribute element with the following string

    ${ =java.util.uuid.randomuuid()}

    you can do the same with date handling, but the syntax to do this was changed a while back and im typing this out on my phone so cant remember the syntax.

    When im in front of my laptop i will update this post with the correct dynamic scripting command you need.

    Cheers

    Rich
    • ravienu's avatar
      ravienu
      Occasional Contributor

      Hi richie ,

      Thank you for your reply.

       

      Sorry If I am not clear in my question.

      I dont want to send just current date to <app_date> element in payload. I want to get current date and then modify the date field to +/- few days with custom code and then send that changed date to payload.

       

      In other words, If I have one variable value in my groovy script, like employeeNumber = 2314, How I can pass this to payload element <employee_id>. ?

       

      Thank you very much for your help.

       

      Regards,

      Ravi.

      • richie's avatar
        richie
        Community Hero
        Hey ravienu,

        You can also use the dynamic scripting option to set current date +/- number of hours/days/months etc. with the code snippet i was talking about but anyway, youre saying you have an employeeNumber variable set as 1234. You could use the setProperty method to write the value to a holding property (e.g. testcase level Property using something like "testRunner.testCase.setPropertyValue( "EmplyeeNumberProp", employeeNumber )" and then use the GUI GetData function from the employeeNumber element within your payload to source the employeeNumber value saved as a testcase level Property. This would satisfy what you need to do.

        I should highlight that there are multiple ways of doing what you need and the genuine coders on the forum (i'm not one of them) like nmrao or ZDGN will likely have far more efficient ways of doing this.

        Ta

        Rich