Posted almost 6 years ago. Visible to the public.

Test whether a form field exists with Cucumber and Capybara

The step definition below lets you say:

Then I should see a field "Password" But I should not see a field "Role"

Here is the step definition:

Then /^I should( not)? see a field "([^"]*)"$/ do |negate, name| expectation = negate ? :should_not : :should begin field = find_field(name) rescue Capybara::ElementNotFound # In Capybara 0.4+ #find_field raises an error instead of returning nil end field.send(expectation, be_present) end

Note that you might have to adapt the step definition for older Capybaras. Unfortunately the Capybara API wasn't very stable over time.

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Author of this card:

Henning Koch
form, input
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandropedia