Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara matcher for presence of button or link

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.

like image 932
mirelon Avatar asked Dec 13 '12 17:12

mirelon


2 Answers

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')
like image 186
mirelon Avatar answered Oct 24 '22 21:10

mirelon


Using the expect syntax

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

This works without needing to define a custom matcher.

like image 27
port5432 Avatar answered Oct 24 '22 23:10

port5432