class Capybara::Select2::Element

Attributes

container[RW]
drop_container[RW]
multiselect[RW]
options[RW]
page[RW]
search_enabled[RW]

Public Class Methods

new(page_context, options = {}) click to toggle source
# File lib/capybara_select2.rb, line 8
def initialize(page_context, options = {})
  self.page = page_context
  self.options= options
  self.search_enabled = !!options[:search]
  self.multiselect = !!options[:multiple]
  find_container(options.slice(:from, :xpath))
end

Public Instance Methods

drop_container_class(value) click to toggle source
# File lib/capybara_select2.rb, line 16
def drop_container_class(value)
  if options.has_key? :search
    page.find(:xpath, "//body").find(".select2-search input.select2-search__field").set(value)
    page.execute_script(%|$("input.select2-search__field:visible").keyup();|)
    ".select2-results"
  elsif page.find(:xpath, "//body").has_selector?(".select2-dropdown")
    # select2 version 4.0
    ".select2-dropdown"
  else
    ".select2-drop"
  end
end
initiate!() click to toggle source
# File lib/capybara_select2.rb, line 40
def initiate!
  focus!
end
select_match!(value) click to toggle source
# File lib/capybara_select2.rb, line 29
def select_match!(value)
  [value].flatten.each do |value|
    if page.find(:xpath, "//body").has_selector?("#{drop_container_class(value)} li.select2-results__option")
      # select2 version 4.0
      page.find(:xpath, "//body").find("#{drop_container_class(value)} li.select2-results__option", text: value).click
    else
      page.find(:xpath, "//body").find("#{drop_container_class(value)} li.select2-result-selectable", text: value).click
    end
  end
end

Private Instance Methods

find_container(options) click to toggle source
# File lib/capybara_select2.rb, line 45
def find_container(options)
  if options.has_key? :xpath
    self.container = page.find(:xpath, options[:xpath])
  elsif options.has_key? :css
    self.container = page.find(:css, options[:css])
  else
    select_name = options[:from]
    self.container = page.find("label", text: select_name).find(:xpath, '..').find(".select2-container")
  end
end
focus!() click to toggle source
# File lib/capybara_select2.rb, line 56
def focus!
  if self.container.has_selector?(".select2-selection")
    # select2 version 4.0
    self.container.find(".select2-selection").click
  elsif select2_container.has_selector?(".select2-choice")
    self.container.find(".select2-choice").click
  else
    self.container.find(".select2-choices").click
  end
end