Class: Appium::Core::Base::Wait

Inherits:
Selenium::WebDriver::Wait
  • Object
show all
Defined in:
lib/appium_lib_core/common/base/wait.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Wait

Returns a new instance of Wait.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/appium_lib_core/common/base/wait.rb', line 8

def initialize(opts = {})
  valid_keys = i(timeout interval message ignore return_if_true)
  invalid_keys = []
  opts.each_key { |key| invalid_keys << key unless valid_keys.include?(key) }
  # [:one, :two] => :one, :two
  unless invalid_keys.empty?
    raise "Invalid keys #{invalid_keys.to_s[1..-2]}. Valid keys are #{valid_keys.to_s[1..-2]}"
  end

  @timeout        = opts.fetch(:timeout, DEFAULT_TIMEOUT)
  @interval       = opts.fetch(:interval, DEFAULT_INTERVAL)
  @message        = opts[:message]
  @ignored        = Array(opts[:ignore] || ::Exception)
  @return_if_true = opts[:return_if_true]

  super(timeout: @timeout, interval: @interval, message: @message, ignore: @ignored)
end

Instance Method Details

#untilObject

Raises:

  • (Selenium::WebDriver::Error::TimeOutError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/appium_lib_core/common/base/wait.rb', line 28

def until
  end_time   = Time.now + @timeout
  last_error = nil

  until Time.now > end_time
    begin
      return yield unless @return_if_true

      result = yield
      return result if result
    rescue ::Errno::ECONNREFUSED => e
      raise e
    rescue *@ignored => last_error
      # swallowed
    end

    sleep @interval
  end

  msg = @message ? @message.dup : "timed out after #{@timeout} seconds"

  msg << " (#{last_error.message})" if last_error

  raise Selenium::WebDriver::Error::TimeOutError, msg
end