Path: blob/trunk/rb/lib/selenium/webdriver/common/target_locator.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 TargetLocator22#23# @api private24#2526def initialize(bridge)27@bridge = bridge28end2930#31# switch to the frame with the given id32#3334def frame(id)35@bridge.switch_to_frame id36end3738#39# switch to the parent frame40#4142def parent_frame43@bridge.switch_to_parent_frame44end4546#47# Switch to a new top-level browsing context48#49# @param type either :tab or :window50#5152# steep:ignore:start53def new_window(type = :window)54raise ArgumentError, "Valid types are :tab and :window, received: #{type.inspect}" unless %i[window55tab].include?(type)5657handle = @bridge.new_window(type)['handle']5859if block_given?60execute_and_close = proc do61yield(self)62begin63@bridge.close64rescue Error::NoSuchWindowError65# window already closed66end67end68window(handle, &execute_and_close)69else70window(handle)71end72end73# steep:ignore:end7475#76# switch to the given window handle77#78# If given a block, this method will switch back to the original window after79# block execution.80#81# @param id82# A window handle, obtained through Driver#window_handles83#8485def window(id)86if block_given?87original = begin88@bridge.window_handle89rescue Error::NoSuchWindowError90nil91end9293unless @bridge.window_handles.include? id94raise Error::NoSuchWindowError, "The specified identifier '#{id}' is not found in the window handle list"95end9697@bridge.switch_to_window id9899begin100yield101ensure102current_handles = @bridge.window_handles103original = current_handles.first unless current_handles.include? original104@bridge.switch_to_window original105end106else107@bridge.switch_to_window id108end109end110111#112# get the active element113#114# @return [WebDriver::Element]115#116117def active_element118@bridge.switch_to_active_element119end120121#122# selects either the first frame on the page, or the main document when a page contains iframes.123#124125def default_content126@bridge.switch_to_default_content127end128129#130# switches to the currently active modal dialog for this particular driver instance131#132133def alert134Alert.new(@bridge)135end136end # TargetLocator137end # WebDriver138end # Selenium139140141