Module: CapybaraSelect2::Helpers

Included in:
CapybaraSelect2
Defined in:
lib/capybara_select2/helpers.rb

Instance Method Summary collapse

Instance Method Details

#select2(*args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/capybara_select2/helpers.rb', line 6

def select2(*args)
  options = args.pop
  values = args

  Utils.validate_options!(options)

  container = if options[:xpath]
    find(:xpath, options[:xpath])
  elsif options[:css]
    find(:css, options[:css])
  else
    find("label:not(.select2-offscreen)", text: options[:from])
      .find(:xpath, '..')
      .find('.select2-container')
  end

  container = if container['class'] =~ /select2-container/
    container
  else
    container.find('.select2-container')
  end

  select2_version = Utils.detect_select2_version(container)

  open_select = {
    '2' => ".select2-choice, .select2-search-field",
    '3' => ".select2-choice, .select2-search-field",
    '4' => ".select2-selection"
  }.fetch(select2_version)

  search_input = {
    '2' => ".select2-dropdown-open input.select2-focused",
    '3' => ".select2-drop-active input.select2-input," +          # single
           ".select2-dropdown-open input.select2-input",          # multi
    '4' => ".select2-container--open input.select2-search__field"
  }.fetch(select2_version)

  option = {
    '2' => ".select2-container-active .select2-result",
    '3' => ".select2-drop-active .select2-result",
    '4' => ".select2-results .select2-results__option[role='treeitem']"
  }.fetch(select2_version)

  values.each do |value|
    container.find(open_select).click

    if options[:search] || options[:tag]
      find(:xpath, '//body').find(search_input).set value
    end

    find(:xpath, '//body').find(option, text: value).click
  end
end