mercredi 10 août 2016

How to test for multiple instances of strings that match the same regex on a page with Capybara / Ruby

Trying to test a page that has 3 house prices on it. Each house price could be anything as low as $10,000 and as high as $1,000,000, so I wrote a regex to try to catch any possible iterations of the numbers:

\$[\d]{0,1},{0,1}[\d]{2,3},{0,1}[\d]{0,3}

The problem that I'm running into is that I can't figure out how to correctly form the Capybara expression to verify that there are 3 iterations. The first attempt here:

Then(/^I should see (\d+) listings which contain the price in the correct format$/) do |times|
 expect(@page.price).to have_text("\$[\d]{0,1},{0,1}[\d]{2,3},{0,1}[\d]{0,3}", :count => times)
end

... gives this error:

Then I should see 3 listings which contain the price in the correct format
# features/step_definitions/feature_format.rb:6
Unused parameters passed to Capybara::Queries::SelectorQuery : ["$[d]{0,1},{0,1}[d]{2,3},{0,1}[d]{0,3}"]
Unused parameters passed to Capybara::Queries::SelectorQuery : ["$[d]{0,1},{0,1}[d]{2,3},{0,1}[d]{0,3}"]
Unused parameters passed to Capybara::Queries::SelectorQuery : ["$[d]{0,1},{0,1}[d]{2,3},{0,1}[d]{0,3}"]

The second attempt here:

Then(/^I should see (\d+) listings which contain the price in the correct format$/) do |times|
 for i in 1..times.to_i do
  expect(@page.price).to assert_text("\$[\d]{0,1},{0,1}[\d]{2,3},{0,1}[\d]{0,3}")
  end
 end

... gives this error:

Then I should see 3 listings which contain the price in the correct format               # features/step_definitions/feature_format.rb:6
      Ambiguous match, found 3 elements matching css "div.psfm-hf-ft-price" (Capybara::Ambiguous)

Does anybody know what's going wrong here?

Aucun commentaire:

Enregistrer un commentaire