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

Code before:
  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

        def browser_options

Review comment (suggestion):
One more thought - in https://github.com/rails/rails/issues/32468, @Edouard-chin had proposed that `driven_by` could be given an additional parameter that allows consumers specify the name to use when registering the driver with Capybara. Is that something that we should consider? (It could be an optional parameter, falling back to the `#{driver}_#{using}_#{object_id}` default.)

It might make the "constructing a `Capybara::Session` manually" use case easier since the consumer could simply pass the symbol they used in the original `driven_by` declaration, rather than getting the `ActionDispatch::SystemTesting::Driver` instance and calling `name` on it:

```rb
driven_by :selenium,
          using: :chrome,
          options: { emulation: { device_name: 'iPhone 4'} },
          as: :selenium_chrome_iphone_4
```
```rb
session = Capybara::Session.new(:selenium_chrome_iphone_4, Capybara.app)
```

Code after:
  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
          end