Module: Softwear::Library::Spec::Select2

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

Instance Method Summary collapse

Instance Method Details

#select2(text, options) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/softwear/library/spec.rb', line 86

def select2(text, options)
  label = find_label_by_text(options[:from], try: true)
  if label
    selector = "select[name='#{label['for']}']+span,select[id='#{label['for']}']+span"
  else
    selector = "#{options[:from]}+span"
  end

  if options[:last]
    all(selector).last.click
  else
    find(selector).click
  end

  old_scopes = page.instance_variable_get(:@scopes)
  page.instance_variable_set(:@scopes, [nil])

  find('input.select2-search__field').set(text)
  sleep options[:wait_before_click] if options[:wait_before_click]
  result = first('li.select2-results__option')
  if result.nil? || result.text == "No results found"
    raise %(No results matching "#{text}" found during select2)
  else
    result.click
  end

ensure
  page.instance_variable_set(:@scopes, old_scopes) if old_scopes
end