更新的答案(应该在 RSpec 3.0+ 中弃用匹配器):
expect(page).to have_selector(:link_or_button, 'Click me')
Before:
page.should have_selector(:link_or_button, 'Click me')
关注自click_link_or_button
其定义如下:https://github.com/jnicklas/capybara/blob/master/lib/capybara/node/actions.rb#L12 https://github.com/jnicklas/capybara/blob/master/lib/capybara/node/actions.rb#L12
def click_link_or_button(locator)
find(:link_or_button, locator).click
end
alias_method :click_on, :click_link_or_button
它调用一个选择器:link_or_button
。该选择器在这里定义:https://github.com/jnicklas/capybara/blob/master/lib/capybara/selector.rb#L143 https://github.com/jnicklas/capybara/blob/master/lib/capybara/selector.rb#L143
Capybara.add_selector(:link_or_button) do
label "link or button"
xpath { |locator| XPath::HTML.link_or_button(locator) }
end
它调用这个方法:http://rdoc.info/github/jnicklas/xpath/XPath/HTML#link_or_button-instance_method http://rdoc.info/github/jnicklas/xpath/XPath/HTML#link_or_button-instance_method
# File 'lib/xpath/html.rb', line 33
def link_or_button(locator)
link(locator) + button(locator)
end
所以我尝试检查选择器是否存在并且它有效:
page.should have_selector(:link_or_button, 'Click me')