Class: Watir::Window

Inherits:
Object
  • Object
show all
Includes:
Exception, Waitable
Defined in:
lib/watir/window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Waitable

#wait_until, #wait_while

Constructor Details

#initialize(browser, selector = {}) ⇒ Window

Returns a new instance of Window.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/watir/window.rb', line 10

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
    types = %i[title url element]
    return if selector.keys.all? { |k| types.include? k }

    raise ArgumentError, "invalid window selector: #{selector_string}"
  end
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.


8
9
10
# File 'lib/watir/window.rb', line 8

def browser
  @browser
end

Instance Method Details

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

Returns true if two windows are equal.

Examples:

browser.window(title: /window_switching/) == browser.window(/closeable/)
#=> false

Parameters:


147
148
149
150
151
# File 'lib/watir/window.rb', line 147

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

  handle == other.handle
end

#closeObject

Closes window.


174
175
176
177
# File 'lib/watir/window.rb', line 174

def close
  @browser.original_window = nil if self == @browser.original_window
  use { @driver.close }
end

#current?Boolean

Returns true if window is current.

Examples:

browser.window.current?
#=> true

Returns:

  • (Boolean)

166
167
168
# File 'lib/watir/window.rb', line 166

def current?
  current_window == handle
end

#exists?Boolean Also known as: present?, exist?

Returns true if window exists.

Returns:

  • (Boolean)

127
128
129
130
131
132
# File 'lib/watir/window.rb', line 127

def exists?
  assert_exists
  true
rescue NoMatchingWindowFoundException
  false
end

#full_screenObject

Make window full screen.

Examples:

browser.window.full_screen

117
118
119
# File 'lib/watir/window.rb', line 117

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

#handleObject


234
235
236
# File 'lib/watir/window.rb', line 234

def handle
  @handle ||= locate
end

#hashObject


154
155
156
# File 'lib/watir/window.rb', line 154

def hash
  [handle, self.class].hash
end

#inspectObject


27
28
29
30
# File 'lib/watir/window.rb', line 27

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

#maximizeObject

Maximizes window.

Examples:

browser.window.maximize

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

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

#minimizeObject

Minimize window.

Examples:

browser.window.minimize

106
107
108
# File 'lib/watir/window.rb', line 106

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

#move_to(x_coord, y_coord) ⇒ Object

Moves window to given x and y coordinates.

Examples:

browser.window.move_to 300, 200

Parameters:

  • x_coord (Integer)
  • y_coord (Integer)

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

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

#positionObject

Returns window position.

Examples:

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

52
53
54
# File 'lib/watir/window.rb', line 52

def position
  use { return @driver.manage.window.position }
end

#resize_to(width, height) ⇒ Object

Resizes window to given width and height.

Examples:

browser.window.resize_to 1600, 1200

Parameters:

  • width (Integer)
  • height (Integer)

66
67
68
69
70
# File 'lib/watir/window.rb', line 66

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_stringObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


230
231
232
# File 'lib/watir/window.rb', line 230

def selector_string
  @selector.inspect
end

#sizeObject

Returns window size.

Examples:

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

40
41
42
# File 'lib/watir/window.rb', line 40

def size
  use { return @driver.manage.window.size }
end

#titleString

Returns window title.

Returns:

  • (String)

185
186
187
# File 'lib/watir/window.rb', line 185

def title
  use { return @driver.title }
end

#urlString

Returns window URL.

Returns:

  • (String)

195
196
197
# File 'lib/watir/window.rb', line 195

def url
  use { return @driver.current_url }
end

#useObject

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

Examples:

browser.window(title: "closeable window").use do
  browser.a(id: "close").click
end

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/watir/window.rb', line 208

def use
  wait_for_exists
  cache_current = current_window
  @browser.original_window ||= cache_current
  restore_to = unless cache_current == handle
                 @driver.switch_to.window(handle)
                 cache_current
               end
  if block_given?
    begin
      yield
    ensure
      @driver.switch_to.window(restore_to) if restore_to
    end
  end
  self
end