Class: Capybara::Select2::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara_select2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_context, options = {}) ⇒ Element

Returns a new instance of Element.



8
9
10
11
12
13
14
# 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

Instance Attribute Details

#containerObject

Returns the value of attribute container.



6
7
8
# File 'lib/capybara_select2.rb', line 6

def container
  @container
end

#drop_containerObject

Returns the value of attribute drop_container.



6
7
8
# File 'lib/capybara_select2.rb', line 6

def drop_container
  @drop_container
end

#multiselectObject

Returns the value of attribute multiselect.



6
7
8
# File 'lib/capybara_select2.rb', line 6

def multiselect
  @multiselect
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/capybara_select2.rb', line 6

def options
  @options
end

#pageObject

Returns the value of attribute page.



6
7
8
# File 'lib/capybara_select2.rb', line 6

def page
  @page
end

#search_enabledObject

Returns the value of attribute search_enabled.



6
7
8
# File 'lib/capybara_select2.rb', line 6

def search_enabled
  @search_enabled
end

Instance Method Details

#drop_container_class(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# 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!Object



40
41
42
# File 'lib/capybara_select2.rb', line 40

def initiate!
  focus!
end

#select_match!(value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# 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