Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara with selenium, send_key doesn't work

I'm using Cucumber to test a comment form that doesn't have a submit button. I found that selenium has a method called send_key, which in theory 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)

Not a clue what I'm doing wrong. Any ideas?

like image 874
Stephen Corwin Avatar asked Oct 22 '22 17:10

Stephen Corwin


1 Answers

You have to use the Selenium driver and not the :rack_test driver in Capybara to access the send_keys method:

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

You get an error because by default, Capybara uses the :rack_test driver. Calling native on an element access the driver specific methods. :rack_test driver elements are implemented natively as Nokogiri::XML::Element, therefore the send_keys methods do not exists and you get this error.

like image 117
Merwan Avatar answered Oct 30 '22 04:10

Merwan