Enter past date in text box.

1 Answer 42 Views
Coded Steps Elements Test Execution
Top achievements
Rank 1
Michael asked on 11 Sep 2023, 06:13 PM


We are coming over from iMacros and running into a few tough spots.

In the past we used variables to enter dates 7, 14 or 21 days in the past.

Is there a way we can do this with Test studio?

The format we need would be "09/01/2023+" the plus sign is included in how we enter the date.

With iMb we had the variable as "d=new Date();d.setDate(d.getDate()-21);var day=d.getDate().toString(); if (day.length < 2) {day = \"0\" + day;} var month = (d.getMonth() + 1).toString(); if (month.length < 2) {month = \"0\" + month;} var year = d.getFullYear().toString(); month+'/'+day+'/'+year+'+';""

I have been trying for a week now to figure this out. Thanks!


1 Answer, 1 is accepted

Sort by
Telerik team
answered on 13 Sep 2023, 07:20 AM

Hi Michael,

Test Studio has a build in feature for random (not current) date and time. It is accessed from the step builder panel.

The fields in that step that will help in your case are start and end date (for setting the range in the past) and date format (in your case 'MM/dd/yyyy"). I am leaving the article from the Test Studio documentation for more detailed information.

The generated date can then be passed to other steps using "DataBindVaraibleName". The name that is set in this field will be visible in the {Bindings} property of the other step


I am not sure if i understood you corectly about the time intervals (7/14/21). I am taking them as an examples and not specific values. But if they are indeed specific values, you can set them using a coded step. In it the current date is extracted by the specific value (days) and the result is the date in the past. 

That's one way of doing it (C#) so you need a coded step in the test:

DateTime currentDate = DateTime.Now;
int daysToThePast = 7*(-1);
DateTime pastDate = currentDate.AddDays(daysToThePast);
string outputDate = pastDate.ToString("MM/dd/yyyy") + "+";

 *Here you can also find more information about the DateTime class. 

The 7 is multiplied by -1 so when we add it to the date it actually removes 7 days from it. The format can be change in the "ToString()" method as you wish.

After the past date is created as a string you can then set it to a step using "SetExtractedValue" method. 

SetExtractedValue("PastDateBind", outputDate);

The first argument is a string and it is used in the properties panel of the step to bind the variable. You cam choose whatever you like there.

The second one is the value or the variable that holds it which will be use in the step.


 I hope that this will help you. 

Regards, Alexander Progress Telerik

Virtual Classroom is the free self-paced technical training portal that gets you up to speed with Telerik and Kendo UI products including Telerik Test Studio! Check it out at https://learn.telerik.com/.
Coded Steps Elements Test Execution
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question