Class: Watir::Window

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Waitable

#wait_until, #wait_until_present, #wait_while, #wait_while_present

Methods included from EventuallyPresent

#when_enabled, #when_present

Constructor Details

#initialize(browser, selector) ⇒ Window

Returns a new instance of Window.


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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].include? k }

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

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser


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

def browser
  @browser
end

Instance Method Details

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

Returns true if two windows are equal.

Examples:

browser.window(index: 0) == browser.window(index: 1)#=> false

Parameters:


122
123
124
125
126
# File 'lib/watir/window.rb', line 122

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

  handle == other.handle
end

#closeObject

Closes window.


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

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)

141
142
143
# File 'lib/watir/window.rb', line 141

def current?
  current_window == handle
end

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

Returns true if window exists.

Returns:

  • (Boolean)

102
103
104
105
106
107
# File 'lib/watir/window.rb', line 102

def exists?
  assert_exists
  true
rescue NoMatchingWindowFoundException
  false
end

#handleObject


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

def handle
  @handle ||= locate
end

#hashObject


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

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

#inspectObject


25
26
27
# File 'lib/watir/window.rb', line 25

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

#maximizeObject

Maximizes window.

Examples:

browser.window.maximize

92
93
94
# File 'lib/watir/window.rb', line 92

def maximize
  use { @driver.manage.window.maximize }
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)

79
80
81
82
83
# 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

#positionObject

Returns window position.

Examples:

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

49
50
51
# File 'lib/watir/window.rb', line 49

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)

63
64
65
66
67
# 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_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.

Referenced in EventuallyPresent


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

def selector_string
  @selector.inspect
end

#sizeObject

Returns window size.

Examples:

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

37
38
39
# File 'lib/watir/window.rb', line 37

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

#titleString

Returns window title.

Returns:

  • (String)

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

def title
  use { return @driver.title }
end

#urlString

Returns window URL.

Returns:

  • (String)

170
171
172
# File 'lib/watir/window.rb', line 170

def url
  use { return @driver.current_url }
end

#use(&blk) ⇒ Object

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

Examples:

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

183
184
185
186
187
188
# 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