{"record":{"id":"72279520de03753d","repo":"teamcapybara/capybara","slug":"the-second-parameter-to-session-new-should-be-a-r","errorCode":null,"errorMessage":"The second parameter to Session::new should be a rack app if passed.","messagePattern":"The second parameter to Session::new should be a rack app if passed\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/capybara/session.rb","lineNumber":81,"sourceCode":"      execute_script evaluate_script evaluate_async_script visit refresh go_back go_forward send_keys\n      within within_element within_fieldset within_table within_frame switch_to_frame\n      current_window windows open_new_window switch_to_window within_window window_opened_by\n      save_page save_and_open_page save_screenshot\n      save_and_open_screenshot reset_session! response_headers\n      status_code current_scope\n      assert_current_path assert_no_current_path has_current_path? has_no_current_path?\n    ].freeze + DOCUMENT_METHODS\n    MODAL_METHODS = %i[\n      accept_alert accept_confirm dismiss_confirm accept_prompt dismiss_prompt\n    ].freeze\n    DSL_METHODS = NODE_METHODS + SESSION_METHODS + MODAL_METHODS\n\n    attr_reader :mode, :app, :server\n    attr_accessor :synchronized\n\n    def initialize(mode, app = nil)\n      if app && !app.respond_to?(:call)\n        raise TypeError, 'The second parameter to Session::new should be a rack app if passed.'\n      end\n\n      @@instance_created = true # rubocop:disable Style/ClassVars\n      @mode = mode\n      @app = app\n      if block_given?\n        raise 'A configuration block is only accepted when Capybara.threadsafe == true' unless Capybara.threadsafe\n\n        yield config\n      end\n      @server = if config.run_server && @app && driver.needs_server?\n        server_options = { port: config.server_port, host: config.server_host, reportable_errors: config.server_errors }\n        server_options[:extra_middleware] = [Capybara::Server::AnimationDisabler] if config.disable_animation\n        Capybara::Server.new(@app, **server_options).boot\n      end\n      @touched = false\n    end\n","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/teamcapybara/capybara/blob/15b5fdb76e972e9623d5af2123ed3755594f9732/lib/capybara/session.rb#L63-L99","documentation":"The second parameter of Capybara::Session.new must be a Rack application - any object responding to #call - because the session's built-in server invokes it to serve requests. Passing anything else (notably a URL string) fails the respond_to?(:call) check with TypeError at construction time.","triggerScenarios":"Capybara::Session.new(:selenium, 'http://localhost:3000') - passing a URL string instead of an app; passing a route/path helper result or a PORO; passing MyApp instead of MyApp::Application in Rails.","commonSituations":"Trying to point a hand-built session at an already-running server (URLs belong in Capybara.app_host, not the app slot); migrating from tools whose constructors take a base URL; picking the wrong constant when both an app class and a routes object exist.","solutions":["Pass the Rack app: Capybara::Session.new(:selenium, Rails.application)","For an external/running server, omit the app and set Capybara.app_host = 'http://localhost:3000'","Prefer the global DSL (Capybara.app=, Capybara.app_host=, visit/page) instead of constructing sessions manually"],"exampleFix":"# before\nsession = Capybara::Session.new(:selenium, 'http://localhost:3000')\n\n# after\nCapybara.app_host = 'http://localhost:3000'\nsession = Capybara::Session.new(:selenium)","handlingStrategy":"type-guard","validationCode":"unless app.nil? || app.respond_to?(:call)\n  raise TypeError, \"#{app.inspect} is not a Rack app; set Capybara.app_host for external servers\"\nend\nsession = Capybara::Session.new(:selenium, app)","typeGuard":"def rack_app?(obj)\n  obj.respond_to?(:call)\nend\n\nsession = rack_app?(candidate) ? Capybara::Session.new(:selenium, candidate) : Capybara::Session.new(:selenium)","tryCatchPattern":"begin\n  Capybara::Session.new(:selenium, app)\nrescue TypeError\n  Capybara.app_host = app.to_s # it was a URL string\n  Capybara::Session.new(:selenium)\nend","preventionTips":["Configure Capybara.app and Capybara.app_host globally instead of hand-building sessions","Remember: the second argument is served by the built-in server; URLs go in app_host","Assert respond_to?(:call) in helpers that accept an app parameter"],"tags":["session","rack","constructor","typeerror","app-host"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"15b5fdb76e972e9623d5af2123ed3755594f9732","analyzedAt":"2026-08-21T16:53:45.588Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}