Kamilski81
Kamilski81

Reputation: 15117

How do I populate a hidden input field in Capybara?

I have the following code on fron-end:

%input#nonce{ type: "hidden", name: "#{param_prefix}[payment_method_nonce]" }

Andy my Capybara code:

hidden_field = find_field("input#nonce", type: :hidden)
hidden_field.set "fake-apple-pay-visa-nonce"

And I am getting this error:

Capybara::ElementNotFound: Unable to find field "input#nonce" that is not disabled of type :hidden

How can I set the value on an input hidden field in Capybara?

Upvotes: 0

Views: 340

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

TL;DR; You don't.

Capybara emulates a user, users can't fill in a hidden field so neither does Capybara. Attempting to do so generally indicates you're doing something wrong in your testing, and potentially means the test you're executing is pointless (since it wouldn't replicate what a user was doing)

If you still feel you like you really really need to fill in a hidden field you would need to do it via JS using execute_script

Upvotes: 4

Related Questions