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

Make Chromedriver truly headless #982

Merged
merged 2 commits into from
Jun 26, 2019

Conversation

gabebw
Copy link
Contributor

@gabebw gabebw commented Jun 19, 2019

When I use Suspenders' configuration on a new project, JavaScript tests open up the Chrome app in a real window (i.e. not headless).

When using the configuration in this commit, Chrome quietly bounces once, in the taskbar (on macOS), and does not create a window.

When I use Suspenders' configuration on a new project, JavaScript tests
open up the Chrome app in a real window.

When using the configuration in this commit, Chrome quietly bounces
once, in the taskbar (on macOS), and does not create a window.
@composerinteralia
Copy link
Contributor

I just started a new project and was frustrated to see my tests open up a non-headless chrome. I was excited to see this PR, but unfortunately the change had no effect for me. Was there anything else you needed to do to get this working?

@gabebw
Copy link
Contributor Author

gabebw commented Jun 21, 2019

Maybe the version of Chrome matters?

I'm using:

  • Chrome 75.0.3770.100 (Official Build) (64-bit) (taken from chrome://version/)
  • macOS Mojave 10.14.5

There is technically slightly more stuff in my spec/support/chromedriver.rb, e.g the changes from #981 and #983, though I think that doesn't affect it. Regardless, here is my full spec/support/chromedriver.rb:

require "selenium/webdriver"

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Capybara.register_driver :headless_chrome do |app|
  options = ::Selenium::WebDriver::Chrome::Options.new
  options.headless!
  # Set a big (15" MBP retina resolution) window size so that
  # `save_and_open_screenshot` captures the whole page.
  options.add_argument '--window-size=1680,1050'

  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: { args: %w(headless disable-gpu) },
    loggingPrefs: {
      browser: 'ALL'
    }
  )

  Capybara::Selenium::Driver.new app,
    browser: :chrome,
    options: options,
    desired_capabilities: capabilities
end

Capybara.javascript_driver = :headless_chrome

# Silence Puma's startup message in specs
# https://github.com/rspec/rspec-rails/issues/1897
Capybara.server = :puma, { Silent: true }

@delphaber
Copy link
Contributor

I can confirm that this PR makes my tests go headless. Anyway, I remember I was able to run them headless without this PR, but now I need this PR.

So maybe it depends on a newever Chrome version?

I'm wondering what headless means in chromeOptions: { args: %w(headless disable-gpu) },. Do we need it?

Google Chrome 75.0.3770.100 (Build ufficiale) (a 64 bit)
Revisione cd0b15c8b6a4e70c44e27f35c37a4029bad3e3b0-refs/branch-heads/3770@{#1033}
Sistema operativo Mac OS X 10.14.5

@composerinteralia
Copy link
Contributor

OK, I updated all the things and this is working for me now. After a bit of digging, I am not convinced the chromeOptions: { args: %w(headless disable-gpu) }, is doing anything anymore. It looks like the key actually changed to goog:chromeOptions in SeleniumHQ/selenium@0ba8188#diff-6d36ad83184bcee9c97af166e99e3f4b.
I tried changing that key without including any of the changes in this PR and it did make chromedriver headless.

It may also be fine to lose the disable-gpu options. I am not sure exactly why that option was there in the first place, but Selenium was using it as a workaround for windows and removed it in SeleniumHQ/selenium#6943.

@gabebw
Copy link
Contributor Author

gabebw commented Jun 26, 2019

I confirm that on macOS, with Chrome 75, using just headless! runs the tests in actual headless Chrome. I've updated this PR appropriately, in 8f33976.

@composerinteralia composerinteralia merged commit 75158c8 into thoughtbot:master Jun 26, 2019
@composerinteralia
Copy link
Contributor

Thanks!

@gabebw gabebw deleted the gbw-headless-chromedriver branch June 27, 2019 00:58
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

Successfully merging this pull request may close these issues.

3 participants