Skip to content

Commit

Permalink
ref: Rename set_window_bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Jan 4, 2024
1 parent 06bd206 commit 5cb2512
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
instead of passing browser and making cyclic dependency on the browser instance, we pass now a thin client [#431]
- Bump `websocket-driver` to `~> 0.7` [#432]
- Got rid of `Concurrent::Async` in `Ferrum::Browser::Subscriber` [#432]
- `Ferrum::Page#set_window_bounds` is renamed to `Ferrum::Page#window_bounds=`

### Fixed

Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,37 @@ Get the position for the browser window
browser.position # => [10, 20]
```

#### window_bounds = \*\*options

Set window bounds

* options `Hash`
* :left `Integer`
* :top `Integer`
* :width `Integer`
* :height `Integer`
* :window_state `String`

```ruby
browser.window_bounds = { left: 10, top: 20, width: 1024, height: 768, window_state: "normal" }
```

#### window_bounds : `Hash<String, Integer | String>`

Get window bounds

```ruby
browser.window_bounds # => { "left": 0, "top": 1286, "width": 10, "height": 10, "windowState": "normal" }
```

#### window_id : `Integer`

Current window id

```ruby
browser.window_id # => 1
```

## Finders

#### at_css(selector, \*\*options) : `Node` | `nil`
Expand Down
76 changes: 59 additions & 17 deletions lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ def set_viewport(width:, height:, scale_factor: 0, mobile: false)
def resize(width: nil, height: nil, fullscreen: false)
if fullscreen
width, height = document_size
set_window_bounds(windowState: "fullscreen")
self.window_bounds = { window_state: "fullscreen" }
else
set_window_bounds(windowState: "normal")
set_window_bounds(width: width, height: height)
self.window_bounds = { window_state: "normal" }
self.window_bounds = { width: width, height: height }
end

set_viewport(width: width, height: height)
Expand All @@ -184,9 +184,7 @@ def disable_javascript
# page.position # => [10, 20]
#
def position
client(browser: true)
.command("Browser.getWindowBounds", windowId: window_id)
.fetch("bounds").values_at("left", "top")
window_bounds.values_at("left", "top")
end

#
Expand All @@ -204,9 +202,61 @@ def position
# page.position = { left: 10, top: 20 }
#
def position=(options)
client(browser: true).command("Browser.setWindowBounds",
windowId: window_id,
bounds: { left: options[:left], top: options[:top] })
self.window_bounds = { left: options[:left], top: options[:top] }
end

# Sets the position of the window.
#
# @param [Hash{Symbol => Object}] bounds
#
# @option options [Integer] :left
# The number of pixels from the left-hand side of the screen.
#
# @option options [Integer] :top
# The number of pixels from the top of the screen.
#
# @option options [Integer] :width
# The window width in pixels.
#
# @option options [Integer] :height
# The window height in pixels.
#
# @option options [String] :window_state
# The window state. Default to normal. Allowed Values: normal, minimized, maximized, fullscreen
#
# @example
# page.window_bounds = { left: 10, top: 20, width: 1024, height: 768, window_state: "normal" }
#
def window_bounds=(bounds)
options = bounds.dup
window_state = options.delete(:window_state)
bounds = { windowState: window_state, **options }.compact

client.command("Browser.setWindowBounds", windowId: window_id, bounds: bounds)
end

#
# Current window bounds.
#
# @return [Hash{String => (Integer, String)}]
#
# @example
# page.window_bounds # => { "left": 0, "top": 1286, "width": 10, "height": 10, "windowState": "normal" }
#
def window_bounds
client.command("Browser.getWindowBounds", windowId: window_id).fetch("bounds")
end

#
# Current window id.
#
# @return [Integer]
#
# @example
# page.window_id # => 1
#
def window_id
client.command("Browser.getWindowForTarget", targetId: target_id)["windowId"]
end

#
Expand Down Expand Up @@ -282,14 +332,6 @@ def bypass_csp(enabled: true)
enabled
end

def window_id
client(browser: true).command("Browser.getWindowForTarget", targetId: @target_id)["windowId"]
end

def set_window_bounds(bounds = {})
client(browser: true).command("Browser.setWindowBounds", windowId: window_id, bounds: bounds)
end

def command(method, wait: 0, slowmoable: false, **params)
iteration = @event.reset if wait.positive?
sleep(@options.slowmo) if slowmoable && @options.slowmo.positive?
Expand Down

0 comments on commit 5cb2512

Please sign in to comment.