Replacing text with regexp Ruby Capybara

怎甘沉沦 提交于 2019-12-14 01:53:42

问题


I have a question running already linked to this problem, but this question is for a different approach.

I have a string of text on a webpage that produces includes a timestamp (hhmmss) after a transaction is complete.

I can't use Time.now as the processing time varies depending on many factors.

The text on page is:

"Your transaction reference number is: 0 16123 (timestamp is here) A1"

I'll be looking within the element to read the text in my test with:

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?

I have tried doing:

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?

Thanks


回答1:


According to Capybara documentation:

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/


来源:https://stackoverflow.com/questions/36572028/replacing-text-with-regexp-ruby-capybara

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!