From a1baac9c150ad0576c0269c49a4ca7288ff8dc34 Mon Sep 17 00:00:00 2001 From: Dmitry Vorotilin Date: Thu, 14 Sep 2023 10:43:39 +0300 Subject: [PATCH] fix: take device_pixel_ratio into account --- .rspec | 1 - CHANGELOG.md | 2 ++ lib/ferrum/browser.rb | 2 +- lib/ferrum/page.rb | 2 +- lib/ferrum/page/screenshot.rb | 6 ++++++ spec/page/screenshot_spec.rb | 19 +++++++++---------- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/.rspec b/.rspec index e1e7cb66..221fc808 100644 --- a/.rspec +++ b/.rspec @@ -1,4 +1,3 @@ --color ---fail-fast --format=doc --require spec_helper diff --git a/CHANGELOG.md b/CHANGELOG.md index c2403094..93d124c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/ferrum/browser.rb b/lib/ferrum/browser.rb index 11f8e1fa..cbad3b3e 100644 --- a/lib/ferrum/browser.rb +++ b/lib/ferrum/browser.rb @@ -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 diff --git a/lib/ferrum/page.rb b/lib/ferrum/page.rb index 0a5aee3d..2e83d4b2 100644 --- a/lib/ferrum/page.rb +++ b/lib/ferrum/page.rb @@ -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 diff --git a/lib/ferrum/page/screenshot.rb b/lib/ferrum/page/screenshot.rb index fdbe1a28..580ed1b7 100644 --- a/lib/ferrum/page/screenshot.rb +++ b/lib/ferrum/page/screenshot.rb @@ -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, diff --git a/spec/page/screenshot_spec.rb b/spec/page/screenshot_spec.rb index fee140b8..19829768 100644 --- a/spec/page/screenshot_spec.rb +++ b/spec/page/screenshot_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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