Question: How to fill dynamic form fields using capybara

Question

How to fill dynamic form fields using capybara

Answers 2
Added at 2017-07-27 12:07
Tags
Question

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?

Answers to

How to fill dynamic form fields using capybara

nr: #1 dodano: 2017-07-28 07:07

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.

nr: #2 dodano: 2017-07-28 08:07

Finally got this to work using the following piece of code

page.execute_script("$('#have_water_bills').val('true').trigger('click')")
Source Show
◀ Wstecz