【问题标题】:Replacing text with regexp Ruby Capybara用正则表达式 Ruby Capybara 替换文本
【发布时间】:2016-08-02 23:49:11
【问题描述】:

我有一个问题已经与这个问题相关联,但这个问题是针对不同的方法。

我的网页上有一个文本字符串,在事务完成后会生成一个时间戳 (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".

如何输入正则表达式来替换文本的时间戳部分?

谢谢

【问题讨论】:

  • 时间戳是什么格式,能举个例子吗?尝试(\w+) 而不是(\d+),甚至是(.*+)
  • 格式为 (hhmmss)。 “'您的交易参考号是:0 16123 120000 A1')”。 strftime 将是 ('%H%M%S')
  • 我已编辑添加显示我遇到的错误

标签: ruby regex selenium-webdriver cucumber capybara


【解决方案1】:

根据Capybara documentation

text (String, 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/

【讨论】:

  • @Tom 这有帮助吗?
猜你喜欢
  • 2017-10-18
  • 2014-03-11
  • 2015-07-25
  • 2017-07-12
  • 2017-01-21
  • 2019-11-29
  • 2012-10-21
相关资源
最近更新 更多