cancel
Showing results for 
Search instead for 
Did you mean: 

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

Highlighted
Community Manager

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)}

 

dynamic.png

 

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. 


Sonya Mihaljova
Community and Education Specialist

 Join the virtual SmartBear Connect user conference on April 27-28! Get your free virtual ticket today!
6 REPLIES 6
Highlighted
Community Hero

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

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/1968...

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.



Regards,
Rao.
Highlighted
Community Leader

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

@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());

Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
Highlighted
Community Hero

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

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.


Regards,
Rao.
Community Leader

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

if it's only intended for online purpose than it's ok but one should not use it in project work.

 


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
Highlighted
Community Hero

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

Corrected typo. Meant that one line.
Dynamic property works in SoapUi project, not online.
Thanks for catching typo.


Regards,
Rao.
Highlighted
Community Leader

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

@nmrao :

 

Seems i have proved that i have 1 quality of good tester, finding a bug

 

Smiley Happy Smiley LOL Smiley LOL Smiley LOL Smiley LOL


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal