Class: Watir::Window

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

Instance Method Summary collapse

Methods included from EventuallyPresent

#wait_until_present, #wait_while_present, #when_present

Constructor Details

#initialize(driver, selector) ⇒ Window

Returns a new instance of Window.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/watir-webdriver/window.rb', line 5

def initialize(driver, selector)
  @driver   = driver
  @selector = selector

  if selector.empty?
    @handle = driver.window_handle
  elsif selector.has_key? :handle
    @handle = selector.delete :handle
  else
    unless selector.keys.all? { |k| [:title, :url, :index].include? k }
      raise ArgumentError, "invalid window selector: #{selector.inspect}"
    end
  end
end

Instance Method Details

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

Returns true if two windows are equal.

Examples:

browser.window(:index => 1) == browser.window(:index => 2)

Parameters:



133
134
135
136
137
# File 'lib/watir-webdriver/window.rb', line 133

def ==(other)
  return false unless other.kind_of?(self.class)

  handle == other.handle
end

#closeObject

CLoses window.



160
161
162
# File 'lib/watir-webdriver/window.rb', line 160

def close
  use { @driver.close }
end

#current?Boolean

Returns true if window is current.

Examples:

browser.window.current?
#=> true

Returns:

  • (Boolean)


152
153
154
# File 'lib/watir-webdriver/window.rb', line 152

def current?
  @driver.window_handle == handle
end

#exists?Boolean

Returns true if window exists.

Returns:

  • (Boolean)


105
106
107
108
109
110
# File 'lib/watir-webdriver/window.rb', line 105

def exists?
  handle
  true
rescue Exception::NoMatchingWindowFoundException
  false
end

#hashObject



140
141
142
# File 'lib/watir-webdriver/window.rb', line 140

def hash
  handle.hash ^ self.class.hash
end

#inspectObject



20
21
22
# File 'lib/watir-webdriver/window.rb', line 20

def inspect
  '#<%s:0x%x located=%s>' % [self.class, hash*2, !!@handle]
end

#maximizeObject

Maximizes window.

Examples:

browser.window.maximize


95
96
97
# File 'lib/watir-webdriver/window.rb', line 95

def maximize
  use { @driver.manage.window.maximize }
end

#move_to(x, y) ⇒ Object

Moves window to given x and y coordinates.

Examples:

browser.window.move_to 300, 200

Parameters:

  • x (Fixnum)
  • y (Fixnum)


81
82
83
84
85
86
# File 'lib/watir-webdriver/window.rb', line 81

def move_to(x, y)
  point = Selenium::WebDriver::Point.new(Integer(x), Integer(y))
  use { @driver.manage.window.position = point }

  point
end

#positionObject

Returns window position.

Examples:

position = browser.window.position
[position.x, position.y] #=> [92, 76]


47
48
49
50
51
52
# File 'lib/watir-webdriver/window.rb', line 47

def position
  pos = nil
  use { pos = @driver.manage.window.position }

  pos
end

#present?Boolean

Returns true if window is present.

Returns:

  • (Boolean)


118
119
120
121
122
# File 'lib/watir-webdriver/window.rb', line 118

def present?
  @handle = nil # relocate

  exists?
end

#resize_to(width, height) ⇒ Object

Resizes window to given width and height.

Examples:

browser.window.resize_to 1600, 1200

Parameters:

  • width (Fixnum)
  • height (Fixnum)


64
65
66
67
68
69
# File 'lib/watir-webdriver/window.rb', line 64

def resize_to(width, height)
  dimension = Selenium::WebDriver::Dimension.new(Integer(width), Integer(height))
  use { @driver.manage.window.size = dimension }

  dimension
end

#sizeObject

Returns window size.

Examples:

size = browser.window.size
[size.width, size.height] #=> [1600, 1200]


32
33
34
35
36
37
# File 'lib/watir-webdriver/window.rb', line 32

def size
  size = nil
  use { size = @driver.manage.window.size }

  size
end

#titleString

Returns window title.

Returns:

  • (String)


170
171
172
173
174
175
# File 'lib/watir-webdriver/window.rb', line 170

def title
  title = nil
  use { title = @driver.title }

  title
end

#urlString

Returns window URL.

Returns:

  • (String)


183
184
185
186
187
188
# File 'lib/watir-webdriver/window.rb', line 183

def url
  url = nil
  use { url = @driver.current_url }

  url
end

#use(&blk) ⇒ Object

Switches to given window and executes block, then switches back.

Examples:

browser.window(:title => "2nd window").use do
  browser.button(:id => "close").click
end


199
200
201
202
# File 'lib/watir-webdriver/window.rb', line 199

def use(&blk)
  @driver.switch_to.window(handle, &blk)
  self
end