Created
October 14, 2013 04:11
-
-
Save onyxrev/6970632 to your computer and use it in GitHub Desktop.
Select2 search query and selection for Capybara
This file contains 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
# usage: | |
# select2_choose("#some_element_id_with_select2_attached", :query => "Santa", :choose => "Santa Monica, California") | |
# NOTE: just make sure your 'choose' is unique in your results set | |
module Select2Helper | |
def select2_choose(id, options) | |
page.execute_script %Q{ | |
i = $('#s2id_#{id} .select2-offscreen'); | |
i.trigger('keydown').val('#{options[:query]}').trigger('keyup'); | |
} | |
within(".select2-drop-active") do | |
find(".select2-result-label", :text => options[:choose]).click | |
end | |
end | |
end |
I got it working a bit differently. In my case there are multiple Select2 fields and all those had class "select2-drop-active", and the find was failing. Moreover, I wanted to have an approximate match, to make the specs a bit more easier.
Here is what I have come up with.
module Select2Helper
def select2_choose(id, options)
options[:query] ||= options[:choose]
page.execute_script %Q{
$('#s2id_#{id} .select2-offscreen')
.trigger('keydown')
.val('#{options[:query]}')
.trigger('keyup');
}
if options[:first]
find(".select2-highlighted .select2-match").click
else
find(".select2-result-label", text: options[:choose]).click
end
end
end
I' m also getting the same error as above when using either of the solutions.
Capybara::NotSupportedByDriverError:
Capybara::Driver::Base#execute_script
@MariaFamador, make sure you have a js enabled driver, for example phantomjs.
I'm getting "Circular dependency detected while autoloading constant Admin::Select2Helper (RuntimeError)" with either of the solutions.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! I'm getting the error:
This code have some gem dependency?