Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/window.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 Window
23
#
24
# @api private
25
#
26
27
def initialize(bridge)
28
@bridge = bridge
29
end
30
31
#
32
# Resize the current window to the given dimension.
33
#
34
# @param [Selenium::WebDriver::Dimension, #width and #height] dimension The new size.
35
#
36
37
def size=(dimension)
38
unless dimension.respond_to?(:width) && dimension.respond_to?(:height)
39
raise ArgumentError, "expected #{dimension.inspect}:#{dimension.class} " \
40
'to respond to #width and #height'
41
end
42
43
@bridge.resize_window dimension.width, dimension.height
44
end
45
46
#
47
# Get the size of the current window.
48
#
49
# @return [Selenium::WebDriver::Dimension] The size.
50
#
51
52
def size
53
@bridge.window_size
54
end
55
56
#
57
# Move the current window to the given position.
58
#
59
# @param [Selenium::WebDriver::Point, #x and #y] point The new position.
60
#
61
62
def position=(point)
63
unless point.respond_to?(:x) && point.respond_to?(:y)
64
raise ArgumentError, "expected #{point.inspect}:#{point.class} " \
65
'to respond to #x and #y'
66
end
67
68
@bridge.reposition_window point.x, point.y
69
end
70
71
#
72
# Get the position of the current window.
73
#
74
# @return [Selenium::WebDriver::Point] The position.
75
#
76
77
def position
78
@bridge.window_position
79
end
80
81
#
82
# Sets the current window rect to the given point and position.
83
#
84
# @param [Selenium::WebDriver::Rectangle, #x, #y, #width, #height] rectangle The new rect.
85
#
86
87
def rect=(rectangle)
88
unless %w[x y width height].all? { |val| rectangle.respond_to? val }
89
raise ArgumentError, "expected #{rectangle.inspect}:#{rectangle.class} " \
90
'to respond to #x, #y, #width, and #height'
91
end
92
93
@bridge.set_window_rect(x: rectangle.x,
94
y: rectangle.y,
95
width: rectangle.width,
96
height: rectangle.height)
97
end
98
99
#
100
# Get the rect of the current window.
101
#
102
# @return [Selenium::WebDriver::Rectangle] The rectangle.
103
#
104
105
def rect
106
@bridge.window_rect
107
end
108
109
#
110
# Equivalent to #size=, but accepts width and height arguments.
111
#
112
# @example Maximize the window.
113
#
114
# max_width, max_height = driver.execute_script("return [window.screen.availWidth, window.screen.availHeight];")
115
# driver.manage.window.resize_to(max_width, max_height)
116
#
117
118
def resize_to(width, height)
119
@bridge.resize_window Integer(width), Integer(height)
120
end
121
122
#
123
# Equivalent to #position=, but accepts x and y arguments.
124
#
125
# @example
126
#
127
# driver.manage.window.move_to(300, 400)
128
#
129
130
def move_to(x, y)
131
@bridge.reposition_window Integer(x), Integer(y)
132
end
133
134
#
135
# Maximize the current window
136
#
137
138
def maximize
139
@bridge.maximize_window
140
end
141
142
#
143
# Minimize the current window
144
#
145
146
def minimize
147
@bridge.minimize_window
148
end
149
150
#
151
# Make current window full screen
152
#
153
154
def full_screen
155
@bridge.full_screen_window
156
end
157
end # Window
158
end # WebDriver
159
end # Selenium
160
161