Module: CapybaraSelect2::Helpers

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

Instance Method Summary collapse

Instance Method Details

#select2(value, options = {}) ⇒ Object



3
4
5
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
# File 'lib/capybara_select2/helpers.rb', line 3

def select2(value, options = {})
  version = Config.select2_version.to_s

  open_select = {
    '2' => ".select2-choice, .select2-search-field",
    '3' => ".select2-choice, .select2-search-field",
    '4' => ".select2-selection"
  }[version]

  search_input = {
    '2' => ".select2-dropdown-open input.select2-focused",
    '3' => ".select2-dropdown-open input.select2-input",
    '4' => (
      ".select2-container--focus input.select2-search__field," + # single
      ".select2-container--open input.select2-search__field")    # multi
  }[version]

  option = {
    '2' => ".select2-container-active .select2-result",
    '3' => ".select2-drop-active .select2-result",
    '4' => ".select2-results .select2-results__option"
  }[version]

  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.find(open_select).click

  if options[:search] || options[:tag]
    find(search_input).set value
  end

  find(option, text: value).click
end