Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows set viewport for screenshot #406

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Added
- `Ferrum::Page#disable_javascript` disables the JavaScript from the HTML source
- `Ferrum::Page#set_viewport` emulates the viewport

### Changed

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,21 @@ browser.disable_javascript
```


#### set_viewport

Overrides device screen dimensions and emulates viewport.

* options `Hash`
* :width `Integer`, viewport width. `0` by default
* :height `Integer`, viewport height. `0` by default
* :scale_factor `Float`, device scale factor. `0` by default
* :mobile `Boolean`, whether to emulate mobile device. `false` by default

```ruby
browser.set_viewport(width: 1000, height: 600, scale_factor: 3)
```


## Frames

#### frames : `Array[Frame] | []`
Expand Down
2 changes: 1 addition & 1 deletion lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Browser
add_script_tag add_style_tag bypass_csp
on position position=
playback_rate playback_rate=
disable_javascript] => :page
disable_javascript set_viewport] => :page
delegate %i[default_user_agent] => :process

attr_reader :client, :process, :contexts, :options, :window_size, :base_url
Expand Down
30 changes: 25 additions & 5 deletions lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@ def close
@client.close
end

#
# Overrides device screen dimensions and emulates viewport according to parameters
#
# Read more [here](https://chromedevtools.github.io/devtools-protocol/tot/Emulation/#method-setDeviceMetricsOverride).
#
# @param [Integer] width width value in pixels. 0 disables the override
#
# @param [Integer] height height value in pixels. 0 disables the override
#
# @param [Float] scale_factor device scale factor value. 0 disables the override
#
# @param [Boolean] mobile whether to emulate mobile device
#
def set_viewport(width:, height:, scale_factor: 0, mobile: false)
command(
"Emulation.setDeviceMetricsOverride",
slowmoable: true,
width: width,
height: height,
deviceScaleFactor: scale_factor,
mobile: mobile
)
end

def resize(width: nil, height: nil, fullscreen: false)
if fullscreen
width, height = document_size
Expand All @@ -142,11 +166,7 @@ def resize(width: nil, height: nil, fullscreen: false)
set_window_bounds(width: width, height: height)
end

command("Emulation.setDeviceMetricsOverride", slowmoable: true,
width: width,
height: height,
deviceScaleFactor: 0,
mobile: false)
set_viewport(width: width, height: height)
end

#
Expand Down
9 changes: 9 additions & 0 deletions spec/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,13 @@
expect(page.main_frame.body).to eq("<html><head></head><body><p>text</p></body></html>")
end
end

describe "#set_viewport" do
it "overrides the viewport size" do
page.set_viewport(width: 500, height: 300, scale_factor: 2)

expect(page.viewport_size).to eq([500, 300])
expect(page.device_pixel_ratio).to eq(2)
end
end
end
11 changes: 0 additions & 11 deletions spec/support/views/initial_alert.erb

This file was deleted.