Method: Capybara::Session#switch_to_window

Defined in:
lib/capybara/session.rb

#switch_to_window(&block) ⇒ Capybara::Window #switch_to_window(window) ⇒ Capybara::Window

Returns window that has been switched to

Overloads:

  • #switch_to_window(&block) ⇒ Capybara::Window

    Switches to the first window for which given block returns a value other than false or nil. If window that matches block can't be found, the window will be switched back and `WindowError` will be raised.

    Examples:

    window = switch_to_window { title == 'Page title' }

    Raises:

  • #switch_to_window(window) ⇒ Capybara::Window

    Raises:

Raises:

  • (Capybara::ScopeError)

    if this method is invoked inside `within` or `within_frame` methods

  • (ArgumentError)

    if both or neither arguments were provided



471
472
473
474
475
476
477
478
479
480
481
# File 'lib/capybara/session.rb', line 471

def switch_to_window(window = nil, **options, &window_locator)
  raise ArgumentError, '`switch_to_window` can take either a block or a window, not both' if window && block_given?
  raise ArgumentError, '`switch_to_window`: either window or block should be provided' if !window && !block_given?

  unless scopes.last.nil?
    raise Capybara::ScopeError, '`switch_to_window` is not supposed to be invoked from '\
                                '`within` or `within_frame` blocks.'
  end

  _switch_to_window(window, options, &window_locator)
end