Search code examples
ruby-on-rails-4rspeccapybarapoltergeist

How to fill dynamic form fields using capybara


I have a form that contains a select box and a text field.

The text field is displayed dynamically based on the selectbox selection. If the value of selectbox is "Yes", then the text field will be displayed and vice versa. I am running an rspec test and filled the select box value with "Yes"

select 'Yes', from: 'property[have_water_bills]'

Now i want to fill a value on the text field

fill_in 'property[irrigation_cycle_count]', with: 5

But i am getting the following error.

Capybara::ElementNotFound:
   Unable to find field "property[irrigation_cycle_count]"

That is, capybara cannot find the dynamic element. Does anyone know how to fix this?


Solution

  • Finally got this to work using the following piece of code

    page.execute_script("$('#have_water_bills').val('true').trigger('click')")