Last active
December 30, 2015 15:48
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated using Module#prepend to address deprecation of alias_method_chain