Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browserless 2 support #501

Open
4tolexx opened this issue Nov 20, 2024 · 5 comments
Open

Browserless 2 support #501

4tolexx opened this issue Nov 20, 2024 · 5 comments
Assignees

Comments

@4tolexx
Copy link

4tolexx commented Nov 20, 2024

does ferrum have support for browserless version 2.

i am trying to migrate from selenium to cuprite. everything seems to be running fine using browserless/chrome version 1 in docker. i set it up following https://evilmartians.com/chronicles/system-of-a-test-setting-up-end-to-end-rails-testing

i have been trying to migrate to browserless chrome using pretty much the same setup but it does not work. below is my config using docker compose

 chrome:
    image: ghcr.io/browserless/chrome:latest
    ports:
      - 3000:3000
    environment:
      HEADLESS: false
      TIMEOUT: 600000
    shm_size: 512m

cuprite setup

Capybara.register_driver(:cuprite) do |app|
  Capybara::Cuprite::Driver.new(
    app,
    **{
      url: 'http://chrome:3000',
      headless: ENV['HEADLESS'],
      process_timeout: 10,
      timeout: 30,
      window_size: [1920, 1080],
      browser_options: { 'no-sandbox': nil, 'disable-gpu': nil },
      save_path: DownloadHelper::PATH
    }
  )
end

i am getting the error:

Errno::ECONNREFUSED:
            Connection refused - connect(2) for "0.0.0.0" port 3000
@jschroeder
Copy link

jschroeder commented Nov 23, 2024

I am having the same issue.
I did find the cause of the problem in Ferrum v0.15.

When Ferrum establishes the connection to chrome in your chrome container, it first makes an HTTP call to the provided host/port to fetch some meta data (Ferrum::Browser::Process#parse_json_version). That meta data includes the actual webSocketDebuggerUrl, which Ferrum will try to utilize. But Browserless 2 returns an incorrect value (host 0.0.0.0 instead of the original hostname).
The subsequent websocket calls to that URL will fail, unless 0.0.0.0:9222 (or :3000 in your case) happens to be bound to the chrome container's port.

That code has changed in HEAD. All will work if you install the gem from git/HEAD, or when a version past v0.15 is out.

@leoc
Copy link

leoc commented Nov 25, 2024

@jschroeder Thanks for the hint. I have used ws_url: "ws://chrome:3333" for the driver setup which should be the correct setup for my browserless container named chrome and the port set to 3333. It resolved the Errno::ECONNREFUSED.

But unfortunately I now only receive Ferrum::DeadBrowserError: Browser is dead or given window is closed from Ferrum.

Did you do something else to get it to work?

@jschroeder
Copy link

I certainly have seen your DeadBrowserError before, but not recently. Browserless 2 disposes of sessions after 30 seconds, and it doesn't care if you interact with the session or not. 30 seconds after craetion it will dispose of them, and probably result in a DeadBrowserError. But I see that you extended your TIMEOUT already, so you are already aware of this.

Here are the seginificant parts of my kamal deploy.yml (it should look familiar to your setup:

service: my_app
require_destination: true
image: kamal-images/my_app

servers:
  job:
    hosts:
      - my_app.dev
    cmd: bundle exec good_job start
    env:
      CDP_WS_URL: ws://chrome:9222 # I am using that environment variable to establish a WS connection to Chrome

registry:  <snip>.....</snip>
ssh: <snip>.....</snip>

accessories:
  chrome:
    image: ghcr.io/browserless/chromium
    env:
      PORT: 9222
      TIMEOUT: 600000 # sessions live up to 10 minutes max
    roles:
      - job

Then, when I run a Rails console on the job container (e.g. via kamal console -r job), I can successfully call Ferrum::Browser.new ws_url: ENV['CDP_WS_URL'].

@jschroeder
Copy link

HEADLESS: false

might be an issue. I don't know where the container would display the UI. May be it fails for that reason?

@leoc
Copy link

leoc commented Nov 27, 2024

Thanks for you thoughts! Much appreciated

For future reference: It worked when I chose ws_url: "ws://chrome:3333/chrome". Otherwise browserless did not know which end-point to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants