rubycdp / ferrum

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

How to use another docker container to run the browser #311

Closed SirMishaa closed 1 year ago

SirMishaa commented 1 year ago

Hello!

In work, we have a rails project that use Cuprite (which under the hood use ferrum)

Capybara.register_driver(:cuprite) do |app|
  Capybara::Cuprite::Driver.new(app, js_errors: true, window_size: [1200, 800], timeout: 180, inspector: true, browser_options: { 'no-sandbox': nil })
end

Initially, we had our chrome installed in the main app container, (base image is Debian) which is fine, but it's not working on Mac M2. (We do not have really error message, just the test stay stuck, but it's working on Ubuntu)

We have found this Docker image (https://hub.docker.com/r/yukinying/chrome-headless-browser-stable/) which is working with AMD64 & ARM64 and seems to be good. See https://github.com/yukinying/chrome-headless-browser-docker for more information

It runs the browser and exposes a remote debugging in 9222 (chrome:9222).

How can I tell ferrum to use this "remote" chrome instead? Can I set like a remote browser host, and it will talk using Web socket with the remote browser? It will simplify a lot the workflow for us and tests may run fine on Mac (it's running fine without docker, btw).

Thank you in advance!

ttilberg commented 1 year ago

Connecting Ferrum to a remote url is done like this:

browser = Ferrum::Browser.new(url: 'http://localhost:9222')

You should be able to pass that url option into your driver registration as described here.

route commented 1 year ago

@ttilberg thanks! Also there's a great article by Evil Martians https://evilmartians.com/chronicles/system-of-a-test-setting-up-end-to-end-rails-testing

devsheva commented 6 months ago

Context: I have a local app with ferrum installed, and a docker container running chromium instance.

I tried the url option, but i get this error. I tried to explore why is that GET /json/version request made, but didn't find an explanation.

browser = Ferrum::Browser.new(url: "http://localhost:3000")

     JSON::ParserError:
       unexpected token at '<!DOCTYPE html>
       <html lang="en">
       <head>
       <meta charset="utf-8">
       <title>Error</title>
       </head>
       <body>
       <pre>Cannot GET /json/version</pre>
       </body>
       </html>
       '