尝试测试一个有3个房价的网页。每个房价可能低至10,000美元,高达1,000,000美元,所以我写了一个正则表达式试图捕捉任何可能的数字迭代:
\$[\d]{0,1},{0,1}[\d]{2,3},{0,1}[\d]{0,3}
我遇到的问题是我无法弄清楚如何正确形成Capybara表达式以验证有3次迭代。第一次尝试:
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
...给出了这个错误:
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}"]
第二次尝试:
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
...给出了这个错误:
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)
有人知道这里出了什么问题吗?
答案 0 :(得分:1)
要使用正则表达式,您需要传递正则表达式而不是字符串。因此,要检查页面是否与正则表达式匹配3次,那么
expect(page).to have_text(/\$[\d]{0,1},{0,1}[\d]{2,3},{0,1}[\d]{0,3}/, count: 3)
如果@page.price
指的是包含所有3个价格的页面,那么它将是
expect(@page.price).to have_text(/\$[\d]{0,1},{0,1}[\d]{2,3},{0,1}[\d]{0,3}/, count: 3)
然而,根据"模棱两可的错误判断"我猜你的@ page.price选择器实际上可能正在选择一个只有一个价格的元素?如果是这种情况,你想要更像
的东西expect(page).to have_css('div.psfm-hf-ft-price', text: /\$[\d]{0,1},{0,1}[\d]{2,3},{0,1}[\d]{0,3}/, count: 3)
将检查页面是否有3个与给定css选择器匹配的元素,每个元素都具有与正则表达式匹配的内容