{"record":{"id":"ee98e5b9ad72e421","repo":"teamcapybara/capybara","slug":"switch-to-window-either-window-or-block-should","errorCode":null,"errorMessage":"`switch_to_window`: either window or block should be provided","messagePattern":"`switch_to_window`: either window or block should be provided","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/capybara/session.rb","lineNumber":514,"sourceCode":"    #\n    # @overload switch_to_window(&block)\n    #   Switches to the first window for which given block returns a value other than false or nil.\n    #   If window that matches block can't be found, the window will be switched back and {Capybara::WindowError} will be raised.\n    #   @example\n    #     window = switch_to_window { title == 'Page title' }\n    #   @raise [Capybara::WindowError]     if no window matches given block\n    # @overload switch_to_window(window)\n    #   @param window [Capybara::Window]   window that should be switched to\n    #   @raise [Capybara::Driver::Base#no_such_window_error] if nonexistent (e.g. closed) window was passed\n    #\n    # @return [Capybara::Window]         window that has been switched to\n    # @raise [Capybara::ScopeError]        if this method is invoked inside {#within} or\n    #   {#within_frame} methods\n    # @raise [ArgumentError]               if both or neither arguments were provided\n    #\n    def switch_to_window(window = nil, **options, &window_locator)\n      raise ArgumentError, '`switch_to_window` can take either a block or a window, not both' if window && window_locator\n      raise ArgumentError, '`switch_to_window`: either window or block should be provided' if !window && !window_locator\n\n      unless scopes.last.nil?\n        raise Capybara::ScopeError, '`switch_to_window` is not supposed to be invoked from ' \\\n                                    '`within` or `within_frame` blocks.'\n      end\n\n      _switch_to_window(window, **options, &window_locator)\n    end\n\n    ##\n    # This method does the following:\n    #\n    # 1. Switches to the given window (it can be located by window instance/lambda/string).\n    # 2. Executes the given block (within window located at previous step).\n    # 3. Switches back (this step will be invoked even if an exception occurs at the second step).\n    #\n    # @overload within_window(window) { do_something }\n    #   @param window [Capybara::Window]       instance of {Capybara::Window} class","sourceCodeStart":496,"sourceCodeEnd":532,"githubUrl":"https://github.com/teamcapybara/capybara/blob/15b5fdb76e972e9623d5af2123ed3755594f9732/lib/capybara/session.rb#L496-L532","documentation":"switch_to_window requires exactly one of a window argument or a locator block; calling it with neither raises ArgumentError immediately. This usually means a variable meant to hold a Window was nil, or the call was written as a bare 'focus the other window' habit from other APIs.","triggerScenarios":"A bare switch_to_window with no argument and no block; switch_to_window(@popup) where @popup was never assigned (nil after a failed window_opened_by, for example).","commonSituations":"Nil window variables after a rescue or failed lookup; stubs/mocks returning nil; team members expecting switch_to_window to focus 'the other window' by default.","solutions":["Provide one: switch_to_window { title == 'Chat' } or switch_to_window(stored_window)","If you wanted the current window, use page.current_window instead of switching","Guard nils from lookups: switch_to_window(win) if win"],"exampleFix":"# before\nswitch_to_window # nothing given\n\n# after\nswitch_to_window { title == 'Chat' }\n# or, with a stored window:\nswitch_to_window(popup_window) if popup_window","handlingStrategy":"validation","validationCode":"raise ArgumentError, 'window or block required' if window.nil? && !block_given?\nswitch_to_window(window, &blk)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Nil-check window variables before switching","Prefer locator blocks in helpers so no nil window can be passed"],"tags":["windows","missing-argument","switch-to-window","nil-guard"],"backgroundTag":"missing-required-argument","analyzedSha":"15b5fdb76e972e9623d5af2123ed3755594f9732","analyzedAt":"2026-08-21T16:53:45.588Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}