module RAutomation::Adapter::MsUia::Functions

@private

Public Class Methods

activate_window(hwnd) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 157
def activate_window(hwnd)
  set_foreground_window(hwnd)
  set_active_window(hwnd)
  bring_window_to_top(hwnd)
  within_foreground_thread(hwnd) do
    set_foreground_window(hwnd)
    set_active_window(hwnd)
    bring_window_to_top(hwnd)
  end
end
child_hwnd(window_hwnd, locators)
Alias for: control_hwnd
child_window_locators(parent_hwnd, locators) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 117
def child_window_locators(parent_hwnd, locators)
  child_hwnd = locators[:hwnd] || child_hwnd(parent_hwnd, locators)
  if child_hwnd
    locators.merge!(:hwnd => child_hwnd)
  else
    popup_hwnd = get_window(parent_hwnd, Constants::GW_ENABLEDPOPUP)
    if popup_hwnd != parent_hwnd
      popup_properties = window_properties(popup_hwnd, locators)
      locators.merge!(:hwnd => popup_hwnd) if locators_match?(locators, popup_properties)
    end
  end
  locators.merge(:adapter => :ms_uia)
end
close_window(hwnd) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 145
def close_window(hwnd)
  _close_window(hwnd)
  closed = send_message_timeout(hwnd, Constants::WM_CLOSE,
                                0, nil, Constants::SMTO_ABORTIFHUNG, 1000, nil)
  # force it to close
  unless closed
    process_hwnd = open_process(Constants::PROCESS_ALL_ACCESS, false, window_pid(hwnd))
    terminate_process(process_hwnd, 0)
    close_handle(process_hwnd)
  end
end
control_class(hwnd)
Alias for: window_class
control_hwnd(window_hwnd, locators) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 168
def control_hwnd(window_hwnd, locators)
  case
    when locators[:id]
      UiaDll.cached_hwnd(UiaDll::SearchCriteria.from_locator(window_hwnd, locators))
    when locators[:point]
      UiaDll::handle_from_point(*locators[:point])
    else
      find_hwnd(locators, window_hwnd) do |hwnd|
        locators_match?(locators, control_properties(hwnd, locators))
      end
  end
end
Also aliased as: child_hwnd
control_name(control_hwnd) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 200
def control_name(control_hwnd)
  UiaDll::name(UiaDll::SearchCriteria.from_locator(control_hwnd, :hwnd => control_hwnd))
end
control_text(hwnd)
Alias for: window_text
control_title(hwnd)
Alias for: window_title
control_value(control_hwnd) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 183
def control_value(control_hwnd)
  text_for(control_hwnd)
end
retrieve_combobox_item_text(control_hwnd, item_no) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 193
def retrieve_combobox_item_text(control_hwnd, item_no)
  text_len = 1024
  string_buffer = FFI::MemoryPointer.new :char, text_len
  UiaDll::select_list_value_at control_hwnd, item_no, string_buffer, text_len
  string_buffer.read_string
end
set_control_focus(control_hwnd) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 187
def set_control_focus(control_hwnd)
  within_foreground_thread control_hwnd do
    _set_control_focus(control_hwnd)
  end
end
window_class(hwnd) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 137
def window_class(hwnd)
  class_name = FFI::MemoryPointer.new :char, 512
  _window_class(hwnd, class_name, 512)
  class_name.read_string
end
Also aliased as: control_class
window_hwnd(locators) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 111
def window_hwnd(locators)
  find_hwnd(locators) do |hwnd|
    window_visible(hwnd) && locators_match?(locators, window_properties(hwnd, locators))
  end
end
window_pid(hwnd) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 131
def window_pid(hwnd)
  pid = FFI::MemoryPointer.new :int
  window_thread_process_id(hwnd, pid)
  pid.read_int
end
window_text(hwnd) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 99
def window_text(hwnd)
  found_text = ""
  window_callback = FFI::Function.new(:bool, [:long, :pointer], {:convention => :stdcall}) do |child_hwnd, _|
    found_text << text_for(child_hwnd)
    true
  end
  enum_child_windows(hwnd, window_callback, nil)
  found_text
end
Also aliased as: control_text
window_title(hwnd) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 90
def window_title(hwnd)
  title_length = window_title_length(hwnd) + 1
  title = FFI::MemoryPointer.new :char, title_length
  _window_title(hwnd, title, title_length)
  title.read_string
end
Also aliased as: control_title

Private Class Methods

control_properties(hwnd, locators) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 219
def control_properties(hwnd, locators)
  element_properties(:control, hwnd, locators)
end
element_properties(type, hwnd, locators) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 223
def element_properties(type, hwnd, locators)
  locators.inject({}) do |properties, locator|
    properties[locator[0]] = self.send("#{type}_#{locator[0]}", hwnd) unless locator[0] == :index
    properties
  end
end
find_hwnd(locators, window_hwnd = nil) { |hwnd| ... } click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 242
def find_hwnd(locators, window_hwnd = nil)
  found_hwnd = nil
  found_index = -1
  window_callback = FFI::Function.new(:bool, [:long, :pointer], {:convention => :stdcall}) do |hwnd, _|
    if yield(hwnd)
      found_index += 1

      if locators[:index]
        found_hwnd = hwnd if locators[:index] == found_index
      else
        found_hwnd = hwnd
      end
    end
    !found_hwnd
  end

  unless window_hwnd
    enum_windows(window_callback, nil)
  else
    enum_child_windows(window_hwnd, window_callback, nil)
  end

  found_hwnd
end
locators_match?(locators, properties) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 230
def locators_match?(locators, properties)
  locators.all? do |locator, value|
    if locator == :index
      true
    elsif value.is_a?(Regexp)
      properties[locator] =~ value
    else
      properties[locator] == value
    end
  end
end
text_for(hwnd) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 267
def text_for(hwnd)
  text_length = send_message(hwnd, Constants::WM_GETTEXTLENGTH, 0, nil) + 1
  text = FFI::MemoryPointer.new :char, text_length
  send_message(hwnd, Constants::WM_GETTEXT, text_length, text)
  text.read_string
end
window_properties(hwnd, locators) click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 215
def window_properties(hwnd, locators)
  element_properties(:window, hwnd, locators)
end
within_foreground_thread(hwnd) { || ... } click to toggle source
# File lib/rautomation/adapter/ms_uia/functions.rb, line 206
def within_foreground_thread(hwnd)
  foreground_thread = window_thread_process_id(foreground_window, nil)
  other_thread = window_thread_process_id(hwnd, nil)
  attach_thread_input(foreground_thread, other_thread, true) unless other_thread == foreground_thread
  yield
ensure
  attach_thread_input(foreground_thread, other_thread, false) unless other_thread == foreground_thread
end