Forum Discussion

Nancy2's avatar
Nancy2
Occasional Contributor
6 years ago

how to increment date for each test run

Hi,

 

In my test step I need to pass new date in each test run. How can I do that?

 

Regards,

Preeti

      • nmrao's avatar
        nmrao
        Champion Level 3
        You can use:
        new Date().format('yyyy-MM-dd')
  • avidCoder's avatar
    avidCoder
    Super Contributor

    Hi Preeti,

     

    I couldn't understand your use cases. How do you want to proceed after the date increment. This is what you can try. Download joda-time and put inside home/SmartBear/bin/ext folder. Restart ReadyAPI and then use this code:-

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.swing.text.html.HTML.Tag;
    import org.joda.time.DateTime;
    import org.joda.time.format.DateTimeFormatter;
    import org.joda.time.format.ISODateTimeFormat;
    
    /*First figure out how many times you need to execute this. I have taken count as 5. 
    		You can set limit of the date creation based on the flag a/c to your choice.
    		This will generate the date for next 5 days.*/
    		for(int i=0; i<5; i++) {
    		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    	    Date date = new Date();    
    	    DateTimeFormatter parser = ISODateTimeFormat.date();
    	    DateTime dt = parser.parseDateTime(dateFormat.format(date));
    	    String nextDay = parser.print(dt.plusDays(i));
    	    System.out.println(nextDay);
               //Store nextDay to testCase Property.
    	    testRunner.testCase.setPropertyValue("Date", nextDay);
               //Now in XML pass "Date" inside this tag - <Value>${#TestCase#Date}</value>
    	   //Then run the XML file, to generate the response based on the "Date" you provided in <Value> Tag. 
    	    def tCase = testRunner.testCase.testSuite.testCases["Your testCase Name"]
    	    def tStep = tCase.testSteps["Your testStep name"]
    	    tStep.run(testRunner, context)
    		}

    Just try once, and let me know, if you face any issue.

     

    Regards,

    AvidCoder.

    • Nancy2's avatar
      Nancy2
      Occasional Contributor

      From all the replies what I understood is that use case is not very clear. Let me explain my use case. However, I'll try all the given solutions and let you know my observation-

       

      My application has a function that in one day only one record can be added in an account.

      For e.g. I can rent a hotel but I cannot post same activity same day means I cannot rent same hotel same day two times. So, this validation message display "Duplicate activity on same day"

       

       

      In my web method, I pass all other input parameters along with date using below syntax

      (${=(new Date().format('yyyy-MM-dd'))}).

      So first time when I run my test case, syntax works fine as it picks up today's date but running same test case same day will fail and give message as "Duplicate activity on same day" due to same date.

       

      My requirement is to pass different date each time when I execute the test case.

       

      Let me know if still more explianation is required for my use case.

       

      Regards