Class: Bewildr::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/bewildr/element.rb

Overview

All MS UI Automation elements accessed using bewildr are represented as instances of Bewildr::Element.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Element

Returns a new instance of Bewildr::Element wrapping the MS UI Automation Element passed in as an argument. You probably won’t be calling this method directly - if you are then you probably know what your’e doing



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bewildr/element.rb', line 12

def initialize(input)
  @automation_element = input
  case input
  when System::Windows::Automation::AutomationElement
    set_control_type
    build_element
    include_additions
  when nil then @control_type = :non_existent
  else raise Bewildr::BewildrInternalError, "Can only initialize an element with a nil or a Sys::Win::Auto::AE[C], not a #{input.class}"
  end
end

Instance Attribute Details

#automation_elementObject (readonly)

Returns the value of attribute automation_element.



6
7
8
# File 'lib/bewildr/element.rb', line 6

def automation_element
  @automation_element
end

#control_typeObject (readonly)

Returns the value of attribute control_type.



7
8
9
# File 'lib/bewildr/element.rb', line 7

def control_type
  @control_type
end

Instance Method Details

#automation_idObject

Returns the underlying automation element’s automation id property



31
32
33
34
# File 'lib/bewildr/element.rb', line 31

def automation_id
  existence_check
  @automation_element.current.automation_id.to_s
end

#childrenObject

Returns an array containing this element’s (direct) children



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/bewildr/element.rb', line 135

def children
  return [] unless has_children?
  walker = System::Windows::Automation::TreeWalker.ControlViewWalker #RawViewWalker
  bewildred_children = []
  child = walker.get_first_child(@automation_element)
  while !child.nil? do
    bewildred_children << Bewildr::Element.new(child)
    child = walker.get_next_sibling(child)
  end
  bewildred_children
end

#clickObject

Clicks this element - this is done by actual mouse moves and clicks. The automation element’s underlying InvokePattern is not used.



162
163
164
# File 'lib/bewildr/element.rb', line 162

def click
  Bewildr::Mouse.click(clickable_point)
end

#clickable_pointObject

Returns the underlying automation element’s clickable point property - this is used by the various click methods



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/bewildr/element.rb', line 177

def clickable_point
  existence_check
  Timeout.timeout(30) do
    current_clickable_point = nil
    begin
      current_clickable_point = @automation_element.get_clickable_point
    rescue System::Windows::Automation::NoClickablePointException
      retry
    end
    return current_clickable_point
  end
end

#contains?(condition_hash) ⇒ Boolean Also known as: contain?

Returns true if this element has a descendant that meets the search criteria, false if it doesn’t Takes a condition hash

Returns:

  • (Boolean)


56
57
58
# File 'lib/bewildr/element.rb', line 56

def contains?(condition_hash)
  get(condition_hash).exists?
end

#double_clickObject

Double clicks this element - this is done by actual mouse moves and clicks. The automation element’s underlying InvokePattern is not used.



167
168
169
# File 'lib/bewildr/element.rb', line 167

def double_click
  Bewildr::Mouse.double_click(clickable_point)
end

#drag(target_details) ⇒ Object

my_element.drag :via => another_element, :to => some_element, :wait_at_via_for => 2.5



210
211
212
213
214
# File 'lib/bewildr/element.rb', line 210

def drag(target_details)
  drag_args = target_details
  drag_args[:from] = self
     Bewildr::Mouse.drag(drag_args)
end

#enabled?Boolean

Returns true if the underlying automation element is enabled, false if it’s not

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/bewildr/element.rb', line 62

def enabled?
  existence_check
  @automation_element.current.is_enabled
end

#exists?Boolean Also known as: exist?

Returns true if the element exists, false if it doesn’t

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
# File 'lib/bewildr/element.rb', line 37

def exists?
  return false if @control_type == :non_existent
  begin
    @automation_element.current.bounding_rectangle
    return true
  rescue System::Windows::Automation::ElementNotAvailableException, TypeError => e
    return false
  end
end

#focusObject

Gives this element focus



84
85
86
# File 'lib/bewildr/element.rb', line 84

def focus
  @automation_element.set_focus
end

#get(condition_hash) ⇒ Object

Returns the result of searching this element’s children or descendants for the first or all matches of a particular id, name or type (or combination!). See Bewildr::Finder for more details

:id => "id"
:name => "bob"
:type => :button
:scope => :children / :descendants
:how_many => :first / :all


101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bewildr/element.rb', line 101

