我有一个问题已经链接到这个问题,但这个问题适用于不同的方法。
我在一个网页上有一个字符串,它在交易完成后生成一个时间戳(hhmmss)。
我不能使用Time.now,因为处理时间因多种因素而异。
页面上的文本为:
"Your transaction reference number is: 0 16123 (timestamp is here) A1"
我将在元素中查找以阅读我的测试中的文本:
expect(find(location)).to have_text trans_text
我可以将位置文本设置为变量吗:
trans_text = find(location, text: 'Your transaction reference number is: 0 16123 (timestamp is here) A1')
然后用正则表达式替换时间戳,然后期望时间戳介于两次之间?
我试过做:
trans_text = find(location, text: 'Your transaction reference number is: 0 16123 (d+) A1')
但是没有快乐。我得到以下错误:
Unable to find css "#main > div > div.section-content > div.two-col.retention-success > div.second-col > div.alert-complete > p" with text "Your retention certificate number is: 0 16123 (/d+) A1".
如何输入正则表达式来替换文本的时间戳部分?
感谢
根据Capybara文档:
text(String,Regexp)--只查找包含此文本或与此Regexp 匹配的元素
您必须通过正则表达式(而不是字符串)进行搜索:
find('p', text: /Your transaction reference number is: 0 16123 (d+) A1/)
或者一步验证:
expect(page).to have_text /Your transaction reference number is: 0 16123 (d+) A1/