71

I have the following helper method to input a string into an input field and press the enter key, but it seems the enter key is never pressed. I see the string entered into the input field, but the events that take place upon hitting enter never happened.

I've tested in an actual browser that the enter key correctly fires the expected events. I'm not sure what I'm missing.

def fill_and_trigger_enter_keypress(selector, value)
  page.execute_script %Q(
                          var input = $('#{selector}');
                          input.val('#{value}');
                          input.trigger("keypress", [13]);
                         )
end

EDIT:

I've also tried the following to no avail:

find('#q_name').native.send_keys(:return)
find('#q_name').native.send_keys(:enter)

They don't cause any error, but still no enter key pressed.

Eric M.
  • 5,399
  • 6
  • 41
  • 67

7 Answers7

90
find('#q_name').native.send_keys(:return)

works for me. I dont have a name or id for my field but the type is input so i used something like

find('.myselector_name>input').native.send_keys(:return)

works perfectly fine!

Tisho
  • 8,320
  • 6
  • 44
  • 52
Mysterio Man
  • 1,667
  • 1
  • 14
  • 17
  • As an additional info, Selinium doesn't simulate key events on hidden elements, whereas this solution works fine even with hidden elements. – RajaRaviVarma Jun 25 '13 at 06:25
  • 4
    @Mysterio Man This solution isn't working with Capybara 2.5. It throws `NoMethodError: undefined method `send_keys' for "5":String` error. – Rahul Roy May 25 '16 at 06:41
36

These days (Capybara version 2.5+) you can simulate the <enter> key in the following way:

find('.selector').set("text\n")

The \n (new line) is the very important bit here.

Anna
  • 113
  • 1
  • 7
Paweł Gościcki
  • 9,066
  • 5
  • 70
  • 81
6

Usually when you run page.execute_script, you get the same results as if you were running that in the page console. Try running that manually in the console and see if you get the expected results. That is usually what I do.. craft the needed js code in the browser console window and paste it into the capybara code when it is working, using execute_script.

Felipe Lima
  • 10,530
  • 4
  • 41
  • 39
  • Working on that. I don't get any errors when I do $('#input_selector').trigger('keyPress', [13]), but still nothing seems triggered as if I were to hit enter in the browser. – Eric M. Jun 04 '12 at 18:16
  • 1
    yeah so you probably have to tweak the event you are triggering. try triggering a focus event first, to simulate that you clicked the input to enter it, and then the keyPress.. what I found is that you usually have to play with the right events – Felipe Lima Jun 05 '12 at 07:53
  • 1
    this is what I do to fill an autocomplete, for example: page.execute_script %Q{$('#{selector}').val('#{value}').focus().keydown()} – Felipe Lima Jun 05 '12 at 08:01
  • Sounds like code driven tests... instead of test driven code :) – lacostenycoder Feb 25 '20 at 17:23
5

Capybara doesn't have native support for a send_keys type event. You might be able to go down to selenium to do it, or you can try this gem https://github.com/markgandolfo/send-keys

DVG
  • 17,392
  • 7
  • 61
  • 88
  • Great, I'll check out that gem. But Capybara shouldn't be the issue since I'm sending the jQuery directly to selenium, right? – Eric M. Jun 03 '12 at 00:49
1

It works for me

page.execute_script("$('form.css-class/#form_id').submit()")
Mary Dear
  • 185
  • 2
  • 14
0

@Page.selector.send_keys :return

This works for me, where selector is the element in your page object element :selector, '<css selector>'

Igbanam
  • 5,904
  • 5
  • 44
  • 68
Murali K
  • 11
  • 3
0

Use fill_in as usual, but append a newline character "\n" to the with: value. The with: value must be in double quotes, not single quotes. This will trigger a form submission:

fill_in "q_name", with: "hello world\n"
Eliot Sykes
  • 9,616
  • 6
  • 50
  • 64