Read more

Test whether a form field exists with Cucumber and Capybara

Henning Koch
February 03, 2012Software engineer at makandra GmbH

The step definition below lets you say:

 Then I should see a field "Password"
 But I should not see a field "Role"
Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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.

Posted by Henning Koch to makandra dev (2012-02-03 13:37)