Skip to content

Commit c2a7b82

Browse files
Use headless driver for next Rails release (rspec#2746)
* Use headless driver for next Rails release 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
1 parent ed3adba commit c2a7b82

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/rspec/rails/example/system_example_group.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ def initialize(*args, &blk)
137137
self.class.before do
138138
# A user may have already set the driver, so only default if driver
139139
# is not set
140-
driven_by(:selenium) unless @driver
140+
if ::Rails::VERSION::STRING.to_f >= 7.2
141+
driven_by(:selenium_chrome_headless) unless @driver
142+
else
143+
driven_by(:selenium) unless @driver
144+
end
141145
end
142146
end
143147

spec/rspec/rails/example/system_example_group_spec.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module RSpec::Rails
3030
end
3131

3232
describe '#driver' do
33-
it 'uses :selenium driver by default' do
33+
it 'uses :selenium driver by default', if: ::Rails::VERSION::STRING.to_f < 7.2 do
3434
group = RSpec::Core::ExampleGroup.describe do
3535
include SystemExampleGroup
3636
end
@@ -40,6 +40,16 @@ module RSpec::Rails
4040
expect(Capybara.current_driver).to eq :selenium
4141
end
4242

43+
it 'uses :selenium_chrome_headless driver by default', if: ::Rails::VERSION::STRING.to_f >= 7.2 do
44+
group = RSpec::Core::ExampleGroup.describe do
45+
include SystemExampleGroup
46+
end
47+
example = group.new
48+
group.hooks.run(:before, :example, example)
49+
50+
expect(Capybara.current_driver).to eq :selenium_chrome_headless
51+
end
52+
4353
it 'sets :rack_test driver using by before_action' do
4454
group = RSpec::Core::ExampleGroup.describe do
4555
include SystemExampleGroup

0 commit comments

Comments
 (0)