def get(condition_hash)
  existence_check
  condition = Finder.condition_for(condition_hash)
  scope = Finder.scope_for(condition_hash)
  how_many = Finder.how_many_for(condition_hash)
  
  result = @automation_element.send how_many, scope, condition
  case result
  when System::Windows::Automation::AutomationElement, nil then return Bewildr::Element.new(result)
  when System::Windows::Automation::AutomationElementCollection
    System::Collections::ArrayList.new(result).to_array.to_a.collect {|element| Bewildr::Element.new(element) }
  end
end

#has_children?Boolean

Returns true if this element has descendants, false if it doesn’t

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/bewildr/element.rb', line 129

def has_children?
  walker = System::Windows::Automation::TreeWalker.RawViewWalker
  !walker.get_first_child(@automation_element).nil?
end

#heightObject

Returns the height of the element in pixels



191
192
193
# File 'lib/bewildr/element.rb', line 191

def height
  @automation_element.current.bounding_rectangle.height.to_i
end

#nameObject

Returns the underlying automation element’s name property



25
26
27
28
# File 'lib/bewildr/element.rb', line 25

def name
  existence_check
  @automation_element.current.name.to_s
end

#next_siblingObject

Returns the current element’s next sibling, or nil if there is no next sibling



148
149
150
151
152
# File 'lib/bewildr/element.rb', line 148

def next_sibling
  walker = System::Windows::Automation::TreeWalker.ControlViewWalker
  potential_next_sibling = walker.get_next_sibling(@automation_element)
  potential_next_sibling.nil? ? nil : Bewildr::Element.new(potential_next_sibling)
end

#parentObject

Returns a new element representing this element’s parent in the UI element tree



121
122
123
124
125
126
# File 'lib/bewildr/element.rb', line 121

def parent
  return nil if root?
  walker = System::Windows::Automation::TreeWalker.RawViewWalker
  potential_parent = walker.get_parent(@automation_element)
  Bewildr::Element.new(potential_parent)
end

#previous_siblingObject

Returns the current element’s previous sibling, or nil if there is no previous sibling



155
156
157
158
159
# File 'lib/bewildr/element.rb', line 155

def previous_sibling
  walker = System::Windows::Automation::TreeWalker.ControlViewWalker
  potential_previous_sibling = walker.get_previous_sibling(@automation_element)
  potential_previous_sibling.nil? ? nil : Bewildr::Element.new(potential_previous_sibling)
end

#right_clickObject

Right clicks this element - this is done by actual mouse moves and clicks. The automation element’s underlying InvokePattern is not used.



172
173
174
# File 'lib/bewildr/element.rb', line 172

def right_click
  Bewildr::Mouse.right_click(clickable_point)
end

#root?Boolean

Returns true if this element is the root element of the UI element tree

Returns:

  • (Boolean)


116
117
118
# File 'lib/bewildr/element.rb', line 116

def root?
  @automation_element == System::Windows::Automation::AutomationElement.root_element
end

#scrollable?Boolean

Returns true if this element is scrollable, false if it isn’t.

Returns:

  • (Boolean)


89
90
91
92
# File 'lib/bewildr/element.rb', line 89

def scrollable?
  return false unless @automation_element.get_supported_patterns.collect {|pattern| pattern.programmatic_name.to_s }.include?("ScrollPatternIdentifiers.Pattern")
  vertically_scrollable
end

#visible?Boolean

Returns true if the element is visible, false if it isn’t

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/bewildr/element.rb', line 49

def visible?
  existence_check
  !@automation_element.current.is_offscreen
end

#wait_for_existence_of(condition_hash) ⇒ Object Also known as: wait_for

Waits up to 30 seconds for a descendant of this element to exist that meets the search criteria, returns the element if found



68
69
70
71
72
73
# File 'lib/bewildr/element.rb', line 68

def wait_for_existence_of(condition_hash)
  Timeout.timeout(30) do
    sleep 0.1 until contains?(condition_hash)
  end
  get(condition_hash)
end

#wait_for_non_existence_of(condition_hash) ⇒ Object

Waits for up to 30 seconds for this element to no longer have a descendant element that meest the criteria



77
78
79
80
81
# File 'lib/bewildr/element.rb', line 77

def wait_for_non_existence_of(condition_hash)
  Timeout.timeout(30) do
    sleep 0.1 unless contains?(condition_hash)
  end
end

#widthObject

Returns the width of the element in pixels



196
197
198
# File 'lib/bewildr/element.rb', line 196

def width
  @automation_element.current.bounding_rectangle.width.to_i
end