module Softwear::Library::Spec::Select2

Public Instance Methods

select2(text, options) click to toggle source
# 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[tabindex="0"]').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