Skip to content

Commit

Permalink
fix: take device_pixel_ratio into account
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Sep 14, 2023
1 parent 23ae70e commit a1baac9
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
1 change: 0 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
--color
--fail-fast
--format=doc
--require spec_helper
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## [Unreleased](https://github.com/rubycdp/ferrum/compare/v0.13...main) ##

### Added
- `Ferrum::Page#device_pixel_ratio` returns the ratio of the resolution in physical pixels to the
resolution in CSS pixels for the current display device.
- `Ferrum::Network#cache(disable: true | false)` whether or not to use cache for every request
- `Ferrum::Network::Exchange#redirect?` determines if the exchange is a redirect
- `Ferrum::Network::Exchange#xhr?` determines if the exchange is XHR
Expand Down
2 changes: 1 addition & 1 deletion lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Browser
body doctype content=
headers cookies network
mouse keyboard
screenshot pdf mhtml viewport_size
screenshot pdf mhtml viewport_size device_pixel_ratio
frames frame_by main_frame
evaluate evaluate_on evaluate_async execute evaluate_func
add_script_tag add_style_tag bypass_csp
Expand Down
2 changes: 1 addition & 1 deletion lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def resize(width: nil, height: nil, fullscreen: false)
command("Emulation.setDeviceMetricsOverride", slowmoable: true,
width: width,
height: height,
deviceScaleFactor: 1,
deviceScaleFactor: 0,
mobile: false,
fitWindow: false)
end
Expand Down
6 changes: 6 additions & 0 deletions lib/ferrum/page/screenshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ def viewport_size
JS
end

def device_pixel_ratio
evaluate <<~JS
window.devicePixelRatio
JS
end

def document_size
evaluate <<~JS
[document.documentElement.scrollWidth,
Expand Down
19 changes: 9 additions & 10 deletions spec/page/screenshot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
create_screenshot(path: file)

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

create_screenshot(path: file, full: true)

File.open(file, "rb") do |f|
expect(ImageSize.new(f.read).size).to eq(
browser.evaluate("[document.documentElement.clientWidth, document.documentElement.clientHeight]")
)
size = browser.evaluate("[document.documentElement.clientWidth, document.documentElement.clientHeight]")
expect(ImageSize.new(f.read).size).to eq(size.map { |s| s * device_pixel_ratio })
end
end

Expand All @@ -31,7 +30,7 @@
create_screenshot(path: file, full: true)

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

Expand All @@ -48,7 +47,7 @@
return [rect.width, rect.height];
}();
JS
expect(ImageSize.new(f.read).size).to eq(size)
expect(ImageSize.new(f.read).size).to eq(size.map { |s| s * device_pixel_ratio })
end
end

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

File.open(file, "rb") do |f|
expect(ImageSize.new(f.read).size).to eq(
browser.evaluate("[document.documentElement.clientWidth, document.documentElement.clientHeight]")
)
size = browser.evaluate("[document.documentElement.clientWidth, document.documentElement.clientHeight]")
expect(ImageSize.new(f.read).size).to eq(size.map { |s| s * device_pixel_ratio })
end
end

Expand Down Expand Up @@ -99,6 +97,7 @@
describe "#screenshot" do
let(:format) { :png }
let(:file) { "#{PROJECT_ROOT}/spec/tmp/screenshot.#{format}" }
let(:device_pixel_ratio) { browser.device_pixel_ratio }

def create_screenshot(**options)
browser.screenshot(**options)
Expand Down Expand Up @@ -165,7 +164,7 @@ def create_screenshot(**options)
browser.screenshot(path: file, full: true)

File.open(file, "rb") do |f|
expect(ImageSize.new(f.read).size).to eq([1280, 1024])
expect(ImageSize.new(f.read).size).to eq([1280, 1024].map { |s| s * device_pixel_ratio })
end
expect(browser.viewport_size).to eq([1024, 768])
end
Expand Down

0 comments on commit a1baac9

Please sign in to comment.