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

- (Window) initialize(driver, selector)



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

- (Object) ==(other) Also known as: eql?

Returns true if two windows are equal.

Examples:

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


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

- (Object) close

CLoses window.



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

def close
  use { @driver.close }
end

- (Boolean) current?

Returns true if window is current.

Examples:

browser.window.current?
#=> true


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

def current?
  @driver.window_handle == handle
end

- (Boolean) exists?

Returns true if window exists.



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

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

- (Object) hash



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

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

- (Object) inspect



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

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

- (Object) maximize

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

- (Object) move_to(x, y)

Moves window to given x and y coordinates.

Examples:

browser.window.move_to 300, 200


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

- (Object) position

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

- (Boolean) present?

Returns true if window is present.



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

def present?
  @handle = nil # relocate

  exists?
end

- (Object) resize_to(width, height)

Resizes window to given width and height.

Examples:

browser.window.resize_to 1600, 1200


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

- (Object) size

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

- (String) title

Returns window title.



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

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

  title
end

- (String) url

Returns window URL.



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

- (Object) use(&blk)

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