Ask a Question

how to increment date for each test run

SOLVED
Nancy2
Occasional Contributor

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

28 REPLIES 28
nmrao
Champion Level 3

What format?


Regards,
Rao.
Nancy2
Occasional Contributor

YYYY-MM-DD

e.g. 2019-02-09

nmrao
Champion Level 3

You can use:
new Date().format('yyyy-MM-dd')


Regards,
Rao.
Nancy2
Occasional Contributor

This is not working as I can give direct date in the <Value> tag

e.g.

 <Value><2019-02-02></value>

 

For below, getting error- String was not recognized as a valid DateTime.

<Value>${=(new Date().format("yyy-MM-dd")}</Value>

 

Requirement is originally I am giving date as <2019-02-02> and for the second time date should be passed as <2019-02-03> and so on..

 

Regards

nmrao
Champion Level 3

Instead you could use:

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


Regards,
Rao.
richie
Community Hero

Hi @Nancy2 

 

I think you're missing a parenthesis character and a 'y' character in the string

 

I use the following 

 

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

you are using 

 

${=(new Date().format("yyy-MM-dd")}

I don't know if wrapping the format pattern value in double quote marks makes a difference - but as I say - the above that I use works all the time,

 

you want dates in the format '2019-02-02',  so I think the string you want is as follows:

 

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

hope I've been clear - I tend to ramble!

 

Cheers,

 

richie

 

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
nmrao
Champion Level 3

@Nancy2, if the above did not help, then you need to provide the use case with details.


Regards,
Rao.
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
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

cancel
Showing results for 
Search instead for 
Did you mean: