hound icon indicating copy to clipboard operation
hound copied to clipboard

Use headless chrome

Open jfrolich opened this issue 7 years ago • 17 comments

Recently there has been progress in creating a real headless chrome. This would probably make integration testing a lot more robust and (probably) faster.

Some instructions on how to use it are here: https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md. What would be the effort on making this work with hound? Any thoughts?

jfrolich avatar Nov 01 '16 03:11 jfrolich

You can do this already with hound. No modification needed.

Just setup Hound to talk to ChromeDriver and pass the --headless and --disable-gpu command line options using the capabilities. Make sure you've got a recent dev channel build and create a symlink from the unstable binary to one of those ChromeDriver searches for: https://github.com/bayandin/chromedriver/blob/e9a1f55b166ea62ef0f6e78da899d9abf117e88f/chrome/chrome_finder.cc#L91

If you don't want to add --headless and --disable-gpu to the capabilities each time, just add both of them to this array and compile ChromeDriver yourself: https://github.com/bayandin/chromedriver/blob/e9a1f55b166ea62ef0f6e78da899d9abf117e88f/chrome_launcher.cc#L70

gliwka avatar Jan 12 '17 17:01 gliwka

I don't really see how to pass these extra arguments to the chrome driver. Reading the code, I see the only args that are given to chrome is the --user-agent= flag here..

arjan avatar May 30 '17 07:05 arjan

We merge anything you pass in the :driver option

https://github.com/HashNuke/hound/blob/d269427310f2cb63a66e6d5024a4279681a4d0b4/lib/hound/session.ex#L46

danhper avatar May 30 '17 09:05 danhper

Ah, on the session level, thanks.

arjan avatar May 30 '17 18:05 arjan

I got it to work using:

use Hound.Helpers

hound_session(driver: %{chromeOptions: %{"args" => ["--headless", "--disable-gpu"]}})

But I think this replaces the "args" set by Hound.Browser.Chrome.user_agent_capabilities/1. So I guess this is a more complete solution:

use Hound.Helpers

hound_session(driver: %{chromeOptions: %{"args" => [
  "--user-agent=#{Hound.Browser.user_agent(:chrome)}",
  "--headless",
  "--disable-gpu"
]}})

aptinio avatar Jun 07 '17 06:06 aptinio

@tuvistavie Would you accept a pull request to make this a configuration option?

gliwka avatar Jun 07 '17 08:06 gliwka

@gliwka Yes, a PR for this would be very welcome, thank you!

danhper avatar Jun 07 '17 09:06 danhper

I'm unable to get this working, I would happy to create a PR if someone could point me how to actually enable this. I'm on chrome 60 and trying to use chrome_driver. I've tried calling

    Hound.start_session %{
      browser: "chrome",
      driver: %{
        chromeOptions: %{
          "args" => [
            "--user-agent=#{Hound.Browser.user_agent(:chrome)}",
            "--headless",
            "--disable-gpu"
          ]
        }
      }
    }

but this doesn't seem to work since it still opens browser windows.

MikaAK avatar Sep 05 '17 11:09 MikaAK

@MikaAK recently I had similar problem and I've submitted: https://github.com/HashNuke/hound/pull/174

Try:

    Hound.start_session(
        additional_capabilities: %{
          chromeOptions: %{ "args" => [
              "--user-agent=#{Hound.Browser.user_agent(:chrome)}",
              "--headless",
              "--disable-gpu"
          ]}
        }
    )

arathunku avatar Sep 05 '17 12:09 arathunku

@MikaAK Also ensure that your ChromeDriver version is up to date. I have it working on: ChromeDriver 2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b)

darksheik avatar Sep 05 '17 12:09 darksheik

My params look like this, btw...

%{driver: %{
         browserName: "chrome",
         chromeOptions: %{"args" => [
           "--headless",
           "--disable-gpu"
         ]}
       }}

darksheik avatar Sep 05 '17 12:09 darksheik

Had several hours of difficulty with this one but finally got it working thanks to pointers by @aptinio and the recently merged PR by @arathunku.

Difference I faced was I wanted to use the Ecto sandbox in addition to headless Chrome. Following this documentation https://github.com/phoenixframework/phoenix_ecto#concurrent-browser-tests I attempted this:

    metadata = Phoenix.Ecto.SQL.Sandbox.metadata_for(MyApp.Repo, self())
    Hound.start_session(metadata: metadata,
                        additional_capabilities: %{
                          chromeOptions: %{ "args" => [
                            "--user-agent=#{Hound.Browser.user_agent(:chrome)}",
                            "--headless",
                            "--disable-gpu"
                            ]}
                          })

The problem is setting args in the chromeOptions like this replaces the metadata which is embedded by Hound in args and results in an Ecto/DBConnection ownership exception. I tried not setting the --user-agent but that also did not work as it appears the args are not merged.

Finally got it working as below... hope this helps someone:

    metadata = Phoenix.Ecto.SQL.Sandbox.metadata_for(MyApp.Repo, self())
    Hound.start_session(additional_capabilities: %{
                          chromeOptions: %{ "args" => [
                            "--user-agent=#{Hound.Browser.user_agent(:chrome) |> Hound.Metadata.append(metadata)}",
                            "--headless",
                            "--disable-gpu"
                            ]}
                          })

It would be great if we could specify headless chrome using configuration options instead but it would be a pretty tough PR for me to try to tackle -- and this is working.

tmepple avatar Sep 08 '17 19:09 tmepple

Is there a setting to restrict all the images from loading?

vijit13 avatar Oct 03 '17 16:10 vijit13

Even with using browser: "chrome_headless" and driver: "chromedriver" it doesn't work 😢 I'm on chromedriver 2.33.506106. I'm still not sure what's going on here

MikaAK avatar Oct 22 '17 09:10 MikaAK

Using @arathunku's suggestion above, we were able to get headless Chrome working. (Slightly modified like this:)

hound_session(additional_capabilities: %{
  chromeOptions: %{ "args" => [
    "--user-agent=#{Hound.Browser.user_agent(:chrome)}",
    "--headless",
    "--disable-gpu"
  ]}
})

paulstatezny avatar Jul 18 '18 18:07 paulstatezny

Same here. chrome_headless in this config:

config :hound, driver: "chrome_driver", browser: "chrome_headless", app_port: 4002

does not have any effect. I'm using ChromeDriver 2.41.578706. It opens the browser's window.

The workaround from the comment above works.

marekciupak avatar Jan 09 '19 22:01 marekciupak

The latest changes haven't been packaged yet, to use the master: in mix.exs use: {:hound, git: "[email protected]:HashNuke/hound.git"} and in config.exs use config :hound, driver: "chrome_driver", browser: "chrome_headless"

aus70 avatar Feb 19 '19 01:02 aus70