Skip to content

Instantly share code, notes, and snippets.

@onyxrev
Created October 14, 2013 04:11
Show Gist options
  • Save onyxrev/6970632 to your computer and use it in GitHub Desktop.
Save onyxrev/6970632 to your computer and use it in GitHub Desktop.
Select2 search query and selection for Capybara
# 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
@MariaFamador
Copy link

I' m also getting the same error as above when using either of the solutions.

Capybara::NotSupportedByDriverError:
Capybara::Driver::Base#execute_script

@Valve
Copy link

Valve commented May 15, 2014

@MariaFamador, make sure you have a js enabled driver, for example phantomjs.

@senott
Copy link

senott commented May 13, 2015

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