Forum Discussion

jonrey's avatar
jonrey
Occasional Contributor
10 years ago

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

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    Well, there are probably a few ways to do this, here is one for your to try:

     

    //To get either 2014 or 2015, I started with 2014 and added either 0 or 1 (randomly generated) to it.

    def randomYear = new Random().nextInt(2)+2014

     

    //To get a 0 padded month between 01 and 12, I generate an integer between 1 and 12.

    def randomMonth = String.valueOf(new Random().nextInt(12)+1)

     

    //Then finally combine the parts, whilst padding the month (as a String) to 2 digits. 
    log.info randomYear + '-' + randomMonth.padLeft( 2, '0' ) + '-01'

     

    To get this in a one liner:

     

    log.info new Random().nextInt(2)+2014 + '-' + String.valueOf(new Random().nextInt(12)+1).padLeft( 2, '0' ) + '-01'

     

    prints

    • Thu May 21 19:57:40 UTC 2015:INFO:2014-06-01

    or as a property expansion like you had:

     

    ${=new Random().nextInt(2)+2014 + '-' + String.valueOf(new Random().nextInt(12)+1).padLeft( 2, '0' ) + '-01'}

     

    Does this do what you want?

     

    Cheers,

    Rupert

     

    • jonrey's avatar
      jonrey
      Occasional Contributor

      Hello Rupert,

       

      Thank you very much for your well thought-out and impressive reply!

       

      This is SO close!  The property expansion returns 2014-02-01T00:00:00 and I'm not sure where the extra "T00:00:00" part is coming from.  Certainly gets me much farther than I was - and helps me learn the syntax - so I'm going to play around with it some more to see if I can get it the last 5% :)

       

      Cheers!

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        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