hound
hound copied to clipboard
Use headless chrome
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?
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
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..
We merge anything you pass in the :driver
option
https://github.com/HashNuke/hound/blob/d269427310f2cb63a66e6d5024a4279681a4d0b4/lib/hound/session.ex#L46
Ah, on the session level, thanks.
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"
]}})
@tuvistavie Would you accept a pull request to make this a configuration option?
@gliwka Yes, a PR for this would be very welcome, thank you!
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 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"
]}
}
)
@MikaAK Also ensure that your ChromeDriver version is up to date.
I have it working on:
ChromeDriver 2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b)
My params look like this, btw...
%{driver: %{
browserName: "chrome",
chromeOptions: %{"args" => [
"--headless",
"--disable-gpu"
]}
}}
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.
Is there a setting to restrict all the images from loading?
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
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"
]}
})
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.
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"