You probably have more than a few Capybara 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, FactoryBot.attributes_for(:user).slice(:name, :age, :city))
Literate Example
# The main entry point for Formulaic is the `fill_form` method.
fill_form(
# Symbol representing the name of the class the form represents
:dependent,
# Pass a hash of attributes to be filled. Works great with
# `FactoryBot.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!