rubycdp / ferrum

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

Not using full viewport using browserless 2 docker image #492

Open bk-one opened 2 months ago

bk-one commented 2 months ago

If you initialize Ferrum with a window size using remote browserless 2 - the window size is applied, but the viewport is not updated automatically. The DOM is rendered in a 800x600 frame, even if the browser window is set to 1900x1080.

I'm not sure if it's a bug or expected behavior, just leaving this here for reference. The workaround to utilize the full viewport is easy:

page.driver.browser.set_viewport width: 1900, height: 1080

But I had to explicitly add this, to use the full window.

Expected behavior: The viewport is automatically extended to the window size given when initializing Ferrum. Did not have the issue when using a local chromium, this appeared after switching to browserless 2.

For reference:

Screenshot 2024-09-10 at 11 44 50
bk-one commented 1 month ago

Some additional information, maybe helpful.

require 'ferrum'
# using local chromium
browser = Ferrum::Browser.new window_size: [1920, 1080] 
browser.viewport_size
=> [1920, 993]

# using browserless 2 via ws connection
browser = Ferrum::Browser.new window_size: [1920, 1080], ws_url: 'ws://chrome:3000?token=XYZ'
browser.viewport_size
=> [800, 600]

And something seems to be resetting it back to 800, 600

require 'ferrum'
browser = Ferrum::Browser.new window_size: [1920, 1080], ws_url: 'ws://chrome:3000?token=XYZ'
browser.viewport_size
=> [800, 600]
bowser.set_viewport height: 1080, width: 1920
=> {}
browser.viewport_size
=> [1920, 1080]
browser.go_to 'https://github.com'
=> "35732CEB91B12F9DDDB3C1FCCB2DF06B"
browser.viewport_size
=> [800, 600]

However, there seems to be a way to change the default viewport in browserless, by passing launch options to the ws_url. This did the trick for me eventually:

browser = Ferrum::Browser.new window_size: [1920, 1080], ws_url: 'ws://chrome:3000?token=XYZ&launch={"defaultViewport":{"height":1080,"width":1920}}'
browser.viewport_size
=> [1920, 1080]

Still a bit un-intuitive, but not sure if this is related to ferrum or rather to browserless. Keeping this open for reference, though.