Forum Discussion

nd's avatar
nd
New Contributor
3 years ago
Solved

How to pass result from groovy script to Request body Parameter and Request header ?

Hello Team ,

 

I am using below code into groovy script and want to pass the variable value into Request body field and Request header field

 

My groovy script is

def Date= new Date().format("yyyy-MM-dd HH.mm.ss.SSSSS Z");
log.info("Date" +Date);(I want to pass this in request body field)

 

def Timestamp = new Date().format("yyyy-MM-dd");
log.info("timestamp" +Timestamp); (I want to pass this into request header)

 

Result is

Wed Nov 17 20:53:26 GMT 2021: INFO: Date2021-11-17 20.53.26.00460 +0000
Wed Nov 17 20:53:26 GMT 2021: INFO: timestamp2021-11-17

 

I am trying set this into request body using get data

but when I add the "result" I see no value under that date field in request body!

 

Please help me how to do that.

  • nd's avatar
    nd
    3 years ago

    Hello ,

     

    I tried with below option 

     

    testRunner.testCase.testSteps['Properties'].setPropertyValue("Date", Date.toString());

    testRunner.testCase.testSteps['Properties'].setPropertyValue("Timestamp", Timestamp.toString());

     

    .

     

    Then I tried some exploration on how to setproperty on test cases level and tried below solution 

     

    String timestamp=new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC"))
    testRunner.testCase.setPropertyValue( "TimeStamp", timestamp )
    log.info(timestamp);

     

    String date = new Date().format("yyyy-MM-dd")
    testRunner.testCase.setPropertyValue( "Date", date )
    log.info(date);

     

    I retrieve value from test case properties  using "Get Data" and used in Request body and request Header Parameter and it worked! 

     

    Thank you for your reply. I got lead to search in right direction! 

     

    Thanks,

    Neha

     

2 Replies

  • TNeuschwanger's avatar
    TNeuschwanger
    Champion Level 2

    hello nd 

     

    I think a "result" from a groovy script execution is singular...  You only get one, not both of the values you have computed (Date and Timestamp).

     

    I would add a "Properties" test step to your testcase and include code in your groovy step to set both of your computed values into it...

     

    testRunner.testCase.testSteps['Properties'].setPropertyValue("Date", Date.toString());

    testRunner.testCase.testSteps['Properties'].setPropertyValue("Timestamp", Timestamp.toString());

     

    After the groovy step runs at least once to put the values into the "Properties" test step, you can do the "Get data" inside your request to pull from the properties test step and you should be able to find and use those computed values from there, or drop them in your request in the correct locations with code similar to:  ${Properties#Date} where needed and ${Properties#Timestamp} where needed.

     

    Regards,

    Todd

    • nd's avatar
      nd
      New Contributor

      Hello ,

       

      I tried with below option 

       

      testRunner.testCase.testSteps['Properties'].setPropertyValue("Date", Date.toString());

      testRunner.testCase.testSteps['Properties'].setPropertyValue("Timestamp", Timestamp.toString());

       

      .

       

      Then I tried some exploration on how to setproperty on test cases level and tried below solution 

       

      String timestamp=new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC"))
      testRunner.testCase.setPropertyValue( "TimeStamp", timestamp )
      log.info(timestamp);

       

      String date = new Date().format("yyyy-MM-dd")
      testRunner.testCase.setPropertyValue( "Date", date )
      log.info(date);

       

      I retrieve value from test case properties  using "Get Data" and used in Request body and request Header Parameter and it worked! 

       

      Thank you for your reply. I got lead to search in right direction! 

       

      Thanks,

      Neha