Module: Unobtainium::MultiWait::DriverModule

Defined in:
lib/unobtainium-multiwait/multiwait.rb

Overview

Driver module implementing multi wait functionality.

Constant Summary collapse

DEFAULT_OPTIONS =

Default options. This hash is also used to detect if any of the Hashes passed to #multiwait is an options Hash; it is considered one if it contains any of the keys specified here.

{
  # If true, raises on error instead of returning nil
  raise_on_error: false,
  # If true, returns the error object instead of nil
  return_errors: false,
  # If :all is specified, the method waits for all elements to
  #   appear or times out.
  # If :first is specified, the method returns when the first
  #   element appears.
  wait_for: :all,
  # Defaults to only finding :displayed? elements. You can use any method
  # that Selenium::WebDriver::Element responds to, or :exists? if you only
  # care whether the element exists.
  check_element: :displayed?,
  # The default wait timeout in seconds.
  timeout: 10,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#multiwait_optionsObject

Current options for multiwait



51
52
53
# File 'lib/unobtainium-multiwait/multiwait.rb', line 51

def multiwait_options
  @multiwait_options
end

Class Method Details

.matches?(impl) ⇒ Boolean

Returns true if the implementation has ‘#find_element`, false otherwise.

Returns:

  • (Boolean)


44
45
46
# File 'lib/unobtainium-multiwait/multiwait.rb', line 44

def matches?(impl)
  return impl.respond_to?(:find_element)
end

Instance Method Details

#multiwait(*args) ⇒ Object Also known as: wait

Wait for multiple elements. Each argument is a Hash of selector options that are passed to #find_element. If one argument contains keys from the DEFAULT_OPTIONS Hash, it is instead treated as an options Hash for the #multiwait method.

Returns:

  • Array of found elements or nil entries if no matching element was found. FIXME: recheck this!



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/unobtainium-multiwait/multiwait.rb', line 62

def multiwait(*args)
  # Parse options
  options, selectors = multiwait_parse_options(*args)

  # Pass some options to multifind
  multifind_opts = {
    raise_on_error: options[:raise_on_error],
    return_errors: options[:return_error],
    check_element: options[:check_element],
  }

  # Wait for elements
  results = []
  wait = ::Selenium::WebDriver::Wait.new(timeout: options[:timeout])
  begin
    wait.until do
      results = multifind(*selectors, multifind_opts)
      got_it, results = multiwait_filter_results(options, results)
      if got_it
        return results
      end
    end
  rescue ::Selenium::WebDriver::Error::TimeOutError => err
    if options[:raise_on_error]
      raise
    end
    if options[:return_errors]
      results.map! { |result| result.nil? ? err : result }
    end
  end

  return results
end