Hi
I have below json response format.
My all the data is constant except the date which chnages every day. due to this i am not able to validate the response using contains assertion. Is there any way through script i can chnage date in my contains assertion as per my requirement.?
Date could be yestarday or 7 days back.
[
{
"locatonID": "a036000000lRWFi",
"locationLevelInfo": [
{
"ssidName": "McDonalds Free WiFi",
"ssidLevelInfoList": [
{
"date": "31-07-2018 00:00:00",
"ssidDayLevelInfoList": [
{
"loyalUserCount": 47,
"newUserCount": 6,
"tonnage_in": 9745977.19,
"tonnage_out": 10456271.34,
"throughput_kbps": 505.18,
"totalUserCount": 53
}
]
}
Solved! Go to Solution.
add a groovy step to create your variable date
If you want today's date as the date to check you can add this
myDate = new Date() context.testCase.setPropertyValue("date", myDate.format("MM-dd-yyyy 00:00:00"))
and then in your assertion step put this in to pull the variable from your property
${#TestCase#date}
if you are wanting to dynamically get tomorrows date then
myDate = new Date() myDate = myDate - 1 context.testCase.setPropertyValue("date", myDate.format("MM-dd-yyyy 00:00:00"))
testRunner.testCase.testSuite.project.getTestSuiteList().each { it.getTestCaseList().each { tc -> myDate = new Date() tc.setPropertyValue("date", myDate.format("dd-MM-yyyy 00:00:00")) } }
add a groovy step to create your variable date
If you want today's date as the date to check you can add this
myDate = new Date() context.testCase.setPropertyValue("date", myDate.format("MM-dd-yyyy 00:00:00"))
and then in your assertion step put this in to pull the variable from your property
${#TestCase#date}
if you are wanting to dynamically get tomorrows date then
myDate = new Date() myDate = myDate - 1 context.testCase.setPropertyValue("date", myDate.format("MM-dd-yyyy 00:00:00"))
Thanks for the solution. it works as per my requirment.
One more doubt, how do i set this to all the test cases present in all the test suites in the project.
Actually i am created a project which contains multiple test suites and every test suite contains multiple test cases.
I am trying something like below but it is not working -
testRunner.testCase.testSuite.project.getTestSuiteList().each
{
it.getTestCaseList().each
{
myDate = new Date()
context.testCase.setPropertyValue("date", myDate.format("dd-MM-yyyy 00:00:00"))
log.info myDate
}
}
testRunner.testCase.testSuite.project.getTestSuiteList().each { it.getTestCaseList().each { tc -> myDate = new Date() tc.setPropertyValue("date", myDate.format("dd-MM-yyyy 00:00:00")) } }
could you please explain how it works? what is
tc ->
It's just giving some variable name to the test cases that you are iterating over. So for example, it could have been:
testRunner.testCase.testSuite.project.getTestSuiteList().each { thisTestSuite -> thisTestSuite.getTestCaseList().each { thisTestCase -> myDate = new Date() thisTestCase.setPropertyValue("date", myDate.format("dd-MM-yyyy 00:00:00")) } }
If you don't name the variable by using the arrow, then the variable name will be "it".
I gave it a name because at that stage we are already inside a loop that had a variable called "it", so it would be confusing to use "it" again.
This arrow notation is used for Closures, you can learn more about them at http://groovy-lang.org/closures.html.