class Watir::Window
Attributes
browser[R]
Public Class Methods
new(browser, selector = {})
click to toggle source
# File lib/watir/window.rb, line 9 def initialize(browser, selector = {}) @browser = browser @driver = browser.driver @selector = selector if selector.empty? @handle = current_window elsif selector.key? :handle @handle = selector.delete :handle else return if selector.keys.all? { |k| %i[title url index element].include? k } raise ArgumentError, "invalid window selector: #{selector_string}" end end
Public Instance Methods
==(other)
click to toggle source
Returns true if two windows are equal.
@example
browser.window(index: 0) == browser.window(index: 1) #=> false
@param [Window] other
# File lib/watir/window.rb, line 122 def ==(other) return false unless other.is_a?(self.class) handle == other.handle end
Also aliased as: eql?
close()
click to toggle source
Closes window.
# File lib/watir/window.rb, line 149 def close @browser.original_window = nil if self == @browser.original_window use { @driver.close } end
current?()
click to toggle source
Returns true if window is current.
@example
browser.window.current? #=> true
# File lib/watir/window.rb, line 141 def current? current_window == handle end
exists?()
click to toggle source
Returns true if window exists.
@return [Boolean]
# File lib/watir/window.rb, line 102 def exists? assert_exists true rescue NoMatchingWindowFoundException false end
handle()
click to toggle source
# File lib/watir/window.rb, line 200 def handle @handle ||= locate end
hash()
click to toggle source
# File lib/watir/window.rb, line 129 def hash handle.hash ^ self.class.hash end
inspect()
click to toggle source
# File lib/watir/window.rb, line 25 def inspect format('#<%s:0x%x located=%s>', self.class, hash * 2, !!@handle) end
maximize()
click to toggle source
Maximizes window.
@example
browser.window.maximize
# File lib/watir/window.rb, line 92 def maximize use { @driver.manage.window.maximize } end
move_to(x_coord, y_coord)
click to toggle source
Moves window to given x and y coordinates.
@example
browser.window.move_to 300, 200
@param [Integer] x_coord @param [Integer] y_coord
# File lib/watir/window.rb, line 79 def move_to(x_coord, y_coord) Selenium::WebDriver::Point.new(Integer(x_coord), Integer(y_coord)).tap do |point| use { @driver.manage.window.position = point } end end
position()
click to toggle source
Returns window position.
@example
position = browser.window.position [position.x, position.y] #=> [92, 76]
# File lib/watir/window.rb, line 49 def position use { return @driver.manage.window.position } end
resize_to(width, height)
click to toggle source
Resizes window to given width and height.
@example
browser.window.resize_to 1600, 1200
@param [Integer] width @param [Integer] height
# File lib/watir/window.rb, line 63 def resize_to(width, height) Selenium::WebDriver::Dimension.new(Integer(width), Integer(height)).tap do |dimension| use { @driver.manage.window.size = dimension } end end
selector_string()
click to toggle source
@api private
Referenced in EventuallyPresent
# File lib/watir/window.rb, line 196 def selector_string @selector.inspect end
size()
click to toggle source
Returns window size.
@example
size = browser.window.size [size.width, size.height] #=> [1600, 1200]
# File lib/watir/window.rb, line 37 def size use { return @driver.manage.window.size } end
title()
click to toggle source
Returns window title.
@return [String]
# File lib/watir/window.rb, line 160 def title use { return @driver.title } end
url()
click to toggle source
Returns window URL.
@return [String]
# File lib/watir/window.rb, line 170 def url use { return @driver.current_url } end
use(&blk)
click to toggle source
Switches to given window and executes block, then switches back.
@example
browser.window(title: "closeable window").use do browser.a(id: "close").click end
# File lib/watir/window.rb, line 183 def use(&blk) @browser.original_window ||= current_window wait_for_exists @driver.switch_to.window(handle, &blk) self end
Private Instance Methods
assert_exists()
click to toggle source
# File lib/watir/window.rb, line 219 def assert_exists return if @driver.window_handles.include?(handle) raise(NoMatchingWindowFoundException, selector_string) end
current_window()
click to toggle source
return a handle to the currently active window if it is still open; otherwise nil
# File lib/watir/window.rb, line 226 def current_window @driver.window_handle rescue Selenium::WebDriver::Error::NoSuchWindowError nil end
locate()
click to toggle source
# File lib/watir/window.rb, line 206 def locate if @selector.empty? nil elsif @selector.key?(:index) Watir.logger.deprecate 'Using :index as a selector for Window', ':title or :url or :element', reference: 'http://watir.com/guides/windows/#locating-by-index-is-no-longer-supported', ids: [:window_index] @driver.window_handles[Integer(@selector[:index])] else @driver.window_handles.find { |wh| matches?(wh) } end end
matches?(handle)
click to toggle source
# File lib/watir/window.rb, line 232 def matches?(handle) @driver.switch_to.window(handle) do matches_title = @selector[:title].nil? || @browser.title =~ /#{@selector[:title]}/ matches_url = @selector[:url].nil? || @browser.url =~ /#{@selector[:url]}/ matches_element = @selector[:element].nil? || @selector[:element].exists? matches_title && matches_url && matches_element end rescue Selenium::WebDriver::Error::NoSuchWindowError # the window may disappear while we're iterating. false end
wait_for_exists()
click to toggle source
# File lib/watir/window.rb, line 245 def wait_for_exists return assert_exists unless Watir.relaxed_locate? begin wait_until(&:exists?) rescue Wait::TimeoutError raise NoMatchingWindowFoundException, selector_string end end