Form Filling is Formulaic

This post originally published on thoughtbot's Giant Robots blog.

You probably have more than a few tests that look a bit like this:

fill_in ...
fill_in ...
fill_in ...
fill_in ...
select ...
choose ...
click_on 'Submit'

Filling out a form with Capybara can be very tedious. With Formulaic, we aim to make the process less repetitive and more fun.

fill_form(:user, name: 'Caleb', age: 24, city: 'Boston')
fill_form(:user, FactoryGirl.attributes_for(:user).slice(:name, :age, :city))

Literate Example

# The main entry point for Formulaic is the `fill_form` method.
  # Symbol representing the name of the class the form represents

  # Pass a hash of attributes to be filled. Works great with
  # `FactoryGirl.attributes_for(:dependent)`.
    # The attribute to set and the value. In this case, Formulaic will
    # `fill_in` the "Name" input with "My dependent".
    name: 'My dependent',

    # If the value of an attribute is a hash, Formulaic will look up
    # translations for the correct model. This is
    # `t('simple_form.labels.profile.zip_code')`.
    profile: { zip_code: '12345' },

    # Works with dates, too!
    date_of_birth: 8.years.ago,

    # When passed an array, it will `check` each of the elements.
    ethnicity: [Profile::ETHNICITY_OPTIONS.first],

# Formulaic provides a simple way to look up the translation for the
# submit helper for your model and action. The default is `:create`, so
# you can leave that off.
click_on submit(:dependent, :create)

Formulaic uses I18n conventions to find the text of labels and assumes that you are using SimpleForm.

We hope that you enjoy using Formulaic as much as we do, and as always we encourage you to report any problems you might have and to contribute your improvements!