123456789_123456789_123456789_123456789_123456789_

Class: Capybara::Window

Relationships & Source Files
Inherits: Object
Defined in: lib/capybara/window.rb

Overview

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:

  • window that is current, ::Capybara will make 2 Selenium method invocations (get handle of current window + get size/resize/close).

  • window that is not current, ::Capybara will make 4 Selenium method invocations (get handle of current window + switch to given handle + get size/resize/close + switch to original handle)

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(session, handle) ⇒ Window

This method is for internal use only.
[ GitHub ]

  
# File 'lib/capybara/window.rb', line 30

def initialize(session, handle)
  @session = session
  @driver = session.driver
  @handle = handle
end

Instance Attribute Details

#closed?Boolean (readonly)

Returns:

  • (Boolean)

    whether the window is closed

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 44

def closed?
  !exists?
end

#current?Boolean (readonly)

Returns:

  • (Boolean)

    whether this window is the window in which commands are being executed

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 50

def current?
  @driver.current_window_handle == @handle
rescue @driver.no_such_window_error
  false
end

#exists?Boolean (readonly)

Returns:

  • (Boolean)

    whether the window is not closed

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 38

def exists?
  @driver.window_handles.include?(@handle)
end

#handleString (readonly)

Returns:

  • (String)

    a string that uniquely identifies window within session

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 24

attr_reader :handle

#sessionCapybara::Session (readonly)

Returns:

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 27

attr_reader :session

Instance Method Details

#==(other)

Alias for #eql?.

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 118

alias_method :==, :eql?

#close

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 session.driver.no_such_window_error until another window will be switched to.

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.

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 67

def close
  @driver.close_window(handle)
end

#eql?(other) ⇒ Boolean Also known as: #==

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 115

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

#fullscreen

Fullscreen window.

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

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.

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 111

def fullscreen
  @driver.fullscreen_window(handle)
end

#hash

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 120

def hash
  [@session, @handle].hash
end

#inspect

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 124

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

#maximize

Maximize window.

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

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.

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 100

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

#resize_to(width, height)

Resize window.

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.

Parameters:

  • width (Integer)

    the new window width in pixels

  • height (Integer)

    the new window height in pixels

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 88

def resize_to(width, height)
  wait_for_stable_size { @driver.resize_window_to(handle, width, height) }
end

#sizeArray<(Integer, Integer)>

Get window size.

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.

Returns:

  • (Array<(Integer, Integer)>)

    an array with width and height

[ GitHub ]

  
# File 'lib/capybara/window.rb', line 77

def size
  @driver.window_size(handle)
end

#wait_for_stable_size(seconds = session.config.default_max_wait_time) (private)

[ GitHub ]

  
# 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