Skip to content

Instantly share code, notes, and snippets.

@infertux
Last active July 27, 2024 16:50
Show Gist options
  • Save infertux/5974047 to your computer and use it in GitHub Desktop.
Save infertux/5974047 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
World(Select2Helper)
@Hirurg103
Copy link

Hirurg103 commented Sep 13, 2018

I wrapped the code which selects options from select2 into a gem capybara-select-2

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