Repository: rails/rails
File: actionpack/lib/action_dispatch/system_testing/driver.rb (Ruby)

Code before:
module ActionDispatch
  module SystemTesting
    class Driver # :nodoc:
      def initialize(name, **options, &capabilities)
        @name = name
        @screen_size = options[:screen_size]
        @options = options[:options] || {}
        @capabilities = capabilities

        if [:poltergeist, :webkit].include?(name)
          ActiveSupport::Deprecation.warn <<~MSG.squish
            Poltergeist and capybara-webkit are not maintained already.
            Driver registration of :poltergeist or :webkit is deprecated and will be removed in Rails 7.1.
            You can still use :selenium, and also :cuprite is available for alternative to Poltergeist.
          MSG
        end

        if name == :selenium
          require "selenium/webdriver"
          @browser = Browser.new(options[:using])
          @browser.preload
        else
          @browser = nil
        end
      end

      def use
        register if registerable?

        setup
      end

      private
        def registerable?
          [:selenium, :poltergeist, :webkit, :cuprite, :rack_test].include?(@name)
        end

        def register
          @browser&.configure(&@capabilities)

          Capybara.register_driver @name do |app|
            case @name
            when :selenium then register_selenium(app)
            when :poltergeist then register_poltergeist(app)
            when :webkit then register_webkit(app)
            when :cuprite then register_cuprite(app)
            when :rack_test then register_rack_test(app)
            end
          end
        end


Review comment (suggestion):
It's not clear to me why `name` should be `private`. It seems useful for the name of the driver to be public since consumers would want to access it if they are [constructing a `Capybara::Session` manually](https://github.com/teamcapybara/capybara/tree/0995b3eea1d67d7a1da527906f34cbdb573578c9#using-sessions-manually), which requires passing the driver name that was registered:
https://github.com/teamcapybara/capybara/blob/0995b3eea1d67d7a1da527906f34cbdb573578c9/lib/capybara/session.rb#L100

Code after:
module ActionDispatch
  module SystemTesting
    class Driver # :nodoc:
      attr_reader :name

      def initialize(driver_type, **options, &capabilities)
        @driver_type = driver_type
        @screen_size = options[:screen_size]
        @options = options[:options] || {}
        @name = @options.delete(:name) || driver_type
        @capabilities = capabilities

        if [:poltergeist, :webkit].include?(driver_type)
          ActiveSupport::Deprecation.warn <<~MSG.squish
            Poltergeist and capybara-webkit are not maintained already.
            Driver registration of :poltergeist or :webkit is deprecated and will be removed in Rails 7.1.
            You can still use :selenium, and also :cuprite is available for alternative to Poltergeist.
          MSG
        end

        if driver_type == :selenium
          require "selenium/webdriver"
          @browser = Browser.new(options[:using])
          @browser.preload
        else
          @browser = nil
        end
      end

      def use
        register if registerable?

        setup
      end

      private
        def registerable?
          [:selenium, :poltergeist, :webkit, :cuprite, :rack_test].include?(@driver_type)
        end

        def register
          @browser&.configure(&@capabilities)

          Capybara.register_driver name do |app|
            case @driver_type
            when :selenium then register_selenium(app)
            when :poltergeist then register_poltergeist(app)
            when :webkit then register_webkit(app)
            when :cuprite then register_cuprite(app)
            when :rack_test then register_rack_test(app)
            end