Skip to content

Commit

Permalink
Use headless driver for next Rails release
Browse files Browse the repository at this point in the history
In the next release of Rails, the default driver was switched from
`:chrome` to `:headless_chrome` as see in: rails/rails#50512
This is to ensure the new [ci template][] will "work out of the box".

However, this will not work with applications using `rspec-rails`, since
it still defaults to `:selenium`. Instead, GitHub actions will fail with
the following error:

```
Selenium::WebDriver::Error::SessionNotCreatedError:
            session not created: Chrome failed to start: exited normally.
              (session not created: DevToolsActivePort file doesn't exist)
              (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
```

[ci template]: https://github.com/rails/rails/blob/main/railties/lib/rails/generators/rails/app/templates/github/ci.yml.tt
  • Loading branch information
stevepolitodesign committed Mar 16, 2024
1 parent 260b3bf commit 6f49c11
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rspec/rails/example/system_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ def initialize(*args, &blk)
self.class.before do
# A user may have already set the driver, so only default if driver
# is not set
driven_by(:selenium) unless @driver
if ::Rails::VERSION::STRING.to_f >= 7.2
driven_by(:selenium_chrome_headless) unless @driver
else
driven_by(:selenium) unless @driver
end
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/rspec/rails/example/system_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ module RSpec::Rails
end
end

describe '#driver', if: ::Rails::VERSION::STRING.to_f >= 7.2 do
it 'uses :selenium_chrome_headless driver by default' do
group = RSpec::Core::ExampleGroup.describe do
include SystemExampleGroup
end
example = group.new
group.hooks.run(:before, :example, example)

expect(Capybara.current_driver).to eq :selenium_chrome_headless
end
end

describe '#after' do
it 'sets the :extra_failure_lines metadata to an array of STDOUT lines' do
allow(Capybara::Session).to receive(:instance_created?).and_return(true)
Expand Down

0 comments on commit 6f49c11

Please sign in to comment.