class Capybara::Window

The {Window} class represents a browser window.

You can get an instance of the class by calling any of:

Note that some drivers (e.g. Selenium) support getting size of/resizing/closing only current window. So if you invoke such method for:

Attributes

handle[R]

@return [String] a string that uniquely identifies window within session

session[R]

@return [Capybara::Session] session that this window belongs to

Public Class Methods

new(session, handle) click to toggle source

@api private

# File lib/capybara/window.rb, line 30
def initialize(session, handle)
  @session = session
  @driver = session.driver
  @handle = handle
end

Public Instance Methods

==(other)
Alias for: eql?
close() click to toggle source

Close window.

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

@!macro about_current

If this method was called for window that is not current, then after calling this method
current window should remain the same as it was before calling this method.
# File lib/capybara/window.rb, line 67
def close
  @driver.close_window(handle)
end
closed?() click to toggle source

@return [Boolean] whether the window is closed

# File lib/capybara/window.rb, line 44
def closed?
  !exists?
end
current?() click to toggle source

@return [Boolean] whether this window is the window in which commands are being executed

# File lib/capybara/window.rb, line 50
def current?
  @driver.current_window_handle == @handle
rescue @driver.no_such_window_error
  false
end
eql?(other) click to toggle source
# File lib/capybara/window.rb, line 115
def eql?(other)
  other.is_a?(self.class) && @session == other.session && @handle == other.handle
end
Also aliased as: ==
exists?() click to toggle source

@return [Boolean] whether the window is not closed

# File lib/capybara/window.rb, line 38
def exists?
  @driver.window_handles.include?(@handle)
end
fullscreen() click to toggle source

Fullscreen window.

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

@macro about_current

# File lib/capybara/window.rb, line 111
def fullscreen
  @driver.fullscreen_window(handle)
end
hash() click to toggle source
# File lib/capybara/window.rb, line 120
def hash
  [@session, @handle].hash
end
inspect() click to toggle source
# File lib/capybara/window.rb, line 124
def inspect
  "#<Window @handle=#{@handle.inspect}>"
end
maximize() click to toggle source

Maximize window.

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

@macro about_current

# File lib/capybara/window.rb, line 100
def maximize
  wait_for_stable_size { @driver.maximize_window(handle) }
end
resize_to(width, height) click to toggle source

Resize window.

@macro about_current @param width [Integer] the new window width in pixels @param height [Integer] the new window height in pixels

# File lib/capybara/window.rb, line 88
def resize_to(width, height)
  wait_for_stable_size { @driver.resize_window_to(handle, width, height) }
end
size() click to toggle source

Get window size.

@macro about_current @return [Array<(Integer, Integer)>] an array with width and height

# File lib/capybara/window.rb, line 77
def size
  @driver.window_size(handle)
end

Private Instance Methods

wait_for_stable_size(seconds = session.config.default_max_wait_time) { || ... } click to toggle source
# File lib/capybara/window.rb, line 130
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.025
    return res if prev_size == size
    break if timer.expired?
  end
  raise Capybara::WindowError, "Window size not stable within #{seconds} seconds."
end