Sha256: 473809b759ed81dbd2e770366d2c8c1d365ba756d010274b1a02b8040f72068f

Contents?: true

Size: 1.63 KB

Versions: 23

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Select2SpecHelper
  def select2(value, opts)
    scope = opts[:from]
    select2_container = first("#{scope} + .select2-container")
    select2_container.first(".select2-selection").click

    first("input.select2-search__field").set(value)
    page.execute_script(%|$("#{scope} ~ input.select2-search__field:visible").keyup();|)
    body = find(:xpath, "//body")
    matches = body.find_all(".select2-results li", text: value)
    matches.last.click
  end

  # select2_ajax helper to make capybara work with ajax-enabled Select2 elements
  # assumes 'placeholder' option is used in Select2 (if it is using ajax, it should be)
  #
  # usage:
  #
  #    it "should have a select2 field for searching by team name" do
  #        @team = Factory :team
  #        select2_ajax @team.name, from: "Select a Team", minlength: 4
  #        click_button "Join"
  #        page.should have_content "You are now on '#{@team.name}'."
  #    end
  # Thx to https://gist.github.com/sbeam/3849340
  def select2_ajax(value, options = {})
    if !options.is_a?(Hash) || !options.key?(:from)
      raise "Must pass a hash containing 'from'"
    end

    placeholder = options[:from]
    minlength = options[:minlength] || 4

    # click_link placeholder

    page.find(:xpath, "//*[text()='#{placeholder}']").click

    js = %Q|container = $('.select2-container:contains("#{placeholder}")');
            $('input[type=text]', container).val('#{value[0, minlength]}').trigger('keyup');
            window.setTimeout( function() {
              $('li:contains("#{value}")', container).click();
            }, 5000);|
    page.execute_script(js)
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
renalware-core-2.0.67 spec/support/select2_spec_helper.rb
renalware-core-2.0.64 spec/support/select2_spec_helper.rb
renalware-core-2.0.63 spec/support/select2_spec_helper.rb
renalware-core-2.0.62 spec/support/select2_spec_helper.rb
renalware-core-2.0.61 spec/support/select2_spec_helper.rb
renalware-core-2.0.60 spec/support/select2_spec_helper.rb
renalware-core-2.0.58 spec/support/select2_spec_helper.rb
renalware-core-2.0.57 spec/support/select2_spec_helper.rb
renalware-core-2.0.56 spec/support/select2_spec_helper.rb
renalware-core-2.0.55 spec/support/select2_spec_helper.rb
renalware-core-2.0.54 spec/support/select2_spec_helper.rb
renalware-core-2.0.53 spec/support/select2_spec_helper.rb
renalware-core-2.0.52 spec/support/select2_spec_helper.rb
renalware-core-2.0.51 spec/support/select2_spec_helper.rb
renalware-core-2.0.50 spec/support/select2_spec_helper.rb
renalware-core-2.0.48 spec/support/select2_spec_helper.rb
renalware-core-2.0.47 spec/support/select2_spec_helper.rb
renalware-core-2.0.46 spec/support/select2_spec_helper.rb
renalware-core-2.0.45 spec/support/select2_spec_helper.rb
renalware-core-2.0.44 spec/support/select2_spec_helper.rb