Ruby-on-rails – Capybara matcher for presence of button or link

capybararuby-on-rails

Users on web page don't distinguish between "button" and "link styled as button".
Is there a way to add check whether a "button or link" is present on page?

For example Capybara has step:

page.should have_button('Click me')

which does not find links styled as buttons.

Best Answer

Updated answer (should matcher is deprecated in RSpec 3.0+):

expect(page).to have_selector(:link_or_button, 'Click me')

Before:

page.should have_selector(:link_or_button, 'Click me')

Followed from click_link_or_button which is defined here: 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

It calls a selector :link_or_button. This selector is defined here: 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

It calls this 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

So i tried to check the presence of the selector and it worked:

page.should have_selector(:link_or_button, 'Click me')
Related Topic