Skip to content

Commit

Permalink
Fix select2 helper in select2 version 4.0.9+
Browse files Browse the repository at this point in the history
[fix #21][close #20][close #16]

Before this commit `select2` helper was failing with
```
Unable to find css ".select2-results .select2-results__option[role='treeitem']"
```
  • Loading branch information
Hirurg103 committed Dec 6, 2019
1 parent 5c9f161 commit 53a9bf4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/capybara_select2/selectors.rb
Expand Up @@ -27,7 +27,7 @@ def search_input_selector(select2_version)
OptionSelectors = {
'2' => ".select2-container-active .select2-result",
'3' => ".select2-drop-active .select2-result-label",
'4' => ".select2-results .select2-results__option[role='treeitem']"
'4' => ".select2-results .select2-results__option[aria-selected='false']"

This comment has been minimized.

Copy link
@Nerian

Nerian Dec 6, 2019

@Hirurg103 This change caused my tests to start failing because it no longer allows selecting an option that is already selected 😢 Do we really need to be so restrictive?

This comment has been minimized.

Copy link
@MatthewKennedy

MatthewKennedy Dec 6, 2019

@Nerian Are your tests set up to select an already selected option, with the intention of de-selecting that option?

This comment has been minimized.

Copy link
@Nerian

Nerian Dec 6, 2019

No, the selected option should be the selected one. My app usually have the selects inputs with a default value, and it is usually the correct value that the user would like to select.

This comment has been minimized.

Copy link
@Nerian

Nerian Dec 6, 2019

In practise, I am just checking that 1) there is a select, 2) it contains the right option.

This comment has been minimized.

Copy link
@MatthewKennedy

MatthewKennedy Dec 6, 2019

Hi @Nerian,

just to clarify, are you using the code below to check there is a Select2 box and it contains the correct value:
expect(page).to have_select2_option('Buy Milk')
And this is failing?

This comment has been minimized.

Copy link
@Nerian

Nerian Dec 6, 2019

I use this:

select2 @manager.name, css: '.membership_contact_id'

And I get

       Unable to find css ".select2-results .select2-results__option[aria-selected='false']" within #<Capybara::Node::Element tag="body" path="//HTML[1]/BODY[1]">

Because the manager is already selected, because the input cannot be blank, and the manager is the first option.

This comment has been minimized.

Copy link
@MatthewKennedy

MatthewKennedy Dec 6, 2019

I understand completely,

Out of interest could you swap the test line:
select2 @manager.name, css: '.membership_contact_id'

For this:
find('.membership_contact_id .select2-selection').click expect(page).to have_select2_option( @manager.name )
And let me know if it works?

This comment has been minimized.

Copy link
@Nerian

Nerian Dec 6, 2019

It didn't work.

find('.membership_contact_id .select2-selection') did find an item. the click did work too – I could visually see that the select was clicked on and the options appeared; I used Chrome driver.

The have_select2_option( @manager.name ) did fail.

This was the HTML after clicking on the select:

<span class="select2-results">
    <ul class="select2-results__options" role="tree" id="select2-membership_contact_id-results" aria-expanded="true" aria-hidden="false">
        <li class="select2-results__option select2-results__option--highlighted" id="select2-membership_contact_id-result-wwdt-7832" role="treeitem" aria-selected="true">Gonzalo Rodríguez</li>
    </ul>
</span>

This comment has been minimized.

Copy link
@MatthewKennedy

MatthewKennedy Dec 6, 2019

I'm just looking through the spec files for this gem as they were recently updated, trying to find you a better use for checking the page as you require, so that moving forward your tests are less likely to brake in future updates if they release Select2 v5.

I found they have changed to this in the examples:

open_select2('#console') 
expect(page).to have_select2_option('XBox')

Targeting this:

<div id="single">
      <label for="console">Select game console you would want to play</label>
      <select id="console" name="console">
        <option></option>
        <option value="xbox">XBox</option>
        <option value="playstation 3">PlayStation 3</option>
        <option value="playstation">PlayStation</option>
        <option value="wii">Wii</option>
      </select>
    </div>

Could this be adapted to work in your tests?

My thoughts being that there is a specific line for expect page to have, and its not failing in the tests for this gem, so you should be able to get it to work for your app.

This comment has been minimized.

Copy link
@Hirurg103

Hirurg103 Dec 6, 2019

Author Owner

Hi @Nerian , could you try to add select2_open helper like this:

def select2_open(selector)
  page.execute_script "$('#{selector}').select2('open')"
end

and then invoke it this way:

select2_open '.membership_contact_id'
expect(page).to have_select2_option('John Smith')

If it doesn't work please create an issue on this repo

This comment has been minimized.

Copy link
@Nerian

Nerian Dec 8, 2019

@MatthewKennedy

When I use open_select2 I get undefined method open_select2'`.

@Hirurg103

When I execute select2_open '#membership_contact_id' I can see the select box opening with the options.

<li class="select2-results__option select2-results__option--highlighted" id="select2-membership_contact_id-result-j8rm-8290" role="treeitem" aria-selected="true">Gonzalo Rodríguez</li>

But the assert fails:

expected #<Capybara::Session> to have select2 option "Gonzalo Rodríguez"

This comment has been minimized.

Copy link
@Hirurg103

Hirurg103 Dec 9, 2019

Author Owner

When I execute select2_open '#membership_contact_id' I can see the select box opening with the options

html<li class="select2-results__option select2-results__option--highlighted" id="select2-membership_contact_id-result-j8rm-8290" role="treeitem" aria-selected="true">Gonzalo Rodríguez</li>

But the assert fails:

expected #<Capybara::Session> to have select2 option "Gonzalo Rodríguez"

That's interesting. Possibly we have another bug there. Could you please add a new GH issue for it and describe your test env in detail (rspec version, capybara version, webdriver, assertion code, html)

}.freeze

def option_selector(select2_version)
Expand Down
7 changes: 6 additions & 1 deletion spec/versions_spec.rb
@@ -1,4 +1,9 @@
['2.1.0', '3.5.4', '4.0.5'].each do |select2_version|
[
'2.1.0',
'3.5.4',
'4.0.5',
'4.0.12'
].each do |select2_version|
Dir[File.join(__dir__, 'shared', '**', '*_spec.rb')].each do |spec|
eval <<-RUBY, binding, __FILE__, __LINE__ + 1
#{File.read(spec)}
Expand Down

0 comments on commit 53a9bf4

Please sign in to comment.