Skip to content

Commit b46d456

Browse files
authored
fix: Chrome doesn't change resolution (#399)
* fix: Chrome doesn't change resolution * fix: take device_pixel_ratio into account * chore: remove outdated argument
1 parent da18ec2 commit b46d456

File tree

6 files changed

+19
-14
lines changed

6 files changed

+19
-14
lines changed

.rspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
--color
2-
--fail-fast
32
--format=doc
43
--require spec_helper

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## [Unreleased](https://github.com/rubycdp/ferrum/compare/v0.13...main) ##
22

33
### Added
4+
- `Ferrum::Page#device_pixel_ratio` returns the ratio of the resolution in physical pixels to the
5+
resolution in CSS pixels for the current display device.
46
- `Ferrum::Network#cache(disable: true | false)` whether or not to use cache for every request
57
- `Ferrum::Network::Exchange#redirect?` determines if the exchange is a redirect
68
- `Ferrum::Network::Exchange#xhr?` determines if the exchange is XHR

lib/ferrum/browser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Browser
2222
body doctype content=
2323
headers cookies network
2424
mouse keyboard
25-
screenshot pdf mhtml viewport_size
25+
screenshot pdf mhtml viewport_size device_pixel_ratio
2626
frames frame_by main_frame
2727
evaluate evaluate_on evaluate_async execute evaluate_func
2828
add_script_tag add_style_tag bypass_csp

lib/ferrum/page.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ def resize(width: nil, height: nil, fullscreen: false)
146146
width: width,
147147
height: height,
148148
deviceScaleFactor: 0,
149-
mobile: false,
150-
fitWindow: false)
149+
mobile: false)
151150
end
152151

153152
#

lib/ferrum/page/screenshot.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ def viewport_size
147147
JS
148148
end
149149

150+
def device_pixel_ratio
151+
evaluate <<~JS
152+
window.devicePixelRatio
153+
JS
154+
end
155+
150156
def document_size
151157
evaluate <<~JS
152158
[document.documentElement.scrollWidth,

spec/page/screenshot_spec.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
create_screenshot(path: file)
1414

1515
File.open(file, "rb") do |f|
16-
expect(ImageSize.new(f.read).size).to eq(browser.viewport_size)
16+
expect(ImageSize.new(f.read).size).to eq(browser.viewport_size.map { |s| s * device_pixel_ratio })
1717
end
1818

1919
create_screenshot(path: file, full: true)
2020

2121
File.open(file, "rb") do |f|
22-
expect(ImageSize.new(f.read).size).to eq(
23-
browser.evaluate("[document.documentElement.clientWidth, document.documentElement.clientHeight]")
24-
)
22+
size = browser.evaluate("[document.documentElement.clientWidth, document.documentElement.clientHeight]")
23+
expect(ImageSize.new(f.read).size).to eq(size.map { |s| s * device_pixel_ratio })
2524
end
2625
end
2726

@@ -31,7 +30,7 @@
3130
create_screenshot(path: file, full: true)
3231

3332
File.open(file, "rb") do |f|
34-
expect(ImageSize.new(f.read).size).to eq(browser.viewport_size)
33+
expect(ImageSize.new(f.read).size).to eq(browser.viewport_size.map { |s| s * device_pixel_ratio })
3534
end
3635
end
3736

@@ -48,7 +47,7 @@
4847
return [rect.width, rect.height];
4948
}();
5049
JS
51-
expect(ImageSize.new(f.read).size).to eq(size)
50+
expect(ImageSize.new(f.read).size).to eq(size.map { |s| s * device_pixel_ratio })
5251
end
5352
end
5453

@@ -59,9 +58,8 @@
5958
create_screenshot(path: file, full: true, selector: "#penultimate")
6059

6160
File.open(file, "rb") do |f|
62-
expect(ImageSize.new(f.read).size).to eq(
63-
browser.evaluate("[document.documentElement.clientWidth, document.documentElement.clientHeight]")
64-
)
61+
size = browser.evaluate("[document.documentElement.clientWidth, document.documentElement.clientHeight]")
62+
expect(ImageSize.new(f.read).size).to eq(size.map { |s| s * device_pixel_ratio })
6563
end
6664
end
6765

@@ -99,6 +97,7 @@
9997
describe "#screenshot" do
10098
let(:format) { :png }
10199
let(:file) { "#{PROJECT_ROOT}/spec/tmp/screenshot.#{format}" }
100+
let(:device_pixel_ratio) { browser.device_pixel_ratio }
102101

103102
def create_screenshot(**options)
104103
browser.screenshot(**options)
@@ -165,7 +164,7 @@ def create_screenshot(**options)
165164
browser.screenshot(path: file, full: true)
166165

167166
File.open(file, "rb") do |f|
168-
expect(ImageSize.new(f.read).size).to eq([1280, 1024])
167+
expect(ImageSize.new(f.read).size).to eq([1280, 1024].map { |s| s * device_pixel_ratio })
169168
end
170169
expect(browser.viewport_size).to eq([1024, 768])
171170
end

0 commit comments

Comments
 (0)