capybara-select2
capybara-select2 copied to clipboard
`Ambiguous match` when there are multiple select2 elements
#37 changed the code which finds the select2 input from using first
to using find
. Unfortunately Capybara chokes when it can't find a unique element with find
- which happens when you have more than one select2 element in the page:
Failure/Error: fill_event_fields_with_valid_data Capybara::Ambiguous: Ambiguous match, found 2 elements matching css ".select2-container"
I don't really understand enough about the issue that #37 fixes to know what the solution should be.
(cc @machisuji)
The code in "capybara-select2/gem/lib/capybara-select2.rb" has a section with "if options.has_key? :search" that uses only a find command to select the element, no use of first to resolve the ambiguous matches. Note that if i have "name" and "city name" i am in the case of an ambiguous match.
This can be fixed by having find(..., match: :first)
So would it be fixed by using a more specific selector when you have two select2s? I.e.
select2 'value', css: '#selectid'
I mean just picking the first one in an ambiguous match is not the right solution IMO. It's ambiguous for a reason.
@machisuji What you're saying is probably correct. The older first is just better represented as find(.., match: :first) if we want to keep the old behavior.
Hmm. I actually agree with @machisuji - it is an ambiguous match, so the current behaviour is correct.
I guess it's arguable whether this change is breaking or a bugfix ;)
I think if there's a 'fix' to be made at all then it would be to add a message into the error suggesting using the :css
or :match
options for a more specific match.
I'm having the same issue when I have the search: true
option enabled in combination with other select2 instances on the page (with either multiple: true
or not). When the code is looking for the search__field, it finds multiple items because it searches the search field on the whole page, instead of the drop container. I get the following error:
Capybara::Ambiguous:
Ambiguous match, found 3 elements matching css ".select2-search input.select2-search__field"
I think this could be fixed by seaching for the search__field in the select2-container:
select2_container = find(:css, '#select2_enabled_field_id + .select2-container')
select2_container.find(".select2-selection").click
find('body > .select2-container .select2-search input.select2-search__field').set('search_query')
page.execute_script(%|$("body > .select2-container input.select2-search__field:visible").keyup();|)
drop_container = "body > .select2-container .select2-results"
find(:xpath, "//body").find("#{drop_container} li.select2-results__option[role=treeitem]", text: 'search query').click
Note that I have incorporated the fix of @abuisman for properly selecting the result items when using optgroups: https://github.com/goodwill/capybara-select2/pull/47
I had the same issue when I had the search: true
enabled in conjunction with another select2 element. I got the same error as above:
Capybara::Ambiguous: Ambiguous match, found 2 elements matching css ".select2-search input.select2-search__field"
I got around this by monkey patching the gem and adding in the extra class .select2-container--open
to the find as so
find(:xpath, "//body").find(".select2-container--open .select2-search input.select2-search__field").set(value)
This worked a charm for me, and haven't investigated if this extra class will work for different versions of select2 but might help others