Skip to content

Commit

Permalink
Use headless driver for next Rails release (#2746)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stevepolitodesign authored Mar 19, 2024
1 parent ed3adba commit c2a7b82
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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: 11 additions & 1 deletion spec/rspec/rails/example/system_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module RSpec::Rails
end

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

it 'uses :selenium_chrome_headless driver by default', if: ::Rails::VERSION::STRING.to_f >= 7.2 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

it 'sets :rack_test driver using by before_action' do
group = RSpec::Core::ExampleGroup.describe do
include SystemExampleGroup
Expand Down

0 comments on commit c2a7b82

Please sign in to comment.