Forum Discussion
- groovyguyCommunity Hero
You can make use of inline-groovy-scripting. It's similar to referencing something with Property Expansion. Here's an example below:
${= Calendar.getInstance(TimeZone.getTimeZone('GMT')).format("yyyy-MM-dd")}
Property Expansion uses the format ${...}. If you use ${=...} that tells ReadyAPI to evaluate whatever comes after the equal sign as a groovy script and will put the result in there.
- mgroen2Super Contributor
Hi,
thanks that worked! Thanks a lot for your code.
Now, is there also a possibility to manipulate the date with -1 day?
So I can run the same test but now with a date = "yesterday" ?
- groovyguyCommunity Hero
mgroen2: That is possible, but that is going to increase the complexity of the in-line groovy script. Also, if you ever need to re-use this in another request, that will mean copy and pasting the same code over and over. One way I get around this is to put in a groovy script test step that writes to a properties step, and I reference the data I generate from there.
The groovy script you are looking for inline, though, is:
${= import static java.util.Calendar.* def now = Calendar.getInstance(TimeZone.getTimeZone('GMT')) String TimeStamp; now[DAY_OF_WEEK] = now[DAY_OF_WEEK] - 1; Timestamp = now.format("yyyy-MM-dd"); // log.info(Timestamp); return Timestamp;}
As you can see, that can insert a lot of groovy script into the request that you otherwise might not want to see. And if you need to re-use it, changing it later may get out of hand. If you write a groovy script test step and a properties step (using the name of "Properties" for it in this example, you can use this snippet:
import static java.util.Calendar.* def datetimeDayFunct(int offset) { // In: Offset, in days // Out: Return generated time based off of now + offset in days. def now = Calendar.getInstance(TimeZone.getTimeZone('GMT')) String TimeStamp; now[DAY_OF_WEEK] = now[DAY_OF_WEEK] + offset; Timestamp = now.format("yyyy-MM-dd"); return Timestamp; } def propertiesStep = context.testCase.testSteps["Properties"]; propertiesStep.setPropertyValue("yesterday", datetimeDayFunct(-1)); propertiesStep.setPropertyValue("tomorrow", datetimeDayFunct(1));
This will provide you a snippet of code that, if you ever need to generate a new property, you can append and add to it. Your time generation is in one, central place and you can easily maintain it there.
Related Content
Recent Discussions
- 30 minutes ago
- 26 days ago