我知道在 Capybara 中,你可以这样做:
page.should have_css("ol li", :count => 2)
但是,假设页面只有一个匹配元素,则错误描述性不是很强:
1) initial page load shows greetings
Failure/Error: page.should have_css("ol li", :count => 2)
expected css "ol li" to return something
代替这个相当模糊的错误消息,有没有办法以这样的方式编写断言,错误输出类似于'当匹配'ol li'时,预期:2,找到:1'。显然,我可以自己为这种行为制定一个自定义逻辑——我问有没有办法“开箱即用”?
对于它的价值,我正在使用 Selenium 驱动程序和 RSpec。
page.should have_css("ol li", :count => 2)
不会已经实现。
我更喜欢这个。
expect(page).to have_selector('input', count: 12)
好吧,似乎没有开箱即用的支持,我写了这个自定义匹配器:
RSpec::Matchers.define :match_exactly do |expected_match_count, selector|
match do |context|
matching = context.all(selector)
@matched = matching.size
@matched == expected_match_count
end
failure_message_for_should do
"expected '#{selector}' to match exactly #{expected_match_count} elements, but matched #{@matched}"
end
failure_message_for_should_not do
"expected '#{selector}' to NOT match exactly #{expected_match_count} elements, but it did"
end
end
现在,您可以执行以下操作:
describe "initial page load", :type => :request do
it "has 12 inputs" do
visit "/"
page.should match_exactly(12, "input")
end
end
并获得如下输出:
1) initial page load has 12 inputs
Failure/Error: page.should match_exactly(12, "input")
expected 'input' to match exactly 12 elements, but matched 13
它现在可以解决问题,我会考虑制作 Capybara 的这一部分。
我认为以下内容更简单,输出相当清晰,并且不需要自定义匹配器。
page.all("ol li").count.should eql(2)
然后打印出错误:
expected: 2
got: 3
(compared using eql?)
(RSpec::Expectations::ExpectationNotMetError)
编辑: 正如@ThomasWalpole 所指出的,使用 all
会禁用 Capybara 的等待/重试,因此@pandaPower 的上述答案要好得多。
这个怎么样?
within('ol') do
expect( all('.opportunity_title_wrap').count ).to eq(2)
end
within
,它在 all
的结果上调用 .count
禁用等待/重试。通过在 all
的结果上调用 count
(一个空的“数组”是一个有效的返回),您可以将其转换为整数并进行比较。如果该比较失败,则期望失败。相反,如果您将 count 选项传递给 Capybara 的匹配器之一,capybara 将等待/重试查找指定的选择器,直到 count 选项匹配(或 Capybara.default_max_wait_time 到期)。
@pandaPower 的回答非常好,但对我来说语法略有不同:
expect(page).to have_selector('.views-row', :count => 30)
不定期副业成功案例分享
have_css
:expect(page).to have_css('input', count: 12)