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 = current_window
  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 => 0) == browser.window(:index => 1)
#=> false

Parameters:



125
126
127
128
129
# File 'lib/watir-webdriver/window.rb', line 125

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

  handle == other.handle
end

#closeObject

Closes window.



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

def close
  use { @driver.close }
end

#current?Boolean

Returns true if window is current.

Examples:

browser.window.current?
#=> true

Returns:

  • (Boolean)


144
145
146
# File 'lib/watir-webdriver/window.rb', line 144

def current?
  current_window == handle
end

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

Returns true if window exists.

Returns:

  • (Boolean)


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

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

#hashObject



132
133
134
# File 'lib/watir-webdriver/window.rb', line 132

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

#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)


162
163
164
165
166
167
# File 'lib/watir-webdriver/window.rb', line 162

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

  title
end

#urlString

Returns window URL.

Returns:

  • (String)


175
176
177
178
179
180
# File 'lib/watir-webdriver/window.rb', line 175

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 => "closeable window").use do
  browser.a(:id => "close").click
end


191
192
193
194
195
# File 'lib/watir-webdriver/window.rb', line 191

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