module Uia::Finder

Public Instance Methods

find_child(parent, locator) click to toggle source
# File lib/uia/finder.rb, line 31
def find_child(parent, locator)
  scope = (locator.delete(:scope) || :descendants).to_s.capitalize

  valid_locators = [:title, :handle, :id, :name, :value, :control_type, :pattern, :scope]
  raise BadChildLocator, locator unless (locator.keys - valid_locators).empty?

  case
    when locator[:title]
      find_by_title locator[:title], parent.handle
    when locator[:handle]
      find_by_handle locator[:handle]
    else
      conditions = locator.collect {|k, v|  Library.send("#{k}_condition", v) }
      Library.find_first parent, scope, *conditions
  end
end
find_children(parent, locator) click to toggle source
# File lib/uia/finder.rb, line 48
def find_children(parent, locator)
  scope = (locator.delete(:scope) || :descendants).to_s.capitalize

  valid_locators = [:id, :name, :value, :control_type, :pattern, :scope]
  raise BadChildLocator, locator unless (locator.keys - valid_locators).empty?

  conditions = locator.collect {|k, v|  Library.send("#{k}_condition", v) }
  Library.find_all parent, scope, *conditions
end
find_from_root(locator) click to toggle source
# File lib/uia/finder.rb, line 12
def find_from_root(locator)
  case
    when locator[:id]
      find_by_id locator[:id]
    when locator[:name], locator[:value]
      find_by_name locator[:name] || locator[:value]
    when locator[:pid]
      find_by_pid locator[:pid]
    when locator[:runtime_id]
      find_by_runtime_id locator[:runtime_id]
    when locator[:handle]
      find_by_handle locator[:handle]
    when locator[:title]
      find_by_title locator[:title]
    else
      raise BadLocator, locator
  end
end

Private Instance Methods

find_by_handle(handle) click to toggle source
# File lib/uia/finder.rb, line 75
def find_by_handle(handle)
  Library.find_by_handle handle
end
find_by_id(id) click to toggle source
# File lib/uia/finder.rb, line 59
def find_by_id(id)
  find_by_property(:id, id)
end
find_by_name(name) click to toggle source
# File lib/uia/finder.rb, line 63
def find_by_name(name)
  find_by_property(:name, name)
end
find_by_pid(pid) click to toggle source
# File lib/uia/finder.rb, line 67
def find_by_pid(pid)
  Library.find_by_pid(pid)
end
find_by_property(property, what) click to toggle source
# File lib/uia/finder.rb, line 93
def find_by_property(property, what)
  case what
    when String
      Library.send("find_by_#{property}", what)
    when Regexp
      children.find { |e| e.send(property) =~ what }
  end
end
find_by_runtime_id(runtime_id) click to toggle source
# File lib/uia/finder.rb, line 71
def find_by_runtime_id(runtime_id)
  Library.find_by_runtime_id(runtime_id)
end
find_by_title(title, parent=0) click to toggle source
# File lib/uia/finder.rb, line 79
def find_by_title(title, parent=0)
  found_window = Win32.find_window(parent) do |handle|
    case title
      when Regexp
        Win32.window_title(handle) =~ title
      else
        Win32.window_title(handle) == title
    end
  end
  find_by_handle found_window if found_window
rescue
  nil
end