Skip to content

Commit

Permalink
AutoIt adapter: allow to search windows by PID
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmo committed Jan 30, 2011
1 parent 90f8e49 commit 6255d2c
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/rautomation/adapter/autoit/window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ def load_autoit
# @see RAutomation::Window#initialize
def initialize(locators)
@hwnd = locators[:hwnd]
if locators[:index] && locators.size == 1
@locator_index = locators.delete(:index)
end
@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
Expand All @@ -59,20 +58,26 @@ def initialize(locators)
def hwnd
@hwnd ||= begin
locators = @autoit_locators
if @locator_index
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 is used
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
handles = @@autoit.WinList(locators, @locator_text).pop.compact.
find_all {|handle| self.class.new(:hwnd => handle.hex).visible?}
handle = handles[@locator_index || 0]
handle ? handle.hex : nil
window ? window.hwnd : nil
end
end

# @see RAutomation::Window#pid
def pid
@@autoit.WinGetProcess(hwnd)
@@autoit.WinGetProcess(locator_hwnd).to_i
end

# @see RAutomation::Window#title
Expand Down

0 comments on commit 6255d2c

Please sign in to comment.