|
| 1 | +# rubocop:disable Lint/HandleExceptions |
| 2 | +module Appium |
| 3 | + module Core |
| 4 | + module Wait |
| 5 | + class TimeoutError < StandardError; end |
| 6 | + |
| 7 | + DEFAULT_TIMEOUT = 30 |
| 8 | + DEFAULT_INTERVAL = 0.5 |
| 9 | + |
| 10 | + class << self |
| 11 | + # Check every interval seconds to see if yield doesn't raise an exception. |
| 12 | + # Give up after timeout seconds. |
| 13 | + # |
| 14 | + # If only a number is provided then it's treated as the timeout value. |
| 15 | + # |
| 16 | + # @param [Integer] timeout: Seconds to wait before timing out. Set default by `appium_wait_timeout` (30). |
| 17 | + # @param [Integer] interval: Seconds to sleep between polls. Set default by `appium_wait_interval` (0.5). |
| 18 | + # @param [String] message: Exception message if timed out. |
| 19 | + # @param [Array, Exception] ignored: Exceptions to ignore while polling (default: Exception) |
| 20 | + # @param [Object, NilClass] object: Object to evaluate block against |
| 21 | + # |
| 22 | + # @example |
| 23 | + # |
| 24 | + # result = Appium::Core::Wait.until { @driver.find_element(:id, 'something') } |
| 25 | + # |
| 26 | + # result = Appium::Core::Wait.until(object: 'some object') { |object| |
| 27 | + # @driver.find_element(:id, object) |
| 28 | + # } |
| 29 | + # |
| 30 | + def until(timeout: DEFAULT_TIMEOUT, interval: DEFAULT_INTERVAL, message: nil, ignored: nil, object: nil) |
| 31 | + ignored = Array(ignored || ::Exception) |
| 32 | + |
| 33 | + end_time = Time.now + timeout |
| 34 | + last_error = nil |
| 35 | + |
| 36 | + until Time.now > end_time |
| 37 | + begin |
| 38 | + return yield(object) |
| 39 | + rescue ::Errno::ECONNREFUSED => e |
| 40 | + raise e |
| 41 | + rescue *ignored => last_error |
| 42 | + # swallowed |
| 43 | + end |
| 44 | + |
| 45 | + sleep interval |
| 46 | + end |
| 47 | + |
| 48 | + msg = message_for timeout, message |
| 49 | + msg << " (#{last_error.message})" if last_error |
| 50 | + |
| 51 | + raise TimeoutError, msg |
| 52 | + end |
| 53 | + |
| 54 | + # Check every interval seconds to see if yield returns a truthy value. |
| 55 | + # Note this isn't a strict boolean true, any truthy value is accepted. |
| 56 | + # false and nil are considered failures. |
| 57 | + # Give up after timeout seconds. |
| 58 | + # |
| 59 | + # If only a number is provided then it's treated as the timeout value. |
| 60 | + # |
| 61 | + # @param [Integer] timeout: Seconds to wait before timing out. Set default by `appium_wait_timeout` (30). |
| 62 | + # @param [Integer] interval: Seconds to sleep between polls. Set default by `appium_wait_interval` (0.5). |
| 63 | + # @param [String] message: Exception message if timed out. |
| 64 | + # @param [Array, Exception] ignored: Exceptions to ignore while polling (default: Exception) |
| 65 | + # @param [Object, NilClass] object: Object to evaluate block against |
| 66 | + # |
| 67 | + # @example |
| 68 | + # |
| 69 | + # Appium::Core::Wait.until_true { @driver.find_element(:id, 'something') } |
| 70 | + # |
| 71 | + # Appium::Core::Wait.until_true(object: 'some object') { |object| |
| 72 | + # @driver.find_element(:id, object) |
| 73 | + # } |
| 74 | + # |
| 75 | + def until_true(timeout: DEFAULT_TIMEOUT, interval: DEFAULT_INTERVAL, message: nil, ignored: nil, object: nil) |
| 76 | + ignored = Array(ignored || ::Exception) |
| 77 | + |
| 78 | + end_time = Time.now + timeout |
| 79 | + last_error = nil |
| 80 | + |
| 81 | + until Time.now > end_time |
| 82 | + begin |
| 83 | + result = yield(object) |
| 84 | + return result if result |
| 85 | + rescue ::Errno::ECONNREFUSED => e |
| 86 | + raise e |
| 87 | + rescue *ignored => last_error |
| 88 | + # swallowed |
| 89 | + end |
| 90 | + |
| 91 | + sleep interval |
| 92 | + end |
| 93 | + |
| 94 | + msg = message_for timeout, message |
| 95 | + msg << " (#{last_error.message})" if last_error |
| 96 | + |
| 97 | + raise TimeoutError, msg |
| 98 | + end |
| 99 | + |
| 100 | + private |
| 101 | + |
| 102 | + def message_for(timeout, message) |
| 103 | + msg = "timed out after #{timeout} seconds" |
| 104 | + msg << ", #{message}" if message |
| 105 | + msg |
| 106 | + end |
| 107 | + end # self |
| 108 | + end # module Wait |
| 109 | + |
| 110 | + module Waitable |
| 111 | + # Check every interval seconds to see if yield returns a truthy value. |
| 112 | + # Note this isn't a strict boolean true, any truthy value is accepted. |
| 113 | + # false and nil are considered failures. |
| 114 | + # Give up after timeout seconds. |
| 115 | + # |
| 116 | + # If only a number is provided then it's treated as the timeout value. |
| 117 | + # |
| 118 | + # @param [Integer] timeout: Seconds to wait before timing out. Set default by `appium_wait_timeout` (30). |
| 119 | + # @param [Integer] interval: Seconds to sleep between polls. Set default by `appium_wait_interval` (0.5). |
| 120 | + # @param [String] message: Exception message if timed out. |
| 121 | + # @param [Array, Exception] ignored: Exceptions to ignore while polling (default: Exception) |
| 122 | + # |
| 123 | + # @example |
| 124 | + # |
| 125 | + # @core.wait_true { @driver.find_element :accessibility_id, 'something' } |
| 126 | + # |
| 127 | + def wait_true(timeout: nil, interval: nil, message: nil, ignored: nil) |
| 128 | + Wait.until_true(timeout: timeout || @wait_timeout, |
| 129 | + interval: interval || @wait_interval, |
| 130 | + message: message, |
| 131 | + ignored: ignored, |
| 132 | + object: self) { yield } |
| 133 | + end |
| 134 | + |
| 135 | + # Check every interval seconds to see if yield doesn't raise an exception. |
| 136 | + # Give up after timeout seconds. |
| 137 | + # |
| 138 | + # If only a number is provided then it's treated as the timeout value. |
| 139 | + # |
| 140 | + # @param [Integer] timeout: Seconds to wait before timing out. Set default by `appium_wait_timeout` (30). |
| 141 | + # @param [Integer] interval: Seconds to sleep between polls. Set default by `appium_wait_interval` (0.5). |
| 142 | + # @param [String] message: Exception message if timed out. |
| 143 | + # @param [Array, Exception] ignored: Exceptions to ignore while polling (default: Exception) |
| 144 | + # |
| 145 | + # @example |
| 146 | + # |
| 147 | + # @core.wait { @driver.find_element :accessibility_id, 'something' } |
| 148 | + # |
| 149 | + def wait(timeout: nil, interval: nil, message: nil, ignored: nil) |
| 150 | + Wait.until(timeout: timeout || @wait_timeout, |
| 151 | + interval: interval || @wait_interval, |
| 152 | + message: message, |
| 153 | + ignored: ignored, |
| 154 | + object: self) { yield } |
| 155 | + end |
| 156 | + end |
| 157 | + end # module Core |
| 158 | +end # module Appium |
0 commit comments