Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ambiguous match when there are multiple select2 elements #41

Open
dgmstuart opened this issue Nov 9, 2015 · 7 comments
Open

Ambiguous match when there are multiple select2 elements #41

dgmstuart opened this issue Nov 9, 2015 · 7 comments

Comments

@dgmstuart
Copy link

#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)

@dgmstuart dgmstuart changed the title Ambiguous match when there are multiple Ambiguous match when there are multiple select2 elements Nov 9, 2015
@thomasmetal
Copy link

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.

@fluke
Copy link

fluke commented May 10, 2016

This can be fixed by having find(..., match: :first)

@machisuji
Copy link
Contributor

machisuji commented May 10, 2016

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.

fluke added a commit to fluke/capybara-select2 that referenced this issue May 10, 2016
In goodwill#37 we changed the `first()` to `find()` but this leads to goodwill#41. This is a fix for the same.
@fluke
Copy link

fluke commented May 10, 2016

@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.

@dgmstuart
Copy link
Author

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.

@OtherCroissant
Copy link

OtherCroissant commented Aug 1, 2016

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: #47

@mrhillman
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants