rubycdp / ferrum

Headless Chrome Ruby API
https://ferrum.rubycdp.com
MIT License
1.69k stars 120 forks source link

fix: Chrome doesn't change resolution #399

Closed route closed 10 months ago

route commented 10 months ago

@aaronjensen interesting but now I've got failing tests on my laptop, when this deviceScaleFactor is set to 0, though same Chrome version on github doesn't show broken tests. Same main branch was working for me for today before my experiments, but now it constantly failing for 117.

aaronjensen commented 10 months ago

What are the failures? I can see if I can reproduce.

aaronjensen commented 10 months ago

I see exactly one failing test, and the failure does seem related, but it may point to Chrome having changed their default scaling for headless browsers to 2x, which means that the test may be the thing that needs to be corrected. If the test produces different results on different machines, it could be that Chrome is somehow taking into account the device's scaling (I do have a high dpi monitor and my window.devicePixelRatio is 2).

  1) Ferrum::Page::Screenshot#screenshot supports screenshotting the whole of a page that goes outside the viewport
     Failure/Error: expect(ImageSize.new(f.read).size).to eq(browser.viewport_size)

       expected: [1024, 768]
            got: [2048, 1536]

       (compared using ==)
     Shared Example Group: "screenshot screen" called from ./spec/page/screenshot_spec.rb:158
     # ./spec/page/screenshot_spec.rb:16:in `block (4 levels) in <top (required)>'
     # ./spec/page/screenshot_spec.rb:15:in `open'
     # ./spec/page/screenshot_spec.rb:15:in `block (3 levels) in <top (required)>'
aaronjensen commented 10 months ago

I propose that we allow deviceScaleFactor to be controlled the same way height and width is. Then the test can be set up to specify that. It should default to 0 so that it will respect the machine it is running on.

route commented 10 months ago

@aaronjensen you should remove fail-fast in .rspec file and you can see more failing tests. What OS are you on?

aaronjensen commented 10 months ago

macOS Ventura

As a point of feedback, it is maximally surprising to have rspec default to fail-fast. That's an end-user choice. I would have never known until I got incredibly frustrated playing test whack-a-mole.

aaronjensen commented 10 months ago

All of the failing tests are exactly the same. It is respecting my device scaling now. As a result all screenshots are higher resolution. That is, they are more accurate to what my device should actually produce. I'd suggest either allowing device pixel ratio to be controlled and updating those tests to control it, or updating the tests to test for a multiple of the expected size, rather than the exact size if, in fact, the CI server is passing these tests with Chrome 117

aaronjensen commented 10 months ago

Alternatively, the tests can be corrected to expect pixel size when they expect pixel size, rather than the virtual device size with something like this:

def viewport_pixel_size
  evaluate <<~JS
    [window.innerWidth * window.devicePixelRatio,
     window.innerHeight * window.devicePixelRatio]
  JS
end
aaronjensen commented 10 months ago

@route if you let me know what you prefer, I can work on a PR later, I think.

route commented 10 months ago

it could be that Chrome is somehow taking into account the device's scaling (I do have a high dpi monitor and my window.devicePixelRatio is 2)

I have Mac for which window.devicePixelRatio=2 and I have Linux with window.devicePixelRatio=1 on the same hiDPI monitor. So thus I guess it's OS related, and the reason why I don't see failing tests on Linux as it works for both 0 and 1 the same way (like nothing changes). But for Mac where it's set to 2 by default scaling down to 1 makes it change the scaling.

As a point of feedback, it is maximally surprising to have rspec default to fail-fast.

Yea I'll remove that, the only intention was to set it for CI to speed up build.

aaronjensen commented 10 months ago

The change looks good, thanks!