Class: RAutomation::Adapter::MsUia::Control

Inherits:
Object
  • Object
show all
Includes:
Locators, WaitHelper
Defined in:
lib/rautomation/adapter/ms_uia/control.rb

Direct Known Subclasses

Button, Checkbox, Label, ListBox, ListItem, Radio, SelectList, Table, TextField

Instance Method Summary collapse

Constructor Details

#initialize(window, locators) ⇒ Control

Note:

this method is not meant to be accessed directly

Creates the control object.

Parameters:

  • window (RAutomation::Window)

    this button belongs to.

  • locators (Hash)

    for searching the button.

Options Hash (locators):

  • :value (String, Regexp)

    Value (text) of the button

  • :class (String, Regexp)

    Internal class name of the button

  • :id (String, Fixnum)

    Internal ID of the button

  • :index (String, Fixnum)

    0-based index to specify n-th button if all other criteria match

See Also:



17
18
19
20
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 17

def initialize(window, locators)
  @window = window
  extract(locators)
end

Instance Method Details

#assert_enabled



174
175
176
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 174

def assert_enabled
  raise "Cannot interact with disabled control #{@locators.inspect} on window #{@window.locators.inspect}!" if disabled?
end

#bounding_rectangle



119
120
121
122
123
124
125
126
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 119

def bounding_rectangle
  control = uia_element

  boundary = FFI::MemoryPointer.new :long, 4
  UiaDll::bounding_rectangle(control, boundary)

  boundary.read_array_of_long(4)
end

#click

todo - replace with UIA version



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 77

def click
  assert_enabled
  clicked = false
  wait_until do
    @window.activate
    @window.active? &&
        Functions.set_control_focus(hwnd) &&
        Functions.control_click(hwnd) &&
        clicked = true # is clicked at least once

    block_given? ? yield : clicked && !exist?
  end
end

#control_class



164
165
166
167
168
169
170
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 164

def control_class
  uia_control = uia_element
  element_class = FFI::MemoryPointer.new :char, UiaDll::get_class_name(uia_control, nil) + 1

  UiaDll::get_class_name(uia_control, element_class)
  element_class.read_string
end

#control_name



156
157
158
159
160
161
162
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 156

def control_name
  uia_control = uia_element
  element_name = FFI::MemoryPointer.new :char, UiaDll::get_name(uia_control, nil) + 1

  UiaDll::get_name(uia_control, element_name)
  element_name.read_string
end

#disabled?Boolean

todo - replace with UIA version

Returns:

  • (Boolean)


104
105
106
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 104

def disabled?
  Functions.unavailable?(hwnd)
end

#enabled?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 99

def enabled?
  !disabled?
end

#exist?Boolean Also known as: exists?

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 91

def exist?
  begin
    !!uia_element
  rescue UnknownElementException
    false
  end
end

#focus



113
114
115
116
117
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 113

def focus
  assert_enabled
  uia_control = UiaDll::element_from_handle(hwnd)
  UiaDll::set_focus(uia_control)
end

#focused?Boolean

todo - replace with UIA version

Returns:

  • (Boolean)


109
110
111
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 109

def focused?
  Functions.has_focus?(hwnd)
end

#get_current_control_type



148
149
150
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 148

def get_current_control_type
  UiaDll::current_control_type(uia_element)
end

#hwnd

todo - replace with UIA version



23
24
25
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 23

def hwnd
  Functions.control_hwnd(@window.hwnd, @locators)
end

#matches_type?(clazz) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 144

def matches_type?(clazz)
  get_current_control_type == clazz
end

#new_pid



152
153
154
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 152

def new_pid
  UiaDll::current_process_id(uia_element)
end

#uia_element



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 27

def uia_element
#          puts "finding element with #{@locators.inspect}"

  case
    #Causes a bug for some reason
#            when @locators[:value]
#              uia_window = UiaDll::element_from_handle(@window.hwnd)
#              begin
#                uia_window.read_pointer
#              rescue FFI::NullPointerError => e
#                raise UnknownElementException, "Window with handle #{@window.hwnd} does not exist"
#              end
#              uia_control = UiaDll::find_child_by_name(uia_window, @locators[:value].to_s)
#              begin
#                uia_control.read_pointer
#              rescue FFI::NullPointerError => e
#                raise UnknownElementException, "#{@locators[:value]} does not exist"
#              end
    when @locators[:focus]
      uia_control = UiaDll::get_focused_element
      begin
        uia_control.read_pointer
      rescue FFI::NullPointerError => e
        raise UnknownElementException, "Focused element does not exist"
      end
    when @locators[:id]
      uia_window = UiaDll::element_from_handle(@window.hwnd)
      uia_control = UiaDll::find_child_by_id(uia_window, @locators[:id].to_s)
      begin
        uia_control.read_pointer
      rescue FFI::NullPointerError => e
        raise UnknownElementException, "#{@locators[:id]} does not exist"
      end
    when @locators[:point]
      uia_control = UiaDll::element_from_point(@locators[:point][0], @locators[:point][1])
      begin
        uia_control.read_pointer
      rescue FFI::NullPointerError => e
        raise UnknownElementException, "#{@locators[:point]} does not exist"
      end
    else
      handle= hwnd
      raise UnknownElementException, "Element with #{@locators.inspect} does not exist" if (handle == 0) or (handle == nil)
      uia_control = UiaDll::element_from_handle(handle)
  end
  uia_control
end

#visible?Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rautomation/adapter/ms_uia/control.rb', line 128

def visible?
  element = UiaDll::element_from_handle(hwnd)

  off_screen = FFI::MemoryPointer.new :int

  if UiaDll::is_offscreen(element, off_screen) == 0
    fail "Could not check element"
  end

#          puts "return #{off_screen.read_int}"
  if off_screen.read_int == 0
    return true
  end
  false
end