Class: WebDriverUtils::Wait
- Inherits:
-
Object
- Object
- WebDriverUtils::Wait
- Defined in:
- lib/webdriver_utils/wait.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
5- DEFAULT_INTERVAL =
0.2
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Wait
constructor
Create a new Wait instance.
-
#until(&blk) ⇒ Object
Wait until the given block returns a true value.
Constructor Details
#initialize(opts = {}) ⇒ Wait
Create a new Wait instance
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/webdriver_utils/wait.rb', line 36 def initialize(opts = {}) # Validate all options to prevent typos such as 'ignored' # instead of 'ignore' # code from my appium gem # https://github.com/appium/ruby_lib/blob/252e83806318df7df28e9060d3f8e1e56dc732ba/lib/appium_lib/common/wait.rb#L10 valid_keys = [:timeout, :interval, :message, :ignore] invalid_keys = [] opts.keys.each { |key| invalid_keys << key unless valid_keys.include?(key) } # [:one, :two] => :one, :two fail "Invalid keys #{invalid_keys.to_s[1..-2]}. Valid keys are #{valid_keys.to_s[1..-2]}" unless invalid_keys.empty? @timeout = opts.fetch(:timeout, DEFAULT_TIMEOUT) @interval = opts.fetch(:interval, DEFAULT_INTERVAL) @message = opts[:message] @ignored = Array(opts[:ignore] || ::Selenium::WebDriver::Error::NoSuchElementError) end |
Instance Method Details
#until(&blk) ⇒ Object
Wait until the given block returns a true value.
61 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 |
# File 'lib/webdriver_utils/wait.rb', line 61 def until(&blk) end_time = Time.now + @timeout last_error = nil until Time.now > end_time begin result = yield return result if result rescue *@ignored => last_error # swallowed end sleep @interval end if @message msg = @message.dup else msg = "timed out after #{@timeout} seconds" end msg << " (#{last_error.})" if last_error raise ::Selenium::WebDriver::Error::TimeOutError, msg end |