Tom
Tom

Reputation: 1105

Replacing text with regexp Ruby Capybara

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

Upvotes: 2

Views: 1060

Answers (1)

Vitalii Elenhaupt
Vitalii Elenhaupt

Reputation: 7326

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/

Upvotes: 0

Related Questions