How do I write a Capybara assertion that checks for the presence of a button and its enabled or disabled state?

It appears that Capybara’s handling of disabled elements has changed, so I figured I’d give an update.

To test whether a page has a button and is enabled, use:

expect(page).to have_button('Save')

To test whether a page has a button and is disabled, use:

expect(page).to have_button('Save', disabled: true)

This format works with has_field?, find_field, etc.

You can read more on this update at http://www.elabs.se/tag/capybara

UPDATE

The old link is broken. This is the GitHub issue where the feature was discussed and this is the unfortunately barren documentation for the method.

Leave a Comment