Module: Capybara::Select2

Defined in:
lib/capybara-select2.rb,
lib/capybara-select2/version.rb

Constant Summary collapse

VERSION =
"1.0.1"

Instance Method Summary collapse

Instance Method Details

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



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.rb', line 7

def select2(value, options = {})
  raise "Must pass a hash containing 'from' or 'xpath' or 'css'" unless options.is_a?(Hash) and [:from, :xpath, :css].any? { |k| options.has_key? k }

  if options.has_key? :xpath
    select2_container = first(:xpath, options[:xpath])
  elsif options.has_key? :css
    select2_container = first(:css, options[:css])
  else
    select_name = options[:from]
    select2_container = first("label", text: select_name).find(:xpath, '..').find(".select2-container")
  end

  # Open select2 field
  if select2_container.has_selector?(".select2-choice")
    select2_container.find(".select2-choice").click
  else
    select2_container.find(".select2-choices").click
  end

  if options.has_key? :search
    find(:xpath, "//body").find(".select2-with-searchbox input.select2-input").set(value)
    page.execute_script(%|$("input.select2-input:visible").keyup();|)
    drop_container = ".select2-results"
  else
    drop_container = ".select2-drop"
  end

  [value].flatten.each do |value|
    find(:xpath, "//body").find("#{drop_container} li.select2-result-selectable", text: value).click
  end
end