Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/target_locator.rb
1865 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
class TargetLocator
23
#
24
# @api private
25
#
26
27
def initialize(bridge)
28
@bridge = bridge
29
end
30
31
#
32
# switch to the frame with the given id
33
#
34
35
def frame(id)
36
@bridge.switch_to_frame id
37
end
38
39
#
40
# switch to the parent frame
41
#
42
43
def parent_frame
44
@bridge.switch_to_parent_frame
45
end
46
47
#
48
# Switch to a new top-level browsing context
49
#
50
# @param type either :tab or :window
51
#
52
53
# steep:ignore:start
54
def new_window(type = :window)
55
raise ArgumentError, "Valid types are :tab and :window, received: #{type.inspect}" unless %i[window
56
tab].include?(type)
57
58
handle = @bridge.new_window(type)['handle']
59
60
if block_given?
61
execute_and_close = proc do
62
yield(self)
63
begin
64
@bridge.close
65
rescue Error::NoSuchWindowError
66
# window already closed
67
end
68
end
69
window(handle, &execute_and_close)
70
else
71
window(handle)
72
end
73
end
74
# steep:ignore:end
75
76
#
77
# switch to the given window handle
78
#
79
# If given a block, this method will switch back to the original window after
80
# block execution.
81
#
82
# @param id
83
# A window handle, obtained through Driver#window_handles
84
#
85
86
def window(id)
87
if block_given?
88
original = begin
89
@bridge.window_handle
90
rescue Error::NoSuchWindowError
91
nil
92
end
93
94
unless @bridge.window_handles.include? id
95
raise Error::NoSuchWindowError, "The specified identifier '#{id}' is not found in the window handle list"
96
end
97
98
@bridge.switch_to_window id
99
100
begin
101
yield
102
ensure
103
current_handles = @bridge.window_handles
104
original = current_handles.first unless current_handles.include? original
105
@bridge.switch_to_window original
106
end
107
else
108
@bridge.switch_to_window id
109
end
110
end
111
112
#
113
# get the active element
114
#
115
# @return [WebDriver::Element]
116
#
117
118
def active_element
119
@bridge.switch_to_active_element
120
end
121
122
#
123
# selects either the first frame on the page, or the main document when a page contains iframes.
124
#
125
126
def default_content
127
@bridge.switch_to_default_content
128
end
129
130
#
131
# switches to the currently active modal dialog for this particular driver instance
132
#
133
134
def alert
135
Alert.new(@bridge)
136
end
137
end # TargetLocator
138
end # WebDriver
139
end # Selenium
140
141