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 an exception occurs at the second step).

Overloads:

  • #within_window(window) ⇒ Object

    Parameters:

    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)

      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:



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/capybara/session.rb', line 544

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