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

def select2(value, options = {})
  open_select = "" +
    ".select2-choice, .select2-search-field," +                # v2 & v3
    ".select2-selection"                                       # v4

  search_input = "" +
    ".select2-dropdown-open input.select2-focused," +          # v2
    ".select2-drop-active input.select2-input," +              # v3
    ".select2-container--focus input.select2-search__field," + # v4 single
    ".select2-container--open input.select2-search__field"     # v4 multi

  option = "" +
    ".select2-container-active .select2-result," +             # v2
    ".select2-drop-active .select2-result," +                  # v3
    ".select2-results .select2-results__option"                # v4


  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