prajeesh
prajeesh

Reputation: 2382

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?

Upvotes: 0

Views: 609

Answers (2)

prajeesh
prajeesh

Reputation: 2382

Finally got this to work using the following piece of code

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

Upvotes: 0

Thomas Walpole
Thomas Walpole

Reputation: 49890

Poltergeist doesn't gemerate a click event when choosing an item from a select. It generates a focus on the option, change on the select, blur on the option. It is more like if a user selected the option with keyboard instead of using a mouse. You probably should be doing the logic to display your text field on the change event anyway so that it works if people use a mouse or a keyboard to navigate around your page. It also makes more sense to run your show/hide logic on the change event because that's what you actually care about, not clicks.

Upvotes: 1

Related Questions