Path: blob/trunk/rb/lib/selenium/webdriver/common/wait.rb
4009 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@message_provider = opts[:message_provider]40@ignored = Array(opts[:ignore] || Error::NoSuchElementError)41end4243#44# Wait until the given block returns a true value.45#46# @raise [Error::TimeoutError]47# @return [Object] the result of the block48#4950def until51end_time = current_time + @timeout52last_error = nil5354until current_time > end_time55begin56result = yield57return result if result58rescue *@ignored => last_error # rubocop:disable Naming/RescuedExceptionsVariableName59# swallowed60end6162sleep @interval63end6465msg = if @message66@message.dup67elsif @message_provider68@message_provider.call69else70"timed out after #{@timeout} seconds"71end7273msg << " (#{last_error.message})" if last_error7475raise Error::TimeoutError, msg76end7778private7980def current_time81Process.clock_gettime(Process::CLOCK_MONOTONIC)82end83end # Wait84end # WebDriver85end # Selenium868788