使用regexp Ruby Capybara替换文本 [英] Replacing text with regexp Ruby Capybara
问题描述
我有一个问题已经连接到这个问题,但这个问题是一个不同的方法。
I have a question running already linked to this problem, but this question is for a different approach.
我在网页上有一个文本字符串,事务完成后的时间戳(hhmmss)。
I have a string of text on a webpage that produces includes a timestamp (hhmmss) after a transaction is complete.
我不能使用Time.now,因为处理时间取决于许多因素。
I can't use Time.now as the processing time varies depending on many factors.
页面上的文字是:
"Your transaction reference number is: 0 16123 (timestamp is here) A1"
我测试的文本:
expect(find(location)).to have_text trans_text
我可以将位置文本设置为变量:
Could I set the location text as a variable:
trans_text = find(location, text: 'Your transaction reference number is: 0 16123 (timestamp is here) A1')
然后用正则表达式替换时间戳,然后期望时间戳在两次之间?
then replace the timestamp with a regex then expect that timestamp to be between two times?
我已经尝试:
trans_text = find(location, text: 'Your transaction reference number is: 0 16123 (\d+) A1')
但没有快乐。我收到以下错误:
But had no joy. I get the following error:
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".
如何输入正则表达式来替换文本的时间戳部分?
How could I input a regex to replace the timestamp part of the text?
感谢
推荐答案
根据 Capybara文档:
text(String,Regexp) - 只找到包含此文本或匹配此正则表达式的元素
text (String, Regexp) — Only find elements which contain this text or match this regexp
通过正则表达式(而不是字符串):
You have to search by regular expression (instead of a string):
find('p', text: /Your transaction reference number is: 0 16123 (\d+) A1/)
或在一个步骤中进行验证:
Or do verification in one step:
expect(page).to have_text /Your transaction reference number is: 0 16123 (\d+) A1/
这篇关于使用regexp Ruby Capybara替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!