Class: Watir::Window

Inherits:
Object
  • Object
show all
Includes:
ElementExtensions
Defined in:
lib/watir-classic/window.rb

Overview

Returned by Browser#window.

Instance Method Summary collapse

Methods included from ElementExtensions

#wait_until_present, #wait_while_present, #when_present

Constructor Details

#initialize(main_browser, locators, browser = nil) ⇒ Window

Returns a new instance of Window.



22
23
24
25
26
27
28
29
30
31
# File 'lib/watir-classic/window.rb', line 22

def initialize(main_browser, locators, browser=nil)
  valid_locators = [:title, :url, :hwnd, :index]
  locators.each_pair do |k, v| 
    raise ArgumentError, "Valid locators are #{valid_locators.join(", ")}" unless valid_locators.include?(k)
  end
  @main_browser = main_browser
  self.class.__main_ie = main_browser.ie
  @locators = locators
  @browser = browser
end

Instance Method Details

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



89
90
91
# File 'lib/watir-classic/window.rb', line 89

def ==(other)
  browser.hwnd == other.hwnd && browser.html == other.browser.html
end

#browserBrowser

Returns browser of the window.

Returns:

  • (Browser)

    browser of the window.



44
45
46
47
48
# File 'lib/watir-classic/window.rb', line 44

def browser
  @browser ||= begin
                Browser.find(@locators.keys.first, @locators.values.first)
               end
end

#closeObject

Close the Watir::Window.



41
# File 'lib/watir-classic/window.rb', line 41

wrap :url, :title, :hwnd, :close

#current?Boolean

Returns true when Watir::Window is the active Browser instance, false otherwise.

Returns:



79
80
81
# File 'lib/watir-classic/window.rb', line 79

def current?
  @main_browser.hwnd == browser.hwnd && @main_browser.html == browser.html
end

#hwndFixnum

Returns handle of the Watir::Window.

Returns:



41
# File 'lib/watir-classic/window.rb', line 41

wrap :url, :title, :hwnd, :close

#present?Boolean

Returns true when Watir::Window browser exists, false otherwise.

Returns:

  • (Boolean)

    true when Watir::Window browser exists, false otherwise.



84
85
86
87
# File 'lib/watir-classic/window.rb', line 84

def present?
  @browser = nil
  browser && browser.exists?
end

#titleString

Returns title of the Watir::Window.

Returns:



41
# File 'lib/watir-classic/window.rb', line 41

wrap :url, :title, :hwnd, :close

#urlString

Returns url of the Watir::Window.

Returns:



41
# File 'lib/watir-classic/window.rb', line 41

wrap :url, :title, :hwnd, :close

#use { ... } ⇒ Object

Use the window.

Examples:

Change current window:

browser.window(:title => /foo/).use
browser.title # => "foo"

Execute code in the other window:

browser.window(:title => /foo/).use do
  browser.title # => "foo"
end
browser.title # => "current window title"

Yields:



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/watir-classic/window.rb', line 63

def use(&blk)
  @main_browser.ie = browser.ie
  if blk
    begin
      blk.call
    ensure
      @main_browser.ie = self.class.__main_ie
      # try to find some existing IE when needed
      @main_browser.ie = Browser._find(:index, 0) unless @main_browser.exists?
    end
  end
  self
end