Module: Softwear::Lib::Spec::Select2

Defined in:
lib/softwear/lib/spec.rb

Instance Method Summary collapse

Instance Method Details

#select2(value, options) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/softwear/lib/spec.rb', line 68

def select2(value, options)
  label = find_label_by_text(options[:from])

  within label.first(:xpath,'.//..') do
    options[:from] = "##{find('.select2-container')['id']}"
  end
  targetted_select2(value, options)
end

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



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/softwear/lib/spec.rb', line 77

def select2_no_label value, options={}
  raise "Must pass a hash containing 'from'" if not options.is_a?(Hash) or not options.has_key?(:from)

  placeholder = options[:from]
  # TODO: still need this?
  # minlength = options[:minlength] || 4

  click_link placeholder

  select_select2_result(value)
end

#select2_search(value, options) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/softwear/lib/spec.rb', line 54

def select2_search(value, options)
  label = find_label_by_text(options[:from])
  within label.first(:xpath,'.//..') do
    options[:from] = "##{find('.select2-container')['id']}"
  end
  targetted_select2_search(value, options)
end

#select_select2_result(value) ⇒ Object



95
96
97
98
99
100
# File 'lib/softwear/lib/spec.rb', line 95

def select_select2_result(value)
  # results are in a div appended to the end of the document
  within(:xpath, '//body') do
    page.find('div.select2-result-label', text: %r{#{Regexp.escape(value)}}i).click
  end
end

#set_select2_field(field, value) ⇒ Object



50
51
52
# File 'lib/softwear/lib/spec.rb', line 50

def set_select2_field(field, value)
  page.execute_script %Q{$('#{field}').select2('val', '#{value}')}
end

#targetted_select2(value, options) ⇒ Object



89
90
91
92
93
# File 'lib/softwear/lib/spec.rb', line 89

def targetted_select2(value, options)
  # find select2 element and click it
  find(options[:from]).find('a').click
  select_select2_result(value)
end

#targetted_select2_search(value, options) ⇒ Object



62
63
64
65
66
# File 'lib/softwear/lib/spec.rb', line 62

def targetted_select2_search(value, options)
  page.execute_script %Q{$('#{options[:from]}').select2('open')}
  page.execute_script "$('#{options[:dropdown_css]} input.select2-input').val('#{value}').trigger('keyup-change');"
  select_select2_result(value)
end