Class: RAutomation::Adapter::Autoit::Window

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

Instance Attribute 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

  • :index (String, Integer)

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

See Also:



49
50
51
52
53
54
55
56
# File 'lib/rautomation/adapter/autoit/window.rb', line 49

def initialize(container, locators)
  @container = container
  @hwnd = locators[:hwnd]
  @locator_index = locators.delete(:index) if locators[:index] && locators.size == 1
  @locator_pid = locators.delete(:pid).to_i if locators[:pid]
  @locator_text = locators.delete(:text)
  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 AutoIt directly.

Examples:

execute AutoIt's WinGetTitle function:

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

See Also:



186
187
188
# File 'lib/rautomation/adapter/autoit/window.rb', line 186

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

Instance Attribute Details

#locators (readonly)

Locators of the window.



30
31
32
# File 'lib/rautomation/adapter/autoit/window.rb', line 30

def locators
  @locators
end

Instance Method Details

#activate

See Also:



97
98
99
100
101
# File 'lib/rautomation/adapter/autoit/window.rb', line 97

def activate
  @@autoit.WinWait(locator_hwnd, "", 1)
  @@autoit.WinActivate(locator_hwnd)
  sleep 1
end

#active?Boolean

Returns:

  • (Boolean)

See Also:



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

def active?
  @@autoit.WinActive(locator_hwnd) == 1
end

#button(locator = {})



172
173
174
# File 'lib/rautomation/adapter/autoit/window.rb', line 172

def button(locator={})
  Button.new(self, locator)
end

#class_names

See Also:



92
93
94
# File 'lib/rautomation/adapter/autoit/window.rb', line 92

def class_names
  @@autoit.WinGetClassList(locator_hwnd).split("\n").sort
end

#close

See Also:



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

def close
  @@autoit.WinClose(locator_hwnd)
  @@autoit.WinKill(locator_hwnd)
end

#exists?Boolean

Returns:

  • (Boolean)

See Also:



114
115
116
# File 'lib/rautomation/adapter/autoit/window.rb', line 114

def exists?
  @@autoit.WinExists(locator_hwnd) == 1
end

#hwnd

Note:

Searches only for visible windows.

Retrieves handle of the window.

See Also:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rautomation/adapter/autoit/window.rb', line 61

def hwnd
  @hwnd ||= begin
    locators = @autoit_locators
    if @locator_index || @locator_pid
      # @todo Come up with some better solution for this case
      locators = "[regexptitle:]" # match all, needed for the case when only :index or :pid is used
    end
    windows = @@autoit.WinList(locators, @locator_text).pop.compact.
        map { |handle| RAutomation::Window.new(:hwnd => handle.hex) }
    windows.delete_if { |window| !window.visible? }

    if @locator_pid
      window = windows.find { |win| win.pid == @locator_pid }
    else
      window = windows[@locator_index || 0]
    end
    window ? window.hwnd : nil
  end
end

#maximize

See Also:



124
125
126
127
# File 'lib/rautomation/adapter/autoit/window.rb', line 124

def maximize
  @@autoit.WinSetState(locator_hwnd, "", @@autoit.SW_MAXIMIZE)
  sleep 1
end

#minimize

See Also:



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

def minimize
  @@autoit.WinSetState(locator_hwnd, "", @@autoit.SW_MINIMIZE)
  sleep 1
end

#minimized?Boolean

Returns:

  • (Boolean)

See Also:



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

def minimized?
  @@autoit.WinGetState(locator_hwnd) & 16 == 16
end

#mouse



165
166
167
168
# File 'lib/rautomation/adapter/autoit/window.rb', line 165

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

#pid

See Also:



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

def pid
  @@autoit.WinGetProcess(locator_hwnd).to_i
end

#restore

See Also:



141
142
143
144
# File 'lib/rautomation/adapter/autoit/window.rb', line 141

def restore
  @@autoit.WinSetState(locator_hwnd, "", @@autoit.SW_RESTORE)
  sleep 1
end

#send_keys(keys)

Activates the window and sends keys to it.

Refer to AutoIt documentation at http://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm for keys syntax.

See Also:



151
152
153
154
155
156
157
# File 'lib/rautomation/adapter/autoit/window.rb', line 151

def send_keys(keys)
  wait_until do
    activate
    active?
  end
  @@autoit.Send(keys[0])
end

#text

See Also:



109
110
111
# File 'lib/rautomation/adapter/autoit/window.rb', line 109

def text
  @@autoit.WinGetText(locator_hwnd)
end

#text_field(locator = {})



178
179
180
# File 'lib/rautomation/adapter/autoit/window.rb', line 178

def text_field(locator={})
  TextField.new(self, locator)
end

#title

See Also:



87
88
89
# File 'lib/rautomation/adapter/autoit/window.rb', line 87

def title
  @@autoit.WinGetTitle(locator_hwnd)
end

#visible?Boolean

Returns:

  • (Boolean)

See Also:



119
120
121
# File 'lib/rautomation/adapter/autoit/window.rb', line 119

def visible?
  @@autoit.WinGetState(locator_hwnd) & 2 == 2
end