Skip to content

Commit 13918d5

Browse files
committed
[rb] implement w3c window rect and minimize commands
1 parent 3f8ea60 commit 13918d5

File tree

4 files changed

+90
-13
lines changed

4 files changed

+90
-13
lines changed

rb/lib/selenium/webdriver/common/window.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,34 @@ def position
8282
@bridge.window_position
8383
end
8484

85+
#
86+
# Sets the current window rect to the given point and position.
87+
#
88+
# @param [Selenium::WebDriver::Rectangle, #x, #y, #width, #height] rectangle The new rect.
89+
#
90+
91+
def rect=(rectangle)
92+
unless %w[x y width height].all? { |val| rectangle.respond_to? val }
93+
raise ArgumentError, "expected #{rectangle.inspect}:#{rectangle.class}" \
94+
' to respond to #x, #y, #width, and #height'
95+
end
96+
97+
@bridge.set_window_rect(x: rectangle.x,
98+
y: rectangle.y,
99+
width: rectangle.width,
100+
height: rectangle.height)
101+
end
102+
103+
#
104+
# Get the rect of the current window.
105+
#
106+
# @return [Selenium::WebDriver::Rectangle] The rectangle.
107+
#
108+
109+
def rect
110+
@bridge.window_rect
111+
end
112+
85113
#
86114
# Equivalent to #size=, but accepts width and height arguments.
87115
#
@@ -115,6 +143,14 @@ def maximize
115143
@bridge.maximize_window
116144
end
117145

146+
#
147+
# Minimize the current window
148+
#
149+
150+
def minimize
151+
@bridge.minimize_window
152+
end
153+
118154
#
119155
# Make current window full screen
120156
#

rb/lib/selenium/webdriver/firefox/w3c_bridge.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,32 @@ def quit
6060
@service.stop if @service
6161
end
6262

63+
# Firefox doesn't implement rect yet
64+
def resize_window(width, height, handle = :current)
65+
unless handle == :current
66+
raise Error::WebDriverError, 'Switch to desired window before changing its size'
67+
end
68+
execute :set_window_size, {}, {width: width, height: height}
69+
end
70+
71+
def window_size(handle = :current)
72+
unless handle == :current
73+
raise Error::UnsupportedOperationError, 'Switch to desired window before getting its size'
74+
end
75+
data = execute :get_window_size
76+
77+
Dimension.new data['width'], data['height']
78+
end
79+
80+
def reposition_window(x, y)
81+
execute :set_window_position, {}, {x: x, y: y}
82+
end
83+
84+
def window_position
85+
data = execute :get_window_position
86+
Point.new data['x'], data['y']
87+
end
88+
6389
private
6490

6591
def create_capabilities(opts)

rb/lib/selenium/webdriver/remote/w3c_bridge.rb

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,39 +234,51 @@ def resize_window(width, height, handle = :current)
234234
unless handle == :current
235235
raise Error::WebDriverError, 'Switch to desired window before changing its size'
236236
end
237-
execute :set_window_size, {}, {width: width,
238-
height: height}
237+
execute :set_window_rect, {}, {width: width, height: height}
239238
end
240239

241-
def maximize_window(handle = :current)
240+
def window_size(handle = :current)
242241
unless handle == :current
243-
raise Error::UnsupportedOperationError, 'Switch to desired window before changing its size'
242+
raise Error::UnsupportedOperationError, 'Switch to desired window before getting its size'
244243
end
245-
execute :maximize_window
244+
data = execute :get_window_rect
245+
246+
Dimension.new data['width'], data['height']
246247
end
247248

248-
def full_screen_window
249-
execute :fullscreen_window
249+
def minimize_window
250+
execute :minimize_window
250251
end
251252

252-
def window_size(handle = :current)
253+
def maximize_window(handle = :current)
253254
unless handle == :current
254-
raise Error::UnsupportedOperationError, 'Switch to desired window before getting its size'
255+
raise Error::UnsupportedOperationError, 'Switch to desired window before changing its size'
255256
end
256-
data = execute :get_window_size
257+
execute :maximize_window
258+
end
257259

258-
Dimension.new data['width'], data['height']
260+
def full_screen_window
261+
execute :fullscreen_window
259262
end
260263

261264
def reposition_window(x, y)
262-
execute :set_window_position, {}, {x: x, y: y}
265+
execute :set_window_rect, {}, {x: x, y: y}
263266
end
264267

265268
def window_position
266-
data = execute :get_window_position
269+
data = execute :get_window_rect
267270
Point.new data['x'], data['y']
268271
end
269272

273+
def set_window_rect(width: nil, height: nil, x: nil, y: nil)
274+
execute :set_window_rect, {}, {width: width, height: height, x: x, y: y}
275+
end
276+
277+
def window_rect
278+
data = execute :get_window_rect
279+
Rectangle.new data['x'], data['y'], data['width'], data['height']
280+
end
281+
270282
def screenshot
271283
execute :take_screenshot
272284
end

rb/lib/selenium/webdriver/remote/w3c_commands.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,14 @@ class W3CBridge
5454
switch_to_window: [:post, 'session/:session_id/window'.freeze],
5555
get_window_handles: [:get, 'session/:session_id/window/handles'.freeze],
5656
fullscreen_window: [:post, 'session/:session_id/window/fullscreen'.freeze],
57+
minimize_window: [:post, 'session/:session_id/window/minimize'.freeze],
5758
maximize_window: [:post, 'session/:session_id/window/maximize'.freeze],
5859
set_window_size: [:post, 'session/:session_id/window/size'.freeze],
5960
get_window_size: [:get, 'session/:session_id/window/size'.freeze],
6061
set_window_position: [:post, 'session/:session_id/window/position'.freeze],
6162
get_window_position: [:get, 'session/:session_id/window/position'.freeze],
63+
set_window_rect: [:post, 'session/:session_id/window/rect'.freeze],
64+
get_window_rect: [:get, 'session/:session_id/window/rect'.freeze],
6265
switch_to_frame: [:post, 'session/:session_id/frame'.freeze],
6366
switch_to_parent_frame: [:post, 'session/:session_id/frame/parent'.freeze],
6467

0 commit comments

Comments
 (0)