O'Reilly logo

Application Testing with Capybara by Matthew Robbins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Submitting forms

Another common task you are likely to want to automate is the completion and submission of forms.

Again Capybara provides a lot of user-friendly API to do just this. Consider this simple form snippet:

<form id="myform">
    <input type="text" name="Forename" value="" />
    <input type="text" name="Surname" value="" />
    <input type="submit" value="Go" />
</form>

The following Cucumber step definition would fill in and submit the form:

When(/^I complete and submit the form$/) do
  fill_in 'Forename', :with => 'Matthew'
  fill_in 'Surname', :with => 'Robbins'
  click_on 'Go'
end

When locating fields that can accept text input, Capybara will use one of the following to find those fields in the DOM:

  • The id attribute of the input element
  • The name attribute ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required