lib/watir/wait.rb in watir-6.13.0 vs lib/watir/wait.rb in watir-6.14.0

- old
+ new

@@ -1,17 +1,14 @@ require 'watir/wait/timer' module Watir module Wait + class TimeoutError < StandardError; end - class TimeoutError < StandardError ; end - INTERVAL = 0.1 - class << self - # # @!attribute timer # Access Watir timer implementation in use. # @see Timer # @return [#wait] @@ -33,15 +30,15 @@ # @param [String] message Message to raise if timeout is exceeded # @param [Object, NilClass] object Object to evaluate block against # @raise [TimeoutError] if timeout is exceeded # - def until(deprecated_timeout = nil, deprecated_message = nil, timeout: nil, message: nil, interval: nil, object: nil) - if deprecated_message || deprecated_timeout - Watir.logger.deprecate "Using arguments for Wait#until", "keywords", ids: [:until, :timeout_arguments] - timeout = deprecated_timeout - message = deprecated_message + def until(depr_timeout = nil, depr_message = nil, timeout: nil, message: nil, interval: nil, object: nil) + if depr_message || depr_timeout + Watir.logger.deprecate 'Using arguments for Wait#until', 'keywords', ids: %i[until timeout_arguments] + timeout = depr_timeout + message = depr_message end timeout ||= Watir.default_timeout run_with_timer(timeout, interval) do result = yield(object) return result if result @@ -59,15 +56,15 @@ # @param [String] message Message to raise if timeout is exceeded # @param [Object, NilClass] object Object to evaluate block against # @raise [TimeoutError] if timeout is exceeded # - def while(deprecated_timeout = nil, deprecated_message = nil, timeout: nil, message: nil, interval: nil, object: nil) - if deprecated_message || deprecated_timeout - Watir.logger.deprecate "Using arguments for Wait#while", "keywords", ids: [:while, :timeout_arguments] - timeout = deprecated_timeout - message = deprecated_message + def while(depr_timeout = nil, depr_message = nil, timeout: nil, message: nil, interval: nil, object: nil) + if depr_message || depr_timeout + Watir.logger.deprecate 'Using arguments for Wait#while', 'keywords', ids: %i[while timeout_arguments] + timeout = depr_timeout + message = depr_message end timeout ||= Watir.default_timeout run_with_timer(timeout, interval) { return unless yield(object) } raise TimeoutError, message_for(timeout, object, message) end @@ -80,27 +77,24 @@ err << ", #{message}" if message err end - def run_with_timer(timeout, interval, &block) + def run_with_timer(timeout, interval) if timeout.zero? - block.call + yield else timer.wait(timeout) do - block.call + yield sleep interval || INTERVAL end end end - end # self end # Wait - module Waitable - # # Waits until the condition is true. # # @example # browser.wait_until(timeout: 2) do |browser| @@ -115,19 +109,20 @@ # # @param [Integer] timeout seconds to wait before timing out # @param [String] message error message for when times out # - def wait_until(deprecated_timeout = nil, deprecated_message = nil, timeout: nil, message: nil, interval: nil, **opt, &blk) - if deprecated_message || deprecated_timeout - Watir.logger.deprecate "Using arguments for #wait_until", "keywords", ids: [:timeout_arguments] - timeout = deprecated_timeout - message = deprecated_message + def wait_until(depr_timeout = nil, depr_message = nil, timeout: nil, message: nil, interval: nil, **opt, &blk) + if depr_message || depr_timeout + Watir.logger.deprecate 'Using arguments for #wait_until', 'keywords', ids: [:timeout_arguments] + timeout = depr_timeout + message = depr_message end - message ||= Proc.new { |obj| "waiting for true condition on #{obj.inspect}" } + message ||= proc { |obj| "waiting for true condition on #{obj.inspect}" } raise ArgumentError, "Unknown keyword(s): #{opt.keys} " if block_given? && !opt.empty? + proc = block_given? ? blk : create_proc(opt) Wait.until(timeout: timeout, message: message, interval: interval, object: self, &proc) self @@ -146,19 +141,20 @@ # # @param [Integer] timeout seconds to wait before timing out # @param [String] message error message for when times out # - def wait_while(deprecated_timeout = nil, deprecated_message = nil, timeout: nil, message: nil, interval: nil, **opt, &blk) - if deprecated_message || deprecated_timeout - Watir.logger.deprecate "Using arguments for #wait_while", "keywords", ids: [:timeout_arguments] - timeout = deprecated_timeout - message = deprecated_message + def wait_while(depr_timeout = nil, depr_message = nil, timeout: nil, message: nil, interval: nil, **opt, &blk) + if depr_message || depr_timeout + Watir.logger.deprecate 'Using arguments for #wait_while', 'keywords', ids: [:timeout_arguments] + timeout = depr_timeout + message = depr_message end - message ||= Proc.new { |obj| "waiting for false condition on #{obj.inspect}" } + message ||= proc { |obj| "waiting for false condition on #{obj.inspect}" } raise ArgumentError, "Unknown keyword(s): #{opt.keys} " if block_given? && !opt.empty? + proc = block_given? ? blk : create_proc(opt) Wait.while(timeout: timeout, message: message, interval: interval, object: self, &proc) self @@ -170,30 +166,33 @@ # # @example # browser.text_field(name: "new_user_first_name").wait_until_present # # @param [Integer] timeout seconds to wait before timing out + # @param [Float] interval seconds to wait before each try + # @param [String] message error message for when times out # # @see Watir::Wait # @see Watir::Element#present? # - def wait_until_present(deprecated_timeout = nil, timeout: nil, interval: nil) - if deprecated_timeout - Watir.logger.deprecate "Using arguments for #wait_until_present", "keywords", ids: [:timeout_arguments] - timeout = deprecated_timeout + def wait_until_present(depr_timeout = nil, timeout: nil, interval: nil, message: nil) + if depr_timeout + Watir.logger.deprecate 'Using arguments for #wait_until_present', 'keywords', ids: [:timeout_arguments] + timeout = depr_timeout end - if self.is_a? Watir::Element - wait_until(timeout: timeout, interval: interval) do - self.reset! if self.is_a? Watir::Element - self.present? + if is_a? Watir::Element + message ||= proc { |obj| "waiting for element #{obj.inspect} to become present" } + wait_until(timeout: timeout, interval: interval, message: message) do + reset! if is_a? Watir::Element + present? end else Watir.logger.deprecate "#{self.class}#wait_until_present", "#{self.class}#wait_until(&:present?)", ids: [:wait_until_present] - wait_until(timeout: timeout, interval: interval, &:present?) + wait_until(timeout: timeout, interval: interval, message: message, &:present?) end end # # Waits while the element is present. @@ -201,51 +200,53 @@ # # @example # browser.text_field(name: "abrakadbra").wait_while_present # # @param [Integer] timeout seconds to wait before timing out + # @param [Float] interval seconds to wait before each try + # @param [String] message error message for when times out # # @see Watir::Wait # @see Watir::Element#present? # - def wait_while_present(deprecated_timeout = nil, timeout: nil, interval: nil) - if deprecated_timeout - Watir.logger.deprecate "Using arguments for #wait_while_present", "keywords", ids: [:timeout_arguments] - timeout = deprecated_timeout + def wait_while_present(depr_timeout = nil, timeout: nil, interval: nil, message: nil) + if depr_timeout + Watir.logger.deprecate 'Using arguments for #wait_while_present', 'keywords', ids: [:timeout_arguments] + timeout = depr_timeout end - if self.is_a? Watir::Element - wait_while(timeout: timeout, interval: interval) do - self.reset! if self.is_a? Watir::Element - self.present? + if is_a? Watir::Element + message ||= proc { |obj| "waiting for element #{obj.inspect} not to be present" } + wait_while(timeout: timeout, interval: interval, message: message) do + reset! if is_a? Watir::Element + present? end else Watir.logger.deprecate "#{self.class}#wait_while_present", "#{self.class}#wait_while(&:present?)", ids: [:wait_while_present] - wait_while(timeout: timeout, interval: interval, &:present?) + wait_while(timeout: timeout, interval: interval, message: message, &:present?) end end private def create_proc(opt) - Proc.new do + proc do opt.keys.all? do |key| expected = opt[key] - actual = if self.is_a?(Watir::Element) && !self.respond_to?(key) - self.attribute_value(key) + actual = if is_a?(Watir::Element) && !respond_to?(key) + attribute_value(key) else - self.send(key) + send(key) end case expected when Regexp expected =~ actual else expected.to_s == actual end end end end - end # Waitable end # Watir