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 2Selenium
method invocations (get handle of current window + get size/resize/close). -
window that is not current,
::Capybara
will make 4Selenium
method invocations (get handle of current window + switch to given handle + get size/resize/close + switch to original handle)
Class Method Summary
- .new(session, handle) ⇒ Window constructor Internal use only Internal use only
Instance Attribute Summary
- #closed? ⇒ Boolean readonly
- #current? ⇒ Boolean readonly
- #exists? ⇒ Boolean readonly
- #handle ⇒ String readonly
- #session ⇒ Capybara::Session readonly
Instance Method Summary
-
#==(other)
Alias for #eql?.
-
#close
Close window.
- #eql?(other) ⇒ Boolean (also: #==)
-
#fullscreen
Fullscreen window.
- #hash
- #inspect
-
#maximize
Maximize window.
-
#resize_to(width, height)
Resize window.
-
#size ⇒ Array<(Integer, Integer)>
Get window size.
- #wait_for_stable_size(seconds = session.config.default_max_wait_time) private
Constructor Details
.new(session, handle) ⇒ Window
Instance Attribute Details
#closed? ⇒ Boolean
(readonly)
# File 'lib/capybara/window.rb', line 44
def closed? !exists? end
#current? ⇒ Boolean
(readonly)
# 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)
# File 'lib/capybara/window.rb', line 38
def exists? @driver.window_handles.include?(@handle) end
#handle ⇒ String
(readonly)
# File 'lib/capybara/window.rb', line 24
attr_reader :handle
#session ⇒ Capybara::Session (readonly)
# File 'lib/capybara/window.rb', line 27
attr_reader :session
Instance Method Details
#==(other)
Alias for #eql?.
# 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.
# File 'lib/capybara/window.rb', line 67
def close @driver.close_window(handle) end
#eql?(other) ⇒ Boolean
Also known as: #==
#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.
# 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.
# 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.
# 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 ⇒ Array
<(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.
# 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)
# 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