Headless Chrome with Capybara/Selenium not working

34 views
Skip to first unread message

Jack Royal-Gordon

unread,
Nov 30, 2020, 7:25:37 PM11/30/20
to rs...@googlegroups.com
I know that this is not specifically an RSpec issue, but the Capybara user forum is unresponsive and appears relatively inactive, and the RSpec group has been very responsive and informative on topics beyond the strict scope of RSpec. I’m trying to implement headless Chrome with Capybara and Selenium for RSpec feature tests, and I followed the instructions in the following article: https://www.imaginarycloud.com/blog/from-capybara-webkit-to-headless-chrome-and-chromedriver/

I’ve added the following gems: webdrivers (v 3.9.4), capybara-selenium (v 0.0.6), and capybara (v 3.15.1). In my spec_helper.rb file, I’ve added the following code:

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

Capybara.register_driver :headless_chrome do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: {
      args: %w[headless enable-features=NetworkService,NetworkServiceInProcess]
    }
  )

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

Capybara.default_driver = :headless_chrome
Capybara.javascript_driver = :headless_chrome

What I’m getting, when I run a feature spec with js:true is a full browser, which is obviously not what I’m expecting. Is there anything obvious that I’m doing wrong here?

Phil Pirozhkov

unread,
Dec 1, 2020, 10:35:44 AM12/1/20
to Jack Royal-Gordon
Jack,

Do you mean that it opens the Chrome application window?
I recall we were making some changes to prevent Rails from overriding the default drivers.



--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/828B0D8D-B280-4A3E-B416-F78658A5A754%40pobox.com.

Jack Royal-Gordon

unread,
Dec 1, 2020, 2:50:40 PM12/1/20
to rs...@googlegroups.com
When I tried that, I got a “NoMethodError”. The example was a feature, not a system spec, and #driven_by is only defined for system tests. So I made it a system test, and then I got the following error:

LoadError: cannot load such file -- action_dispatch/system_test_case System test integration requires Rails >= 5.1 and has a hard dependency on a webserver and `capybara`, please add capybara to your Gemfile and configure a webserver (e.g. `Capybara.server = :webrick`) before attempting to use system specs.

I’m on 5.0 (haven’t finished testing with 5.0 so not ready to migrate to 5.1). The message leads me to believe that headless Chrome will only work on Rails >= 5.1 (which makes sense, since the articles on headless chrome started showing up about two months after 5.1 was released).

Jack Royal-Gordon

unread,
Dec 2, 2020, 1:16:09 PM12/2/20
to rs...@googlegroups.com
Follow-up question, now I’m on RSpec 3.9.3 with Ruby 2.4.10 and Rails 5.1.7, and it seems that the correct solution for getting headless chrome includes the following in the RSpec configuration (instead of the Capybara #register_driver calls):

RSpec.configure do |config|
  config.before(:each, type: :system, js: true) do
    driven_by :selenium_chrome_headless
  end
end

What I’m wondering is why “driven_by” is only supported on “system” specs and not on “feature” specs. Feature specs with “js: true” will always run the full Chrome version, as it stands now (I’ve checked rspec-rails and determined that #driven_by is defined in RSpec::Rails::SystemExampleGroup). Or is that something that will change if I upgrade one or more gems? My understanding is that the only difference between the two modes is whether or not a browser display appears, so if you can do all the same things in a feature spec as a system spec, why can’t you go headless with a feature spec?

supa...@gmail.com

unread,
Dec 2, 2020, 6:59:22 PM12/2/20
to rspec
as far as I know driven_by is part of the Rails DSL for system specs, not RSpec feature specs. I've never had to use it with feature specs

Actually I've never used Rails new system specs, I've always used RSpec/Capybara feature specs with both headless & non headless drivers for years, they work great

Here's how I configure headless chrome with capybara

## Headless!
Capybara.register_driver :headless_chrome do |app|
  options = Selenium::WebDriver::Chrome::Options.new

  options.add_argument('--headless')
  options.add_argument('--disable-gpu')
  options.add_argument('--test-type')
  options.add_argument('--ignore-certificate-errors')
  options.add_argument('--disable-popup-blocking')
  options.add_argument('--disable-extensions')
  options.add_argument('--enable-automation')
  options.add_argument('--window-size=1920,1080')
  options.add_argument("--start-maximized")

  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    logging_prefs: { 'browser' => 'ALL' }
  )

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

## Capy Configuration Defaults
Capybara.configure do |config|
  config.server                = :puma
  config.javascript_driver     = (ENV['HEADLESS'] || ENV['H']) ? :headless_chrome : :chrome
  config.default_max_wait_time = 7
end

I use an environment variable to control when I pick headless or not from the command line, i.e.

H=1 bundle exec rspec spec/models/

feature specs go in a /features directory under my /spec directory and look like the following, note the :js tag to trigger the browser vs the capybara default :rack_test (no browser)

RSpec.feature 'Admin can do stuff, :js do
  describe 'fancy feature' do
    it 'works well' do
      # spec implementation
    end
  end
end

Jack Royal-Gordon

unread,
Dec 2, 2020, 7:55:14 PM12/2/20
to rs...@googlegroups.com
I am still wondering why the decision was made not to add this RSpec functionality to feature specs — I think that there is no other difference between feature specs and system specs, although looking at the code for SystemExampleGroup vs. FeatureExampleGroup there seems quite a significant difference. Perhaps it’s because it’s driving Rails’ ActionDispatch::Integration instead of being RSpec-only.

I guess the difference is that your approach is independent of RSpec, whereas mine uses RSpec. I could not get your approach to fire up headless Chrome (although I didn’t try your exact code, just code that should have the same effect (some options are different, but “headless” is still there.

Jon Rowe

unread,
Dec 3, 2020, 8:30:10 AM12/3/20
to rs...@googlegroups.com
Hi Jack

I can explain this a bit. RSpec itself has no inbuilt support for browser testing at all.

Feature specs are a configuration of Capybara on top of RSpec that is part of `rspec-rails` a “convention other configuration” from when that was the recommended way to go with testing full stack setups within Rails.

System specs are Rails own replacement for this previous “convention”.

If you are using system specs I would probably replace feature specs with them.

Hope that helps!

Phil Pirozhkov

unread,
Dec 3, 2020, 8:47:57 AM12/3/20
to Jack Royal-Gordon
To add to what Jon said, there's this in the README, which
unfortunately didn't make it to the documentation on Relish
https://github.com/rspec/rspec-rails/#system-specs-feature-specs-request-specswhats-the-difference
> --
> You received this message because you are subscribed to the Google Groups "rspec" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/dejalu-217-5bc1a821-9fb1-489c-9633-136b1157c4dc%40jonrowe.co.uk.

Jack Royal-Gordon

unread,
Dec 3, 2020, 2:23:02 PM12/3/20
to rs...@googlegroups.com
Thanks, Jon and Phil. That clarifies things greatly. System Specs it is!
> To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/CAAk5Ok9pZF%2B-wzdbaQbMuW2fsJ2RUri603as9v%3DSt7iDSctAWA%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages