时间戳 Ruby 水豚黄瓜
Timestamps Ruby Capybara Cucumber
我有一个测试,在成功交易后生成一个参考号。在这个参考号中是 (hhmmss) 的时间戳。此时间戳是在用户按下提交时捕获的,而不是在出现带有可见参考号的确认页面时捕获的。因此,我不能使用 Time.now,因为它总是错误的。
我想做的(如果可能的话)是创建一个确认测试,我在其中查看参考编号并可以确认时间间隔在 x 和 y 之间,y 为 x - 20 秒(以允许出现确认页面)。
参考确认信息如下:
"Your transaction reference number is: 0 16123 #{timestamp} A1"
参考号出现在句子的中间,所以我想按照:
expect('location-of-text').to have_text "Your transaction reference number is: 0 1612 {timestamp_range_here} A1"
我不确定是否可以做我需要的。
任何解决方案的帮助都会很棒
您可以找到您的元素、提取文本、解析时间戳并检查它是否在您的范围内。假设您的原始字符串看起来像这样 "Your transaction reference number is: 0 16123 120000 A1"
,那么您可以执行以下操作:
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)
假设在服务器端的记录中设置了时间,一个更简洁的解决方案是使用 Timecop gem 在特定时间冻结测试,从而修复应该在给定时间生成的字符串时间.
我有一个测试,在成功交易后生成一个参考号。在这个参考号中是 (hhmmss) 的时间戳。此时间戳是在用户按下提交时捕获的,而不是在出现带有可见参考号的确认页面时捕获的。因此,我不能使用 Time.now,因为它总是错误的。
我想做的(如果可能的话)是创建一个确认测试,我在其中查看参考编号并可以确认时间间隔在 x 和 y 之间,y 为 x - 20 秒(以允许出现确认页面)。
参考确认信息如下:
"Your transaction reference number is: 0 16123 #{timestamp} A1"
参考号出现在句子的中间,所以我想按照:
expect('location-of-text').to have_text "Your transaction reference number is: 0 1612 {timestamp_range_here} A1"
我不确定是否可以做我需要的。
任何解决方案的帮助都会很棒
您可以找到您的元素、提取文本、解析时间戳并检查它是否在您的范围内。假设您的原始字符串看起来像这样 "Your transaction reference number is: 0 16123 120000 A1"
,那么您可以执行以下操作:
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)
假设在服务器端的记录中设置了时间,一个更简洁的解决方案是使用 Timecop gem 在特定时间冻结测试,从而修复应该在给定时间生成的字符串时间.