Capybara with selenium, send_key not working

I use Cucumber to validate a comment form that does not have a submit button. I found that selenium has a send_key method, which theoretically should allow me to do this:

find_field('my-field').native.send_key(:enter) 

But when I run my test, I get:

 undefined method `send_key' for #<Nokogiri::XML::Element:0x007f874b361828> (NoMethodError) 

I don’t know what I am doing wrong. Any ideas?

+4
source share
2 answers

To access the send_keys method send_keys you need to use the Selenium driver, not the :rack_test driver in Capybara:

  • Install gem selenium-webdriver and add it to your gem file if you are using bundler.
  • Mark your test with :js => true so that it works with the Selenium driver.

You get an error because Capybara uses the driver :rack_test . Call native for an access element to specific driver methods. Driver elements :rack_test initially implemented as Nokogiri::XML::Element , so the send_keys methods send_keys not exist, and you get this error.

+2
source

Try using xpath

 within(:xpath, "//form[@id='the_form']") do locate(:xpath, "//input[@name='the_input']").set(value) locate(:xpath, "//input[@name='the_input']").node.send_keys(:return) end 
0
source

All Articles