Forum Discussion

jonrey's avatar
jonrey
Occasional Contributor
9 years ago
Solved

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 Requ...
  • rupert_anderson's avatar
    rupert_anderson
    9 years ago

    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

  • rupert_anderson's avatar
    rupert_anderson
    9 years ago

    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