Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
5 years ago

API Masterminds - How to use dynamic properties to get today’s date?

Hello API Masterminds! 

 

I’m sure you are already familiar with the Property Expansion syntax in ReadyAPI and often use it to transfer data from one part of your test to another.

 

But, did you know that with a similar syntax you can inject results of an arbitrary Groovy code execution to your tests and thus feed them with some dynamic data?

 

This functionality is called Dynamic Properties.

 

For example, you can insert a random number to a request body the following dynamic property as shown in the below screenshot:  

 

${=(int)(Math.random()*1000)}

 

 

Each time you click the “Send“ button, the request body will contain a new random integer number.

 

Questions:

 

If you need today’s date in your test, what Groovy script would you use in the dynamic property for this? 

 

If you already use dynamic properties in your tests, please share the use cases and provide script examples. 

6 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    This is not only nice question but also nice feature as that will help to avoid another groovy script test step for small tasks.

     

    However, there is a limitation that there can't have nested "${..}" in the expression.

     

    Date is the most commonly used dynamic value for both SOAP or REST requests apart from generating value for unique field such as ID.

     

    If unique id is needed as value in the request payload,

    Number type

    ${= System.currentTimeMillis()}

    String type with unique

    ${= UUID.randomUUID()}

    For date,

    Below sample with future formatted date with time zone

     

    https://community.smartbear.com/t5/SoapUI-Pro/Add-minutes-to-current-date-time-using-groovy/m-p/196839#M45011

    WIth JDK 8

    ${= java.time.ZonedDateTime.now(java.time.ZoneId.of("GMT")).plusMinutes(40).format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))}

    The above are oneliners, so no need to create separate script test step, just embed the code snippet in the request step directly.

    Note that user have to use them along with package as shown in the date example.

    • HimanshuTayal's avatar
      HimanshuTayal
      Community Hero

      sonya_m ,

       

      Yes we can generate today's date via dynamic property but as per me it will be a duplicacy to write same code again and again to overcome this what we are doing is writing a small script in TestController to generate Date in our desired format and storing it in project level

       

      and using it in our Request from there.

       

      def startDate = new Date() 
      def date = startDate.format("dd-MM-yyyy");
      testRunner.testCase.testSuite.project.setPropertyValue("currentDateDDMMYYYY_Format",date.toString());
      • nmrao's avatar
        nmrao
        Champion Level 3
        It becomes static value then if the same is used in the test run.
         
        Dynamic property is intended for one line of code. So, no optiomation can be done for one line code, I guess.
         
        EDIT: There was typo, not online, meant for one line.
         
        By the way, Dynamic properties is there for reason to simplify the work i.e., directly evaluate the value within the request using the expression which is better way than compute the value in a script, set it in property and retrieve it before submitting.the request.