Path: blob/trunk/rb/lib/selenium/webdriver/common/wait.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21class Wait22DEFAULT_TIMEOUT = 523DEFAULT_INTERVAL = 0.22425#26# Create a new Wait instance27#28# @param [Hash] opts Options for this instance29# @option opts [Numeric] :timeout (5) Seconds to wait before timing out.30# @option opts [Numeric] :interval (0.2) Seconds to sleep between polls.31# @option opts [String] :message Exception message if timed out.32# @option opts [Array, Exception] :ignore Exceptions to ignore while polling (default: Error::NoSuchElementError)33#3435def initialize(opts = {})36@timeout = opts.fetch(:timeout, DEFAULT_TIMEOUT)37@interval = opts.fetch(:interval, DEFAULT_INTERVAL)38@message = opts[:message]39@ignored = Array(opts[:ignore] || Error::NoSuchElementError)40end4142#43# Wait until the given block returns a true value.44#45# @raise [Error::TimeoutError]46# @return [Object] the result of the block47#4849def until50end_time = current_time + @timeout51last_error = nil5253until current_time > end_time54begin55result = yield56return result if result57rescue *@ignored => last_error # rubocop:disable Naming/RescuedExceptionsVariableName58# swallowed59end6061sleep @interval62end6364msg = if @message65@message.dup66else67"timed out after #{@timeout} seconds"68end6970msg << " (#{last_error.message})" if last_error7172raise Error::TimeoutError, msg73end7475private7677def current_time78Process.clock_gettime(Process::CLOCK_MONOTONIC)79end80end # Wait81end # WebDriver82end # Selenium838485