cancel
Showing results for 
Search instead for 
Did you mean: 

how to increment date for each test run

SOLVED
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

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: how to increment date for each test run

@Nancy2 ,

 

In your test case, place below a Groovy Script test step before the Request test step, so that it can generate the next date and also remembers the same for next time not to generate the duplicate date.

 

Selection_029.png

 

And in the request, use <element>${#TestCase#NEW_DATE}</element>

 

Here is the text of the above script

https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/date/CreateNewDate.groovy

 

Doesn't require any additional libraries.



Regards,
Rao.
28 REPLIES 28
Community Hero

Re: how to increment date for each test run

What format?


Regards,
Rao.
Occasional Contributor

Re: how to increment date for each test run

YYYY-MM-DD

e.g. 2019-02-09

Community Hero

Re: how to increment date for each test run

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


Regards,
Rao.
Occasional Contributor

Re: how to increment date for each test run

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

Community Hero

Re: how to increment date for each test run

Instead you could use:

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


Regards,
Rao.
Community Hero

Re: how to increment date for each test run

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 title of the post to something more descriptive? This will help people when searching for problems.
Community Hero

Re: how to increment date for each test run

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


Regards,
Rao.
Community Leader

Re: how to increment date for each test run

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.

Occasional Contributor

Re: how to increment date for each test run

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

New Here?
Join us and watch the welcome video:
Top Kudoed Authors