{"record":{"id":"adbef161a4a4e688","repo":"teamcapybara/capybara","slug":"switch-to-window-can-take-either-a-block-or-a-wi","errorCode":null,"errorMessage":"`switch_to_window` can take either a block or a window, not both","messagePattern":"`switch_to_window` can take either a block or a window, not both","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/capybara/session.rb","lineNumber":513,"sourceCode":"    # Switch to the given window.\n    #\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 }","sourceCodeStart":495,"sourceCodeEnd":531,"githubUrl":"https://github.com/teamcapybara/capybara/blob/15b5fdb76e972e9623d5af2123ed3755594f9732/lib/capybara/session.rb#L495-L531","documentation":"switch_to_window supports two addressing modes - a Capybara::Window object or a block evaluated per window - and explicitly rejects using both at once with ArgumentError before any driver work happens.","triggerScenarios":"switch_to_window(page.windows.last) { title == 'Chat' } - a window argument plus a locator block in the same call; helper methods that always pass a block while callers also supply a window argument.","commonSituations":"Refactoring between the two addressing modes and leaving both in place; page-object helpers with a default window parameter invoked together with a block.","solutions":["Pick one mode: switch_to_window(window) to switch to a known window object","Or switch_to_window { title == 'Chat' } to find and switch by predicate","Audit helper signatures so the window argument is dropped when a block is given"],"exampleFix":"# before\nswitch_to_window(chat_window) { title == 'Chat' }\n\n# after\nswitch_to_window(chat_window)\n# or\nswitch_to_window { title == 'Chat' }","handlingStrategy":"type-guard","validationCode":"raise ArgumentError, 'window or block, not both' if window && block_given?\nswitch_to_window(window)","typeGuard":"def window_arg?(obj)\n  obj.is_a?(Capybara::Window)\nend\n\nswitch_to_window(w) if window_arg?(w)","tryCatchPattern":null,"preventionTips":["Standardize on one addressing mode per codebase","Store Window objects from window_opened_by so locator blocks are unnecessary"],"tags":["windows","argument-validation","switch-to-window","api-misuse"],"backgroundTag":"invalid-argument-combination","analyzedSha":"15b5fdb76e972e9623d5af2123ed3755594f9732","analyzedAt":"2026-08-21T16:53:45.588Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}