Forum Discussion
SiKing
12 years agoCommunity Hero
When I compare timestamps, I chose one of two options:
1. Easy, but less precision
Use XPath Match. The XPath expression will be something like
This example is accurate only to the minute, and completely ignores the timezone!
2. Difficult, but higher precision
Use Script assertion. You will have to read in the String of your timestamp and convert that to a Date object, then do the comparison. Your code might look something like the following.
The comparison can be done to the milisecond and takes timezones into consideration.
1. Easy, but less precision
Use XPath Match. The XPath expression will be something like
substring(//*:DatumKuendigung, 0, 17)and the expected result, if you are comparing to "right now", will be something like
${=String.format("%tFT%tR", new Date(), new Date())}This example is accurate only to the minute, and completely ignores the timezone!
2. Difficult, but higher precision
Use Script assertion. You will have to read in the String of your timestamp and convert that to a Date object, then do the comparison. Your code might look something like the following.
def nowDate = new Date()
def sdf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")
def currentDate = sdf.parse(context.expand('${' + context.currentStep.name + '#ResponseAsXml#//*:DatumKuendigung}'))
// adjust how accurate you need the comparison; following checks that the time is accurate to within 5 seconds
def secondsDiff = Math.abs((nowDate.getTime() - currentDate.getTime()) / 1000)
assert secondsDiff < 5 : "Too great of a difference with server!"
The comparison can be done to the milisecond and takes timezones into consideration.