Skip to content

Commit

Permalink
Testing Generator: Improvements
Browse files Browse the repository at this point in the history
Follow-up to #1156

Creates parity with Rails' decision to [use a headless driver by
default][headless].

This will be fixed in an [upcoming release][rspec] of rspec-rails, but I
felt it was important to capture here. Additionally, it ensures the
`screen_size` is the same as what is set in Rails.

Removes `webdrivers` dependency in favor of `selenium-webdriver`. This
generator assumes the app was generated with the `--skip-test`, which
means we need to add the `selenium-webdriver` and `capybara` gems.

Updates `action_dispatch-testing-integration-capybara` dependency to the
most recent tagged release in an effort to suppress Dependabot
notifications.

[headless]:drive://github.com/rails/rails/pull/50512
[rspec]: rspec/rspec-rails#2746
  • Loading branch information
stevepolitodesign committed Mar 21, 2024
1 parent 39efb28 commit e8ac2a5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 57 deletions.
7 changes: 4 additions & 3 deletions lib/generators/suspenders/testing_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ def add_gems
end

gem_group :test do
gem "capybara"
gem "action_dispatch-testing-integration-capybara",
github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.0",
github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.1",
require: "action_dispatch/testing/integration/capybara/rspec"
gem "selenium-webdriver"
gem "shoulda-matchers", "~> 6.0"
gem "webdrivers"
gem "webmock"
end

Expand Down Expand Up @@ -59,7 +60,7 @@ def create_system_spec_dir
end

def configure_chromedriver
copy_file "chromedriver.rb", "spec/support/chromedriver.rb"
copy_file "driver.rb", "spec/support/driver.rb"
end

def configure_i18n_helper
Expand Down
27 changes: 0 additions & 27 deletions lib/generators/templates/testing/chromedriver.rb

This file was deleted.

5 changes: 5 additions & 0 deletions lib/generators/templates/testing/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
end
end
33 changes: 6 additions & 27 deletions test/generators/suspenders/testing_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class TestingGeneratorTest < Rails::Generators::TestCase
end
group :test do
gem "action_dispatch-testing-integration-capybara", github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.0", require: "action_dispatch/testing/integration/capybara/rspec"
gem "capybara"
gem "action_dispatch-testing-integration-capybara", github: "thoughtbot/action_dispatch-testing-integration-capybara", tag: "v0.1.1", require: "action_dispatch/testing/integration/capybara/rspec"
gem "selenium-webdriver"
gem "shoulda-matchers", "~> 6.0"
gem "webdrivers"
gem "webmock"
end
RUBY
Expand Down Expand Up @@ -90,40 +91,18 @@ class TestingGeneratorTest < Rails::Generators::TestCase
end
end

test "configures Chromedriver" do
test "configures driver" do
expected = <<~RUBY
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!
options.add_argument "--window-size=1680,1050"
Capybara::Selenium::Driver.new app,
browser: :chrome,
options: options
end
Capybara.javascript_driver = :headless_chrome
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :rack_test
end
config.before(:each, type: :system, js: true) do
driven_by Capybara.javascript_driver
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
end
end
RUBY

run_generator

assert_file app_root("spec/support/chromedriver.rb") do |file|
assert_file app_root("spec/support/driver.rb") do |file|
assert_equal expected, file
end
end
Expand Down

0 comments on commit e8ac2a5

Please sign in to comment.