Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

automation - Karate- Need help to assert a single dimension array for date range

I am trying to assert the values inside a single dimensional array. I have tried using match but it looks like the date ranges cannot be asserted.

Below is the object array:

[
"2019-04-24T17:41:28",
"2019-04-24T17:41:27.975",
"2019-04-24T17:41:27.954",
"2019-04-24T17:41:27.93",
"2019-04-24T17:41:27.907",
"2019-04-24T17:41:27.886",
"2019-04-24T17:41:27.862",
"2019-04-24T17:41:27.84",
"2019-04-24T17:41:27.816",
"2019-04-24T17:41:27.792"
]

I am trying to assert each values between the following date ranges:

MinDate:2019-04-24T17:25:00.000000+00:00
MaxDate:2019-04-24T17:50:00.000000+00:00

I have tried the following but none works:

* match dateCreated == '#[]? _.value >= fromDate'
 * eval for(var i = 0; i < responseJson.response.data.TotalItemCount; i++) dateCreated.add(responseJson.response.data.Items[i].DateCreated)  karate.assert(dateCreated[i] >= fromDate)

Any hint/tip on how to go about it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here you go:

* def dateToLong =
"""
function(s) {
  var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
  var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
  return sdf.parse(s).time;
} 
"""
* def min = dateToLong('2019-04-24T17:25:00.000')
* def max = dateToLong('2019-04-24T17:50:00.000')
* def isValid = function(x){ var temp = dateToLong(x); return temp >= min && temp <= max }

* def response =
"""
[
"2019-04-24T17:41:27.975",
"2019-04-24T17:41:27.954",
"2019-04-24T17:41:27.93",
"2019-04-24T17:41:27.907",
"2019-04-24T17:41:27.886",
"2019-04-24T17:41:27.862",
"2019-04-24T17:41:27.84",
"2019-04-24T17:41:27.816",
"2019-04-24T17:41:27.792"
]
"""
* match each response == '#? isValid(_)'

Please refer the docs if you have doubts about any of the keywords. I removed the first date in the list because it was not consistent, but you have enough info to handle it if needed - you may need some conditional logic somewhere.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...