rubycdp / ferrum

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

Browserless 2.0 support #439

Closed Fodoj closed 4 months ago

Fodoj commented 5 months ago

Did anyone manage to make it work with Browserless 2.0? I am running everything in containers, meaning my rails app container connects to browsleress container. Even with the HEAD version, if I pass ws_url, it still stays localhost:4000 and tests are failing:

Cuprite

Capybara.register_driver(:browserless) do |app|
  Capybara::Cuprite::Driver.new(
    app, 
    **{
      host: 'chrome', #'
      url: 'http://172.23.0.3:4000', 
      ws_url: 'ws://chrome:4000', 
      browser_options: { 
        headless: true,
        'no-sandbox': nil
      },
      process_timeout: 15,
      slowmo: 0.1,
      headless: "new",
      timeout: 20,
      window_size: [ 1920, 2048 ], 
    }
  )
end

Ferrum

https://github.com/rubycdp/ferrum/blob/main/lib/ferrum/browser.rb#L242

    242: def start
    243:   Utils::ElapsedTime.start
    244:   binding.pry
    245:   @process = Process.new(options)
    246: 
    247:   begin
 => 248:     @process.start
    249:     @options.default_user_agent = @process.default_user_agent
    250: 
    251:     @client = Client.new(@process.ws_url, options)
    252:     @contexts = Contexts.new(@client)
    253:   rescue StandardError
    254:     @process.stop
    255:     raise
    256:   end
    257: end

Result:

[6] pry(#<Capybara::Cuprite::Browser>)> options.ws_url
=> "ws://chrome:4000"
[7] pry(#<Capybara::Cuprite::Browser>)> @process.ws_url
=> #<Addressable::URI:0x7f1c URI:ws://0.0.0.0:4000>

Because of this my tests won't even start, as they quickly fail during initialization phase.

route commented 5 months ago

It's described here and works https://evilmartians.com/chronicles/system-of-a-test-setting-up-end-to-end-rails-testing#dockerizing-system-tests

Fodoj commented 5 months ago

I have this setup already working. The article was written in 2020, Browserless 2.0 was released in December 2023. Browserless 1.x works without any issues, but new one works as describe in the issue above.

Fodoj commented 5 months ago

See also: https://github.com/browserless/browserless/blob/main/MIGRATION-2.0.md#browserless-20-migration-guide

route commented 5 months ago

Got it, I'll check then.

Aubermean commented 4 months ago

Did you ever manage to get it working or find out what the issue was? @Fodoj

route commented 4 months ago

It's not my top priority but it works, so I don't know what is not working:

require "ferrum"

browser = Ferrum::Browser.new(ws_url: "wss://chrome.browserless.io?token=xxx")
browser.version
page = browser.create_page
page.go "http://example.com"
page.screenshot(path: "a.png")
sleep 5
browser.quit
Fodoj commented 4 months ago

As of my knowledge, browserless.io SaaS doesn't offer browserless 2.0 yet.

Fodoj commented 4 months ago

Just fyi, from Browserless support. In my case I was testing with local containers, not with the cloud offering.

Bildschirmfoto 2024-02-22 um 16 32 32
Aubermean commented 4 months ago

I figured out that Ferrum is stripping the query params, and in a newer version of ferrum that added ws support without a url, it is obtaining the ws_url from /json/version which incorrectly sets ws_url to 0.0.0.0:3000 :)

I am in the process of submitting a PR that fixes this

route commented 4 months ago

As of my knowledge, browserless.io SaaS doesn't offer browserless 2.0 yet.

Ah I see now. That's why it working. Then I need more tests and check out the PR

Fodoj commented 4 months ago

@route try with production-sfo.browserless.io endpoint, per their support this one is backed by v2 (or just local container)

route commented 4 months ago

Fixed in master