Last active
July 27, 2024 16:50
-
-
Save infertux/5974047 to your computer and use it in GitHub Desktop.
Helper to select a Select2 item with Capybara/Cucumber
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
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I agree with the other posters. I'm using:
One problem is that if the list of options is fetched from a remote source, it won't be loaded immediately. In that scenario, if we use:
...that means that capybara won't wait for the results to exist, so that block will return nil.
If instead we use:
...then we're telling Capybara to wait for an element to exist which contains that text, and to fail if it doesn't exist.
This blog post was very helpful in understanding this: https://www.varvet.com/blog/why-wait_until-was-removed-from-capybara/