Method: Capybara::Session#within_window

Defined in:
lib/capybara/session.rb

#within_window(window) ⇒ Object #within_window(proc_or_lambda) ⇒ Object #within_window(string) ⇒ 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 unexistent (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:

  • #within_window(string) ⇒ Object
    Deprecated.

    Pass window or lambda instead

    Parameters:

    • handle, (String)

      name, url or title of the window

Returns:

  • value returned by the block

Raises:



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/capybara/session.rb', line 531

def within_window(window_or_handle)
  if window_or_handle.instance_of?(Capybara::Window)
    original = current_window
    scopes << nil
    begin
      _switch_to_window(window_or_handle) unless original == window_or_handle
      begin
        yield
      ensure
        _switch_to_window(original) unless original == window_or_handle
      end
    ensure
      scopes.pop
    end
  elsif window_or_handle.is_a?(Proc)
    original = current_window
    scopes << nil
    begin
      _switch_to_window { window_or_handle.call }
      begin
        yield
      ensure
        _switch_to_window(original)
      end
    ensure
      scopes.pop
    end
  else
    offending_line = caller.first
    file_line = offending_line.match(/^(.+?):(\d+)/)[0]
    warn "DEPRECATION WARNING: Passing string argument to #within_window is deprecated. "\
         "Pass window object or lambda. (called from #{file_line})"
    begin
      scopes << nil
      driver.within_window(window_or_handle) { yield }
    ensure
      scopes.pop
    end
  end
end