Class: Bewildr::Finder

Inherits:
Object
  • Object
show all
Extended by:
BewildrHelpers
Defined in:
lib/bewildr/finder.rb

Overview

Wraps the Search Condition paradigm used by MS UI Automation to find objects in the UI Element tree. It contains various methods that build up the parts needed for to search for elements. These methods are called by Bewildr::Element#get in order to get and test for existence of descendent elements in the UI element tree. All of the methods take a condition hash, this is made up of at least one, but can be several key/value pairs.

Class Method Summary collapse

Methods included from BewildrHelpers

r_array_to_cs_array_of_conditions, r_array_to_cs_array_of_strings, r_string_to_c_string

Class Method Details

.condition_for(condition_hash) ⇒ Object

Returns a System::Windows::Automation::PropertyCondition or AndCondition based on a translation of the condition hash. The condition hash must contain at least one of the following keys:

:id => "some id"
:name => "some name"
:type => :some_control_type

These keys can be combined to create more complex search criteria



18
19
20
21
22
23
24
25
26
27
# File 'lib/bewildr/finder.rb', line 18

def condition_for(condition_hash)
  conditions = condition_hash.select {|key, value| [:id, :name, :type].include?(key) }
  conditions = Hash[*conditions.flatten] if conditions.instance_of?(Array) #if condition deals with ironruby's 1.8.6 emulation

  case
  when conditions.length == 0 then raise "Condition needs to include at least an :id, a :name or a :type"
  when conditions.length == 1 then return single_condition(conditions)
  when conditions.length > 1  then return multiple_conditions(conditions)
  end
end

.how_many_for(condition_hash) ⇒ Object

Returns whether the seach should stop at the first element that matches the criteria or whether it should find all elements that match. If neither is specified, the default is to find only the first element that matches. The condition hash should contain one of the following options:

:how_many => :first
:how_many => :all
nothing


47
48
49
50
51
52
53
# File 'lib/bewildr/finder.rb', line 47

def how_many_for(condition_hash)
  case condition_hash[:how_many]
  when :first, nil then :find_first
  when :all then :find_all
  else raise "Invalid number of elements to look for. Use ':first' or ':all'"
  end
end

.scope_for(condition_hash) ⇒ Object

Returns whether the seach should interrogate only children, or all descendants. If neither is specified, the default is to search the descendants. The condition hash should contain one of the following options:

:scope => :children
:scope => :descendants
nothing


34
35
36
37
38
39
# File 'lib/bewildr/finder.rb', line 34

def scope_for(condition_hash)
  case condition_hash[:scope]
  when :children          then System::Windows::Automation::TreeScope.Children
  when :descendants, nil  then System::Windows::Automation::TreeScope.Descendants
  end
end