1
votes

I have a test where a reference number is generated after a successful transaction. Within this reference number is a timestamp of (hhmmss). This timestamp is captured at the time the user presses submit, not when the confirmation page appears with the reference number visible. For this reason, I can't use Time.now as it will always be wrong.

What I would like to do (if possible) is create a confirmation test where I look within the reference number and can confirm that the timestap is between x and y, with y being x - 20 seconds (to allow for the confirmation page to appear).

The confirmation message with reference is as follows:

"Your transaction reference number is: 0 16123 #{timestamp} A1"

The reference number appears in the middle of a sentence so I'd like to be along the lines of:

expect('location-of-text').to have_text "Your transaction reference number is: 0 1612 {timestamp_range_here} A1"

I'm not sure if it's possible to do what I need.

Any help for a solution would be great

2
Wouldn't it be easier to take the confirmation message, and make sure that the timestamp is in a particular range? So for instance taking the Time.now and taking away the timestamp, and if the result is less than or equal to 20, but greater than 0, you could say it's a pass?KyleFairns
I've been trying that too but I've struggled to isolate the timestamp in the string.Tom
stackoverflow.com/questions/36572028/… This is my other question related to this problem mentioning your approach @kyle FairnsTom

2 Answers

1
votes

You can find your element, extract text, parse timestamp and check that it is within your range. Let's say your original string looks like this "Your transaction reference number is: 0 16123 120000 A1", then you can do the following:

text = find(element, text: /Your transaction reference number is: 0 16123 .* A1/).text
timestamp = text[/0 16123 (.*) A1/, 1]        #=> 120000
time = DateTime.strptime timestamp, '%H%M%S'  #=> Tue, 12 Apr 2016 12:00:00 +0000
expect(time).to be_between(Time.now - 20.seconds, Time.now)
0
votes

Assuming the time is set in the record on the server side a cleaner solution is to use the Timecop gem to freeze the test at a specific time and thereby fix the string that should be produced at a given time.