Module: Appium::Core::Wait

Defined in:
lib/appium_lib_core/common/wait.rb,
lib/appium_lib_core/common/wait/timer.rb

Defined Under Namespace

Classes: TimeoutError, Timer

Constant Summary collapse

DEFAULT_TIMEOUT =
30
DEFAULT_INTERVAL =
0.5

Class Method Summary collapse

Class Method Details

.until(timeout: DEFAULT_TIMEOUT, interval: DEFAULT_INTERVAL, message: nil, ignored: nil, object: nil) ⇒ Object

Check every interval seconds to see if yield doesn’t raise an exception. Give up after timeout seconds.

If only a number is provided then it’s treated as the timeout value.

Examples:


result = Appium::Core::Wait.until { @driver.find_element(:id, 'something') }

result = Appium::Core::Wait.until(timeout: 30, message: 'timeout') { @driver.find_element(:id, 'something') }

result = Appium::Core::Wait.until(object: 'some object') { |object|
   @driver.find_element(:id, object)
}

Parameters:

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    Seconds to wait before timing out. Set default by appium_wait_timeout (30).

  • interval (Integer) (defaults to: DEFAULT_INTERVAL)

    Seconds to sleep between polls. Set default by appium_wait_interval (0.5).

  • message (String) (defaults to: nil)

    Exception message if timed out.

  • ignored (Array, Exception) (defaults to: nil)

    Exceptions to ignore while polling (default: Exception)

  • object (Object, NilClass) (defaults to: nil)

    Object to evaluate block against

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/appium_lib_core/common/wait.rb', line 47

def until(timeout: DEFAULT_TIMEOUT, interval: DEFAULT_INTERVAL, message: nil, ignored: nil, object: nil)
  ignored = Array(ignored || ::Exception)

  last_error = nil
  timer = Wait::Timer.new(timeout)

  until timer.timeout?
    begin
      return yield(object)
    rescue ::Errno::ECONNREFUSED => e
      raise e
    rescue *ignored => last_error
      # swallowed
    end
    sleep interval
  end

  msg = message_for timeout, message
  msg += " (#{last_error.message})" if last_error

  raise TimeoutError, msg
end

.until_true(timeout: DEFAULT_TIMEOUT, interval: DEFAULT_INTERVAL, message: nil, ignored: nil, object: nil) ⇒ Object

Check every interval seconds to see if yield returns a truthy value. Note this isn’t a strict boolean true, any truthy value is accepted. false and nil are considered failures. Give up after timeout seconds.

If only a number is provided then it’s treated as the timeout value.

Examples:


Appium::Core::Wait.until_true { @driver.find_element(:id, 'something') }

Appium::Core::Wait.until_true(timeout: 30) { @driver.find_element(:id, 'something') }

Appium::Core::Wait.until_true(object: 'some object') { |object|
   @driver.find_element(:id, object)
}

Parameters:

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    Seconds to wait before timing out. Set default by appium_wait_timeout (30).

  • interval (Integer) (defaults to: DEFAULT_INTERVAL)

    Seconds to sleep between polls. Set default by appium_wait_interval (0.5).

  • message (String) (defaults to: nil)

    Exception message if timed out.

  • ignored (Array, Exception) (defaults to: nil)

    Exceptions to ignore while polling (default: Exception)

  • object (Object, NilClass) (defaults to: nil)

    Object to evaluate block against

Raises:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/appium_lib_core/common/wait.rb', line 93

def until_true(timeout: DEFAULT_TIMEOUT, interval: DEFAULT_INTERVAL, message: nil, ignored: nil, object: nil)
  ignored = Array(ignored || ::Exception)

  last_error = nil
  timer = Wait::Timer.new(timeout)

  until timer.timeout?
    begin
      result = yield(object)
      return result if result
    rescue ::Errno::ECONNREFUSED => e
      raise e
    rescue *ignored => last_error
      # swallowed
    end
    sleep interval
  end

  msg = message_for timeout, message
  msg += " (#{last_error.message})" if last_error

  raise TimeoutError, msg
end