capybara: fill_in "field_id", with => "" does not trigger keyUp event

I am using jQuery to bind a text field to the keyUp event. A fill_in "field_id", with => "test" works as expected (keyUp is triggered), but clearing the field with fill_in "field_id", with => "" does not trigger that event.

About this issue

  • Original URL
  • State: closed
  • Created 14 years ago
  • Comments: 21 (5 by maintainers)

Most upvoted comments

@DougMaisells send_keys needs to be called on an element - you probably want something like

prefix = page.find_field('Prefix')
prefix.set ""
prefix.send_keys [:control, "a"]

of you could just do something like

find_field(‘Prefix’).send_keys([:control, “a”], :backspace)

which should have a similar effect (assuming ctrl-a selects all contents in the platform you’re testing on).

To work around this I use fill_in to clear the input field, and then manually trigger a keyup event on the field. I am using capybara via cucumber, so the below is the helper method I use for triggering events, and the cucumber step that I use when I need to clear a field:

def trigger_event_for(selector, event)
  raise "Please supply a selector" if selector.blank?
  if Capybara.javascript_driver == :selenium
    page.execute_script %Q{ $('#{selector}').trigger("#{event}") }
  end

  if Capybara.javascript_driver == :webkit
    page.find(selector).trigger(event.to_sym)
  end
end

Then 'delete the content of "$id"' do |id|
  fill_in id, with: ""
  trigger_event_for("##{id}", :keyup)
end