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
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
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)

 @overload within_window(window) { do_something }
   @param window [Capybara::Window]       instance of `Capybara::Window` class
     that will be switched to
   @raise [driver#no_such_window_error] if nonexistent (e.g. closed) window was passed
 @overload within_window(proc_or_lambda) { do_something }
   @param lambda [Proc]                  lambda. First window for which lambda
     returns a value other than false or nil will be switched to.
   @example
     within_window(->{ page.title == 'Page title' }) { click_button 'Submit' }
   @raise [Capybara::WindowError]         if no window matching lambda was found

 @raise [Capybara::ScopeError]        if this method is invoked inside `within_frame` method
 @return                              value returned by the block