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(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, Fixnum)

    Window handle in decimal format

  • :index (String, Fixnum)

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

See Also:



47
48
49
50
51
52
53
# File 'lib/rautomation/adapter/autoit/window.rb', line 47

def initialize(locators)
  @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:



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

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

Instance Attribute Details

#locators (readonly)

Locators of the window.



28
29
30
# File 'lib/rautomation/adapter/autoit/window.rb', line 28

def locators
  @locators
end

Instance Method Details

#activate

See Also:



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

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

#active?Boolean

Returns:

  • (Boolean)

See Also:



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

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

#button(locator)



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

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

#close

See Also:



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

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

#exists?Boolean

Returns:

  • (Boolean)

See Also:



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

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

#hwnd

Note:

Searches only for visible windows.

Retrieves handle of the window.

See Also:



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

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| self.class.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:



116
117
118
119
# File 'lib/rautomation/adapter/autoit/window.rb', line 116

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

#minimize

See Also:



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

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

#minimized?Boolean

Returns:

  • (Boolean)

See Also:



128
129
130
# File 'lib/rautomation/adapter/autoit/window.rb', line 128

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

#pid

See Also:



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

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

#restore

See Also:



133
134
135
136
# File 'lib/rautomation/adapter/autoit/window.rb', line 133

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 www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm for keys syntax.

See Also:



143
144
145
146
147
148
149
# File 'lib/rautomation/adapter/autoit/window.rb', line 143

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

#text

See Also:



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

def text
  @@autoit.WinGetText(locator_hwnd)
end

#text_field(locator)



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

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

#title

See Also:



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

def title
  @@autoit.WinGetTitle(locator_hwnd)
end

#visible?Boolean

Returns:

  • (Boolean)

See Also:



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

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