Capybara and Select2 Version 4

How can I fill / select a search box select2(gets results via ajax) using capybara.

Using the last version 4of select2and the last capybara/rspecin the rails project.

In SOand elsewhere, there are many examples of using Capybara with Select2 3.x, but not version 4, which is rewriting.

+4
source share
3 answers

In 4.0, they seem to have been simplified a bit. For one choice, you can simply enable this helper method:

def select2(value, **options)
  first("#select2-#{options[:from]}-container").click
  find(".select2-results__option", text: value).click
end

It works for me so far, but I have not tried with more customizable versions of the selector yet.

+4
source

( .)

select2 3.x.x. 4.

spec/support/feature/xyz.helper.rb, :

module Feature
  module XYZHelper
    def select2(value, element_selector)
      first("##{element_selector}").find(".select2-choice").click
      find(:xpath, "//body").find(".select2-results li", text: value).click
    end
  end
end

include Feature::XYZHelper. :

select2("Text value here", "Id of selector")
+3

, , ( ) select2 4 . , select2 . select2 div , .

def select2(values, id)
    values.each do |val|
        if page.has_no_css? ".select2-dropdown"
            within(id) do 
                find('span.select2').click 
            end
        end
        within ".select2-dropdown" do
            find('li', text: val).click
        end
    end
end

:

select2([value_to_select], "id of div containing desired select2")

, . select2 .

0
source

All Articles