Skip to content

Instantly share code, notes, and snippets.

@Joseph507
Forked from infertux/select2_helper.rb
Last active November 7, 2016 02:33
Show Gist options
  • Save Joseph507/8896ac1421a35f89923c908f66a64eb3 to your computer and use it in GitHub Desktop.
Save Joseph507/8896ac1421a35f89923c908f66a64eb3 to your computer and use it in GitHub Desktop.
Helper to select a Select2 item with Capybara/Cucumber
# spec/support/capybara/select2_helper.rb or features/support/select2_helper.rb
module Select2Helper
# @example
# select2 "Item", from: "select_id"
# select2 /^Item/, from: "select_id"
#
# @note Works with Select2 version 3.4.1.
def select2(text, options)
find("#s2id_#{options[:from]}").click
all(".select2-result-label").find do |result|
result.text =~ Regexp.new(text)
end.click
end
end
module Select2Helper
def select2(value, attrs)
find("#select2-#{attrs[:from]}-container").click
find(".select2-search__field").set(value)
within ".select2-results" do
find("li", text: value).click
end
end
end
World(Select2Helper)
World(Select2Helper)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment