Class: RAutomation::Adapter::Win32::Window

Inherits:
Object
  • Object
show all
Includes:
WaitHelper
Defined in:
lib/rautomation/adapter/win_32/window.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container, locators) ⇒ Window

Note:

this method is not meant to be accessed directly, but only through Window#initialize!

Creates the window object.

Parameters:

  • locators (Hash)

    for searching the window.

Options Hash (locators):

  • :title (String, Regexp)

    Title of the window

  • :text (String, Regexp)

    Visible text of the window

  • :class (String, Regexp)

    Internal class name of the window

  • :hwnd (String, Integer)

    Window handle in decimal format

  • :pid (String, Integer)

    Window process ID (PID)

  • :index (String, Integer)

    0-based index to specify n-th window if all other criteria match all other criteria match

See Also:



39
40
41
42
# File 'lib/rautomation/adapter/win_32/window.rb', line 39

def initialize(container, locators)
  @container = container
  extract(locators)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args)

Redirects all method calls not part of the public API to the Functions directly.



201
202
203
# File 'lib/rautomation/adapter/win_32/window.rb', line 201

def method_missing(name, *args)
  Functions.respond_to?(name) ? Functions.send(name, *args) : super
end

Instance Attribute Details

#container (readonly)

Returns the value of attribute container.



21
22
23
# File 'lib/rautomation/adapter/win_32/window.rb', line 21

def container
  @container
end

#locators (readonly)

Locators of the window.



24
25
26
# File 'lib/rautomation/adapter/win_32/window.rb', line 24

def locators
  @locators
end

Class Method Details

.oleacc_module_handle



12
13
14
15
16
17
18
# File 'lib/rautomation/adapter/win_32/window.rb', line 12

def oleacc_module_handle
  @oleacc_module_handle ||= begin
    oleacc = Functions.load_library "oleacc.dll"
    Functions.co_initialize nil
    oleacc
  end
end

Instance Method Details

#activate

See Also:



71
72
73
74
75
76
# File 'lib/rautomation/adapter/win_32/window.rb', line 71

def activate
  return if !exists? || active?
  restore if minimized?
  Functions.activate_window(hwnd)
  sleep 0.1
end

#active?Boolean

Returns:

  • (Boolean)

See Also:



79
80
81
# File 'lib/rautomation/adapter/win_32/window.rb', line 79

def active?
  exists? && Functions.foreground_window == hwnd
end

#button(locator)



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

def button(locator)
  Button.new(self, locator)
end

#checkbox(locator = {})



231
232
233
234
# File 'lib/rautomation/adapter/win_32/window.rb', line 231

def checkbox(locator={})
  @container.wait_until_present
  Checkbox.new(self, locator)
end

#child(locators) ⇒ RAutomation::Window

Note:

This is an Win32 adapter specific method, not part of the public API

Creates the child window object.

Examples:

RAutomation::Window.new(:title => /Windows Internet Explorer/i).
  child(:title => /some popup/)

Parameters:

  • locators (Hash)

    for searching the window.

Returns:



253
254
255
# File 'lib/rautomation/adapter/win_32/window.rb', line 253

def child(locators)
  RAutomation::Window.new Functions.child_window_locators(hwnd, locators)
end

#class_names

See Also:



62
63
64
65
66
67
68
# File 'lib/rautomation/adapter/win_32/window.rb', line 62

def class_names
  classes = []
  controls.each do |control|
    classes << control.class_name
  end
  classes.sort
end

#close

See Also:



176
177
178
# File 'lib/rautomation/adapter/win_32/window.rb', line 176

def close
  Functions.close_window(hwnd)
end

#control(locator = {})



216
217
218
219
# File 'lib/rautomation/adapter/win_32/window.rb', line 216

def control(locator={})
  @container.wait_until_present
  Control.new(self, locator)
end

#dimensionsHash

Returns Hash[:left, :top, :width, :height] with window coordinates.

Returns:

  • (Hash)

    Hash[:left, :top, :width, :height] with window coordinates



137
138
139
140
141
# File 'lib/rautomation/adapter/win_32/window.rb', line 137

def dimensions
  @container.wait_until_present
  left, top, right, bottom = Functions.window_rect(hwnd)
  {:left => left, :top => top, :width => right - left, :height => bottom - top}
end

#exists?Boolean

Returns:

  • (Boolean)

See Also:



89
90
91
92
# File 'lib/rautomation/adapter/win_32/window.rb', line 89

def exists?
  result = hwnd && Functions.window_exists(hwnd)
  !!result
end

#hwnd

Note:

Searches only for visible windows.

Retrieves handle of the window.

See Also:



47
48
49
# File 'lib/rautomation/adapter/win_32/window.rb', line 47

def hwnd
  @hwnd ||= Functions.window_hwnd(@locators)
end

#label(locator = {})

Win32 adapter specific API methods



206
207
208
209
# File 'lib/rautomation/adapter/win_32/window.rb', line 206

def label(locator={})
  @container.wait_until_present
  Label.new(self, locator)
end

#list_box(locator = {})



221
222
223
224
# File 'lib/rautomation/adapter/win_32/window.rb', line 221

def list_box(locator={})
  @container.wait_until_present
  ListBox.new(self, locator)
end

#maximize

See Also:



100
101
102
103
# File 'lib/rautomation/adapter/win_32/window.rb', line 100

def maximize
  Functions.show_window(hwnd, Constants::SW_MAXIMIZE)
  sleep 1
end

#minimize

See Also:



106
107
108
109
# File 'lib/rautomation/adapter/win_32/window.rb', line 106

def minimize
  Functions.show_window(hwnd, Constants::SW_MINIMIZE)
  sleep 1
end

#minimized?Boolean

Returns:

  • (Boolean)

See Also:



112
113
114
# File 'lib/rautomation/adapter/win_32/window.rb', line 112

def minimized?
  Functions.minimized(hwnd)
end

#mouse

Retrieves the Mouse object for automating mouse movements and clicks

Raises:



182
183
184
185
# File 'lib/rautomation/adapter/win_32/window.rb', line 182

def mouse
  @container.wait_until_present
  Mouse.new(self)
end

#move(coords = {})

Note:

All coordinates are optional and if not specified current coordinates will be used

Moves/resizes the window.

Parameters:

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

    for specifying the coordinates.

Options Hash (coords):

  • :left (Integer)

    Window coordinate from the left side of screen

  • :top (Integer)

    Window coordinate from the top of the screen

  • :width (Integer)

    Width of window

  • :height (Integer)

    Height of window



129
130
131
132
133
134
# File 'lib/rautomation/adapter/win_32/window.rb', line 129

def move(coords={})
  @container.wait_until_present
  rect = dimensions.merge(coords)
  Functions.move_window(hwnd, rect[:left], rect[:top], rect[:width], rect[:height])
  sleep 1
end

#password_field(locator)



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

def password_field(locator)
  @container.wait_until_present
  PasswordField.new(self, locator)
end

#pid

See Also:



52
53
54
# File 'lib/rautomation/adapter/win_32/window.rb', line 52

def pid
  Functions.window_pid(hwnd)
end

#radio(locator = {})



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

def radio(locator={})
  @container.wait_until_present
  Radio.new(self, locator)
end

#restore

See Also:



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

def restore
  Functions.show_window(hwnd, Constants::SW_RESTORE)
  sleep 1
end

#select_list(locator = {})



226
227
228
229
# File 'lib/rautomation/adapter/win_32/window.rb', line 226

def select_list(locator={})
  @container.wait_until_present
  SelectList.new(self, locator)
end

#send_keys(args)

Activates the window and sends keys to it.

Refer to Keys::KEYS for all the special keycodes.

Examples:

RAutomation::Window.new(:title => //).send_keys "hello!"
RAutomation::Window.new(:title => //).send_keys [:control, "a"], "world!"

See Also:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rautomation/adapter/win_32/window.rb', line 151

def send_keys(args)
  Keys.encode(args).each do |arg|
    wait_until do
      activate
      active?
    end

    if arg.is_a?(Array)
      arg.reduce([]) do |pressed_keys, k|
        if k == Keys[:null]
          pressed_keys.each {|pressed_key| release_key pressed_key}
          pressed_keys = []
        else
          pressed_keys << press_key(k)
        end
        pressed_keys
      end
    else
      send_key arg
    end
  end
  sleep 1
end

#table(locator = {})



241
242
243
244
# File 'lib/rautomation/adapter/win_32/window.rb', line 241

def table(locator={})
  @container.wait_until_present
  Table.new(self, locator)
end

#text

See Also:



84
85
86
# File 'lib/rautomation/adapter/win_32/window.rb', line 84

def text
  Functions.window_text(hwnd)
end

#text_field(locator)



195
196
197
# File 'lib/rautomation/adapter/win_32/window.rb', line 195

def text_field(locator)
  TextField.new(self, locator)
end

#title

See Also:



57
58
59
# File 'lib/rautomation/adapter/win_32/window.rb', line 57

def title
  Functions.window_title(hwnd)
end

#visible?Boolean

Returns:

  • (Boolean)

See Also:



95
96
97
# File 'lib/rautomation/adapter/win_32/window.rb', line 95

def visible?
  Functions.window_visible(hwnd)
end