I need to pass current date and past date in a POST request in Karate. This is what I am doing:
In Background I calculate the current date and past date - Background:
def todaysdate =
"""
function(curdate){
var currentDate = new Date();
karate.log(currentDate);
return currentDate;
}
"""
And def minus30date =
"""
function(pasdate){
var pastDate = new Date();
pastDate.setDate(pastDate.getDate() - 30);
karate.log(pastDate);
return pastDate;
}
"""
The above code gives me the dates like this in the console:
[Date 2020-07-20T16:18:15.369Z]
[Date 2020-06-20T16:18:15.379Z]
The POST request is in Example section for the scenario outline:
| Individual ID Request |
| '{ "endDate": "2020-06-20T16:18:15.379Z", "startDate": "2020-07-20T16:18:15.369Z" }' |
How do I use the return variables in the request for start and end date? I tried putting things like '#(pastDate)' in the request but it does not work. I tried calling the functions in scenario outline and assigning them to a variable and use it in the request but that does not work too.
Any help would be appreciated.
you date functions can be like modified as below,
* def getDate =
"""
function(numberOfDays){
var date = new Date();
date.setDate(date.getDate() + (numberOfDays));
return date.toString()
}
"""
after which you can call the function with params like below,
* def todaysdate = getDate(0)
* def minus30date = getDate(-30)
* def payload = {"startDate" : "#(todaysdate)", "endDate" : "#(minus30date)"}
or
* def payload = {"startDate" : "#(getDate(0))", "endDate" : "#(getDate(-30))"}