Class: RAutomation::Window

Inherits:
Object
  • Object
show all
Includes:
Adapter::Helper
Defined in:
lib/rautomation/window.rb

Constant Summary collapse

@@wait_timeout =

Timeout for waiting until object exists. If the timeout exceeds then an RAutomation::WaitHelper::TimeoutError is raised.

60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Adapter::Helper

#build_solution, #find_missing_externals, #move_adapter_dlls, #msbuild_solution, #supported_for_current_platform?

Constructor Details

#initialize(locators) ⇒ Window

Note:

Refer to all possible locators in each Adapter documentation.

Note:

This constructor doesn't check for window's existance.

Note:

Only visible windows are supported.

Note:

If given locators include :hwnd then every other possible locator is ignored.

Creates the window object.

Possible window locators may depend of the used platform and adapter, but following examples will use :title, :class and :hwnd.

locators may also include a key called :adapter to change default adapter, which is dependent of the platform, to automate windows and their controls.

It is also possible to change the default adapter by using environment variable called RAUTOMATION_ADAPTER

Examples:

Use window with some title:

RAutomation::Window.new(:title => "some title")

Use window with Regexp title:

RAutomation::Window.new(:title => /some title/i)

Use window with handle (hwnd):

RAutomation::Window.new(:hwnd => 123456)

Use multiple locators, every locator will be matched (AND-ed) to the window:

RAutomation::Window.new(:title => "some title", :class => "IEFrame")

Parameters:

  • locators (Hash)

    locators for the window.



67
68
69
70
71
72
73
74
# File 'lib/rautomation/window.rb', line 67

def initialize(locators)
  @adapter = locators.delete(:adapter) || ENV["RAUTOMATION_ADAPTER"] && ENV["RAUTOMATION_ADAPTER"].to_sym || default_adapter
  unless Adapter::Helper.supported_for_current_platform?(@adapter)
    raise UnsupportedAdapterException, "RAutomation #{@adapter} adapter is only supported on a 32bit Ruby - please use either :win32 adapter or 32bit Ruby."
  end

  @window = Adapter.const_get(normalize(@adapter)).const_get(:Window).new(self, locators)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args)

Allows to execute specific Adapter methods not part of the public API.



225
226
227
# File 'lib/rautomation/window.rb', line 225

def method_missing(name, *args)
  @window.send(name, *args)
end

Instance Attribute Details

#adapter (readonly)

Currently used Adapter.



36
37
38
# File 'lib/rautomation/window.rb', line 36

def adapter
  @adapter
end

Class Method Details

.wait_timeoutInteger

Retrieve current timeout in seconds to wait before RAutomation::WaitHelper::TimeoutError is raised.

Returns:

  • (Integer)

    timeout in seconds



88
89
90
# File 'lib/rautomation/window.rb', line 88

def wait_timeout
  @@wait_timeout
end

.wait_timeout=(timeout)

Change the timeout to wait before RAutomation::WaitHelper::TimeoutError is raised.

Parameters:

  • timeout (Integer)

    in seconds.



82
83
84
# File 'lib/rautomation/window.rb', line 82

def wait_timeout=(timeout)
  @@wait_timeout = timeout
end

.windows(locators = {}) ⇒ Windows

Returns all windows.

Parameters:

  • locators (Hash) (defaults to: {})

    locators for the window.

Returns:

  • (Windows)

    all windows.



22
23
24
# File 'lib/rautomation/window.rb', line 22

def windows(locators = {})
  Windows.new(nil, locators)
end

Instance Method Details

#activate

Activates the Window, e.g. brings it to the top of other windows.



123
124
125
# File 'lib/rautomation/window.rb', line 123

def activate
  @window.activate
end

#active?Boolean

Checks if the window is active, e.g. on the top of other windows.

Returns:

  • (Boolean)

    true if the window is active, false otherwise.



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

def active?
  @window.active?
end

#button(locators = {})

Note:

Refer to specific Adapter documentation for possible locator parameters.

Retrieves Button on the window.

Parameters:

  • locators (Hash) (defaults to: {})

    for the Button.

Raises:



211
212
213
214
# File 'lib/rautomation/window.rb', line 211

def button(locators={})
  wait_until_present
  Button.new(@window, locators)
end

#class_namesArray<String>

Returns all RAutomation::Window class names in a sorted array.

Returns:

Raises:



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

def class_names
  wait_until_present
  @window.class_names
end

#close

Closes the window if it exists.



202
203
204
205
# File 'lib/rautomation/window.rb', line 202

def close
  return unless @window.exists?
  @window.close
end

#exists?Boolean Also known as: exist?

Checks if the window exists (does have to be visible).

Returns:

  • (Boolean)

    true if the window exists, false otherwise.



143
144
145
# File 'lib/rautomation/window.rb', line 143

def exists?
  @window.exists?
end

#hwndInteger

Returns handle of the window which is used internally for other methods.

Returns:

  • (Integer)

    handle of the window which is used internally for other methods.

Raises:



96
97
98
99
# File 'lib/rautomation/window.rb', line 96

def hwnd
  wait_until_present
  @window.hwnd
end

#maximize

Maximizes the window.

Raises:



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

def maximize
  wait_until_present
  @window.maximize
end

#minimize

Minimizes the window.

Raises:



173
174
175
176
# File 'lib/rautomation/window.rb', line 173

def minimize
  wait_until_present
  @window.minimize
end

#minimized?Boolean

Checks if window is minimized.

Returns:

  • (Boolean)

    true if window is minimized, false otherwise.

Raises:



181
182
183
184
# File 'lib/rautomation/window.rb', line 181

def minimized?
  wait_until_present
  @window.minimized?
end

#pidInteger

Returns process identifier (PID) of the window.

Returns:

  • (Integer)

    process identifier (PID) of the window.

Raises:



103
104
105
106
# File 'lib/rautomation/window.rb', line 103

def pid
  wait_until_present
  @window.pid
end

#present?Boolean

Checks if the window exists and is visible.

Returns:

  • (Boolean)

    true if window exists and is visible, false otherwise



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

def present?
  exists? && visible?
end

#restore

Note:

If the window is minimized, makes it visible again.

Restores the window size and position.

Raises:



189
190
191
192
# File 'lib/rautomation/window.rb', line 189

def restore
  wait_until_present
  @window.restore
end

#send_keys(*keys)

Sends keyboard keys to the window. Refer to specific Adapter documentation for all possible values.

Raises:



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

def send_keys(*keys)
  wait_until_present
  @window.send_keys(keys)
end

#textString

Returns visible text of the Window.

Returns:

  • (String)

    visible text of the window.

Raises:



136
137
138
139
# File 'lib/rautomation/window.rb', line 136

def text
  wait_until_present
  @window.text
end

#text_field(locators = {})

Note:

Refer to specific Adapter documentation for possible locators parameters.

Retrieves TextField on the window.

Raises:



219
220
221
222
# File 'lib/rautomation/window.rb', line 219

def text_field(locators={})
  wait_until_present
  TextField.new(@window, locators)
end

#titleString

Returns title of the window.

Returns:

  • (String)

    title of the window.

Raises:



110
111
112
113
# File 'lib/rautomation/window.rb', line 110

def title
  wait_until_present
  @window.title
end

#visible?Boolean

Note:

Window is also visible, if it is behind other windows or minimized.

Checks if window is visible.

Returns:

  • (Boolean)

    true if window is visible, false otherwise.

Raises:



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

def visible?
  wait_until_exists
  @window.visible?
end

#wait_until_exists



235
236
237
238
239
# File 'lib/rautomation/window.rb', line 235

def wait_until_exists
  WaitHelper.wait_until {exists?}
rescue WaitHelper::TimeoutError
  raise UnknownWindowException, "Window with locator #{@window.locators.inspect} doesn't exist!"
end

#wait_until_present



229
230
231
232
233
# File 'lib/rautomation/window.rb', line 229

def wait_until_present
  WaitHelper.wait_until {present?}
rescue WaitHelper::TimeoutError
  raise UnknownWindowException, "Window with locator #{@window.locators.inspect} doesn't exist or is not visible!"
end

#windows(locators = @window.locators) ⇒ Windows

Retrieves all windows with similar locators to the current window.

Parameters:

  • locators (Hash) (defaults to: @window.locators)

    locators for the window.

Returns:

  • (Windows)

    all windows matching current window's locators if no explicit locators specified or windows matching the specified locators.



31
32
33
# File 'lib/rautomation/window.rb', line 31

def windows(locators = @window.locators)
  Windows.new(nil, locators)
end