Random Date Project Property
Hello all!
I'm hoping that somebody can help me with this request - I'm a complete novice at this type of coding. I need to create a Project Property variable to randomize a date in my Soap Request with the following parameters:
- Year is 2014 or 2015 only
- Any month 01-12 (has to have the leading 0 if single digit)
- Day is always 01
- Format example: YYYY-MM-DD (2014-06-01)
So far - without even toggling between year of 2014 and 2015 - I have a value of ${=String.valueOf('2014-'&'(Math.Random(01-12))'&'-01')}, but get error:
The string 'No signature of method: java.lang.String.and() is applicable for argument types: (java.lang.String) values: [(Math.Random(01-12))]
Possible solutions: any(), find(java.lang.String), any(groovy.lang.Closure), find(), find(groovy.lang.Closure), find(java.lang.String, groovy.lang.Closure)' is not a valid AllXsd value
Another way to accomplish this could be using an array, but I'm not sure how to set that up in a Property one-liner either.
Thank you. Any help or guidance would be greatly appreciated!!
Hi,
You're welcome, happy to help! :-)
I'm not sure, but think the 00:00:00 is the HH:MM:SS part of the XML DateTime datatype e.g.
<startdate>2002-05-30T09:00:00</startdate>
see http://www.w3schools.com/schema/schema_dtypes_date.asp
Are you using a SOAP or XML request with that data type? If so, it will probably force your date to include the time portion.
Cheers,
Rup
Hi,
Don't worry, I'm happy to help and wondered if you might also want to know how to use the script inline as a property expansion, as its more of a mouthful than the first expression was!
I think the short answer is (use semicolons to separate the statements when using inline):
${=def now = new Date().parse("yy-MM-dd", "14-06-01");now.month = 6 + new Random().nextInt(18);now.format("yy-MM-dd")}
But, personally, especially for larger scripts I don't do this as it isn't very readable. Instead I would simply add a Groovy TestStep before your Test Request TestStep and then add the result to a property on the (TestCase) 'context' e.g.
def now = new Date().parse("yy-MM-dd", "14-06-01")
now.month = 6 + new Random().nextInt(18)
context["randomDate"] = now.format("yy-MM-dd")Then in the Test Request TestStep, all you need to reference and insert the value is:
${=randomDate}
(this method may serve you better in the future)
Make sense?
Cheers,
Rup