Skip to content

Commit 5cb2512

Browse files
committed
ref: Rename set_window_bounds
1 parent 06bd206 commit 5cb2512

File tree

3 files changed

+91
-17
lines changed

3 files changed

+91
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
instead of passing browser and making cyclic dependency on the browser instance, we pass now a thin client [#431]
1717
- Bump `websocket-driver` to `~> 0.7` [#432]
1818
- Got rid of `Concurrent::Async` in `Ferrum::Browser::Subscriber` [#432]
19+
- `Ferrum::Page#set_window_bounds` is renamed to `Ferrum::Page#window_bounds=`
1920

2021
### Fixed
2122

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,37 @@ Get the position for the browser window
268268
browser.position # => [10, 20]
269269
```
270270

271+
#### window_bounds = \*\*options
272+
273+
Set window bounds
274+
275+
* options `Hash`
276+
* :left `Integer`
277+
* :top `Integer`
278+
* :width `Integer`
279+
* :height `Integer`
280+
* :window_state `String`
281+
282+
```ruby
283+
browser.window_bounds = { left: 10, top: 20, width: 1024, height: 768, window_state: "normal" }
284+
```
285+
286+
#### window_bounds : `Hash<String, Integer | String>`
287+
288+
Get window bounds
289+
290+
```ruby
291+
browser.window_bounds # => { "left": 0, "top": 1286, "width": 10, "height": 10, "windowState": "normal" }
292+
```
293+
294+
#### window_id : `Integer`
295+
296+
Current window id
297+
298+
```ruby
299+
browser.window_id # => 1
300+
```
301+
271302
## Finders
272303

273304
#### at_css(selector, \*\*options) : `Node` | `nil`

lib/ferrum/page.rb

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ def set_viewport(width:, height:, scale_factor: 0, mobile: false)
156156
def resize(width: nil, height: nil, fullscreen: false)
157157
if fullscreen
158158
width, height = document_size
159-
set_window_bounds(windowState: "fullscreen")
159+
self.window_bounds = { window_state: "fullscreen" }
160160
else
161-
set_window_bounds(windowState: "normal")
162-
set_window_bounds(width: width, height: height)
161+
self.window_bounds = { window_state: "normal" }
162+
self.window_bounds = { width: width, height: height }
163163
end
164164

165165
set_viewport(width: width, height: height)
@@ -184,9 +184,7 @@ def disable_javascript
184184
# page.position # => [10, 20]
185185
#
186186
def position
187-
client(browser: true)
188-
.command("Browser.getWindowBounds", windowId: window_id)
189-
.fetch("bounds").values_at("left", "top")
187+
window_bounds.values_at("left", "top")
190188
end
191189

192190
#
@@ -204,9 +202,61 @@ def position
204202
# page.position = { left: 10, top: 20 }
205203
#
206204
def position=(options)
207-
client(browser: true).command("Browser.setWindowBounds",
208-
windowId: window_id,
209-
bounds: { left: options[:left], top: options[:top] })
205+
self.window_bounds = { left: options[:left], top: options[:top] }
206+
end
207+
208+
# Sets the position of the window.
209+
#
210+
# @param [Hash{Symbol => Object}] bounds
211+
#
212+
# @option options [Integer] :left
213+
# The number of pixels from the left-hand side of the screen.
214+
#
215+
# @option options [Integer] :top
216+
# The number of pixels from the top of the screen.
217+
#
218+
# @option options [Integer] :width
219+
# The window width in pixels.
220+
#
221+
# @option options [Integer] :height
222+
# The window height in pixels.
223+
#
224+
# @option options [String] :window_state
225+
# The window state. Default to normal. Allowed Values: normal, minimized, maximized, fullscreen
226+
#
227+
# @example
228+
# page.window_bounds = { left: 10, top: 20, width: 1024, height: 768, window_state: "normal" }
229+
#
230+
def window_bounds=(bounds)
231+
options = bounds.dup
232+
window_state = options.delete(:window_state)
233+
bounds = { windowState: window_state, **options }.compact
234+
235+
client.command("Browser.setWindowBounds", windowId: window_id, bounds: bounds)
236+
end
237+
238+
#
239+
# Current window bounds.
240+
#
241+
# @return [Hash{String => (Integer, String)}]
242+
#
243+
# @example
244+
# page.window_bounds # => { "left": 0, "top": 1286, "width": 10, "height": 10, "windowState": "normal" }
245+
#
246+
def window_bounds
247+
client.command("Browser.getWindowBounds", windowId: window_id).fetch("bounds")
248+
end
249+
250+
#
251+
# Current window id.
252+
#
253+
# @return [Integer]
254+
#
255+
# @example
256+
# page.window_id # => 1
257+
#
258+
def window_id
259+
client.command("Browser.getWindowForTarget", targetId: target_id)["windowId"]
210260
end
211261

212262
#
@@ -282,14 +332,6 @@ def bypass_csp(enabled: true)
282332
enabled
283333
end
284334

285-
def window_id
286-
client(browser: true).command("Browser.getWindowForTarget", targetId: @target_id)["windowId"]
287-
end
288-
289-
def set_window_bounds(bounds = {})
290-
client(browser: true).command("Browser.setWindowBounds", windowId: window_id, bounds: bounds)
291-
end
292-
293335
def command(method, wait: 0, slowmoable: false, **params)
294336
iteration = @event.reset if wait.positive?
295337
sleep(@options.slowmo) if slowmoable && @options.slowmo.positive?

0 commit comments

Comments
 (0)