class Watir::WindowCollection

Public Class Methods

new(browser, selector = {}) click to toggle source
# File lib/watir/window_collection.rb, line 6
def initialize(browser, selector = {})
  unless selector.keys.all? { |k| %i[title url element].include? k }
    raise ArgumentError, "invalid window selector: #{selector.inspect}"
  end

  @browser = browser
  @selector = selector
end

Public Instance Methods

==(other) click to toggle source
# File lib/watir/window_collection.rb, line 72
def ==(other)
  window_list == other.send(:window_list)
end
Also aliased as: eql?
[](value) click to toggle source

Get the window at the given index or range.

@note windows in a collection are not ordered so this is not reliably @deprecated use Browser#switch_window or a better Window locator @param [Integer, Range] value Index (0-based) or Range of desired window(s) @return [Watir::Window] Returns an instance of a Watir::Window

# File lib/watir/window_collection.rb, line 63
def [](value)
  old = 'using indexing with windows'
  new = 'Browser#switch_window or Browser#window with :title, :url or :element selectors'
  reference = 'http://watir.com/window_indexes'
  Watir.logger.deprecate old, new, reference: reference, ids: [:window_index]

  window_list[value]
end
each(&blk) click to toggle source

Yields each window in collection.

@yieldparam [Watir::Window]

# File lib/watir/window_collection.rb, line 21
def each(&blk)
  reset!
  window_list.each(&blk)
end
eql?(other)
Alias for: ==
first() click to toggle source

First window of the collection

@note windows in a collection are not ordered so this is not reliably @deprecated use Browser#switch_window or a better Window locator @return [Watir::Window] Returns an instance of a Watir::Window

# File lib/watir/window_collection.rb, line 38
def first
  self[0]
end
last() click to toggle source

Last window of the collection

@note windows in a collection are not ordered so this is not reliably @deprecated use Browser#switch_window or a better Window locator @return [Watir::Window] Returns an instance of a Watir::Window

# File lib/watir/window_collection.rb, line 50
def last
  self[-1]
end
reset!() click to toggle source
# File lib/watir/window_collection.rb, line 77
def reset!
  @window_list = nil
end
to_a() click to toggle source
# File lib/watir/window_collection.rb, line 81
def to_a
  old = 'WindowCollection#to_a to interact with indexed windows'
  new = 'Enumerable methods to iterate over windows'
  reference = 'http://watir.com/window_indexes'
  Watir.logger.deprecate old, new, reference: reference, ids: [:window_index]

  window_list
end

Private Instance Methods

matches?(handle) click to toggle source

NOTE: This is the exact same code from `Window#matches?` TODO: Move this code into a separate WindowLocator class

# File lib/watir/window_collection.rb, line 101
def matches?(handle)
  @selector.empty? || @browser.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
window_list() click to toggle source
# File lib/watir/window_collection.rb, line 92
def window_list
  @window_list ||= begin
    handles = @browser.driver.window_handles.select { |wh| matches?(wh) }
    handles.map { |wh| Window.new(@browser, handle: wh) }
  end
end