class Capybara::Window


(get handle of current window + switch to given handle + get size/resize/close + switch to original handle)
* window that is not current, Capybara will make 4 Selenium method invocations
(get handle of current window + get size/resize/close).
* window that is current, Capybara will make 2 Selenium method invocations
current window. So if you invoke such method for:
Note that some drivers (e.g. Selenium) support getting size of/resizing/closing only
* {Capybara::Session#switch_to_window}
* {Capybara::Session#window_opened_by}
* {Capybara::Session#current_window}
* {Capybara::Session#windows}
You can get an instance of the class by calling either of:
The Window class represents a browser window.
#

def close


current window shouldn remain the same as it was before calling this method.
If this method was called for window that is not current, then after calling this method
@!macro about_current

`session.driver.no_such_window_error` until another window will be switched to.
future invocations of other Capybara methods should raise
If this method was called for window that is current, then after calling this method

Close window.
#
def close
  @driver.close_window(handle)
end

def closed?

Returns:
  • (Boolean) - whether the window is closed
def closed?
  !exists?
end

def current?

Returns:
  • (Boolean) - whether this window is the window in which commands are being executed
def current?
  @driver.current_window_handle == @handle
rescue @driver.no_such_window_error
  false
end

def eql?(other)

def eql?(other)
  other.is_a?(self.class) && @session == other.session && @handle == other.handle
end

def exists?

Returns:
  • (Boolean) - whether the window is not closed
def exists?
  @driver.window_handles.include?(@handle)
end

def fullscreen


@macro about_current

If a particular driver doesn't have concept of fullscreen it may not support this method.

Fullscreen window.
#
def fullscreen
  @driver.fullscreen_window(handle)
end

def hash

def hash
  @session.hash ^ @handle.hash
end

def initialize(session, handle)

Other tags:
    Api: - private
def initialize(session, handle)
  @session = session
  @driver = session.driver
  @handle = handle
end

def inspect

def inspect
  "#<Window @handle=#{@handle.inspect}>"
end

def maximize


@macro about_current

may not support this method.
If a particular driver (e.g. headless driver) doesn't have concept of maximizing it

Maximize window.
#
def maximize
  wait_for_stable_size { @driver.maximize_window(handle) }
end

def raise_unless_current(what)

def raise_unless_current(what)
  raise Capybara::WindowError, "#{what} not current window is not possible." unless current?
end

def resize_to(width, height)

Parameters:
  • height (String) -- the new window height in pixels
  • width (String) -- the new window width in pixels
def resize_to(width, height)
  wait_for_stable_size { @driver.resize_window_to(handle, width, height) }
end

def size

Returns:
  • (Array<(Integer, Integer)>) - an array with width and height
def size
  @driver.window_size(handle)
end

def wait_for_stable_size(seconds = session.config.default_max_wait_time)

def wait_for_stable_size(seconds = session.config.default_max_wait_time)
  res = yield if block_given?
  timer = Capybara::Helpers.timer(expire_in: seconds)
  loop do
    prev_size = size
    sleep 0.05
    return res if prev_size == size
    break if timer.expired?
  end
  raise Capybara::WindowError, "Window size not stable within #{seconds} seconds."
end