Method: Capybara::Session#within_window

Defined in:
lib/capybara/session.rb

#within_window(window) ⇒ Object #within_window(proc_or_lambda) ⇒ Object

This method does the following:

  1. Switches to the given window (it can be located by window instance/lambda/string).

  2. Executes the given block (within window located at previous step).

  3. Switches back (this step will be invoked even if exception will happen at second step)

Overloads:

  • #within_window(window) ⇒ Object

    Parameters:

    • window (Capybara::Window)

      instance of `Capybara::Window` class that will be switched to

    Raises:

    • (driver#no_such_window_error)

      if nonexistent (e.g. closed) window was passed

  • #within_window(proc_or_lambda) ⇒ Object

    Examples:

    within_window(->{ page.title == 'Page title' }) { click_button 'Submit' }

    Parameters:

    • lambda (Proc)

      lambda. First window for which lambda returns a value other than false or nil will be switched to.

    Raises:

Returns:

  • value returned by the block

Raises:



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/capybara/session.rb', line 505

def within_window(window_or_proc)
  original = current_window
  scopes << nil
  begin
    case window_or_proc
    when Capybara::Window
      _switch_to_window(window_or_proc) unless original == window_or_proc
    when Proc
      _switch_to_window { window_or_proc.call }
    else
      raise ArgumentError("`#within_window` requires a `Capybara::Window` instance or a lambda")
    end

    begin
      yield if block_given?
    ensure
      _switch_to_window(original) unless original == window_or_proc
    end
  ensure
    scopes.pop
  end
end