Skip to content

Instantly share code, notes, and snippets.

@owenr
Last active December 30, 2015 15:48
Show Gist options
  • Save owenr/8903946 to your computer and use it in GitHub Desktop.
Save owenr/8903946 to your computer and use it in GitHub Desktop.
Extend Capybara's #set and #fill_in methods to support select boxes. Save to Rails directory config/initializers/
require 'capybara'
module Capybara
module Node
module ElementExt
def set(value, fill_options={})
if self.tag_name == 'select'
opt = find(:option, value)
if opt.blank?
warn "Could not find option '#{value}' in '#{self.inspect}"
else
opt.select_option
end
else
super(value, fill_options)
end
end
end
module ActionsExt
def fill_in(name, options={})
begin
super(name, with: options[:with])
rescue Capybara::ElementNotFound
find_field(name).set(options[:with])
end
end
end
Element.prepend ElementExt
Base.prepend ActionsExt
end
end
@owenr
Copy link
Author

owenr commented Dec 28, 2015

Updated using Module#prepend to address deprecation of alias_method_chain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment