How to fill in Capybara's hidden field?

I already found that when I want to set a value in a text box, text box or password box, I can use id, name or label as something in fill_in something, :with => some_value . However, this approach fails when I try to set the value in the <input type="hidden"> field (and I want to do this because these are usually populated client scripts that I test separately). How could I set such a hidden field with Capybara? Is it possible?

HTML:

 <input id='offer_latitude' name='offer[latitude]' type='hidden'> <input id='offer_longitude' name='offer[longitude]' type='hidden'> 

Specification:

 describe "posting new offer" do it "should add new offer" do visit '/offer/new' fill_in 'offer[latitude]', :with => '11.11' fill_in 'offer[longitude]', :with => '12.12' click_on 'add' end end 

gives:

 1) posting new offer should add new offer Failure/Error: fill_in 'offer[latitude]', :with => '11.11' Capybara::ElementNotFound: cannot fill in, no text field, text area or password field with id, name, or label 'offer[latitude]' found 
+48
rspec capybara
May 29 '12 at 18:47
source share
3 answers

You need to find the hidden field and set its value. There are several ways, this is probably the easiest

 find(:xpath, "//input[@id='my_hidden_field_id']").set "my value" 

If you execute client_side script during production, you can just tell capybara to run it using javascript-compatible driver

 page.execute_script("$('hidden_field_id').my_function()") 
+72
May 29 '12 at 19:05
source share

There are many ways to achieve the same result. The one I like best:

 first('input#id.class', visible: false).set("your value") 
+39
Feb 18 '15 at 15:51
source share

If you use poltergeist / phantomjs as a driver and jquery does not work for ya, there is always a good old-fashioned js:

 page.execute_script("document.getElementById('#some-id').value = 'some-value'"); 
+1
Feb 19 '14 at 21:38
source share



All Articles