Class: TansParser::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/tans_parser/selector.rb

Overview

Scans terminal state for recognized UI elements.

selector = Selector.new(state)
selector.get_by_text("OK")       # => [Element, ...]
selector.get_by_role(:button)    # => [Element, ...]
selector.buttons                 # => [Element, ...]
selector.dialogs                 # => [Element, ...]
selector.button(text: "OK")      # => Element or nil

Constant Summary collapse

TOP_LEFT_CORNERS =
/[┌┏┎┍╭╔╓╒]/
BOTTOM_LEFT_CORNERS =
%w[     ].freeze
BOTTOM_RIGHT_CORNERS =
%w[     ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state) ⇒ Selector

Returns a new instance of Selector.



22
23
24
25
# File 'lib/tans_parser/selector.rb', line 22

def initialize(state)
  @state = state.is_a?(State) ? state : State.new(state)
  @elements = scan
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



20
21
22
# File 'lib/tans_parser/selector.rb', line 20

def elements
  @elements
end

#stateObject (readonly)

Returns the value of attribute state.



20
21
22
# File 'lib/tans_parser/selector.rb', line 20

def state
  @state
end

Instance Method Details

#button(**filters) ⇒ Object

Convenience accessors (singular — return first element or nil)



81
82
83
# File 'lib/tans_parser/selector.rb', line 81

def button(**filters)
  buttons(**filters).first
end

#buttons(**filters) ⇒ Object

Convenience accessors (plural — return arrays)



44
45
46
# File 'lib/tans_parser/selector.rb', line 44

def buttons(**filters)
  get_by_role(:button, **filters)
end

#checkbox(**filters) ⇒ Object



85
86
87
# File 'lib/tans_parser/selector.rb', line 85

def checkbox(**filters)
  checkboxes(**filters).first
end

#checkboxes(**filters) ⇒ Object



48
49
50
# File 'lib/tans_parser/selector.rb', line 48

def checkboxes(**filters)
  get_by_role(:checkbox, **filters)
end

#dialog(**filters) ⇒ Object



89
90
91
# File 'lib/tans_parser/selector.rb', line 89

def dialog(**filters)
  dialogs(**filters).first
end

#dialogs(**filters) ⇒ Object



52
53
54
# File 'lib/tans_parser/selector.rb', line 52

def dialogs(**filters)
  get_by_role(:dialog, **filters)
end

#get_by_role(role, text: nil, checked: nil, disabled: nil) ⇒ Object

Find elements by role with optional filters. rubocop:disable Metrics/CyclomaticComplexity



34
35
36
37
38
39
40
# File 'lib/tans_parser/selector.rb', line 34

def get_by_role(role, text: nil, checked: nil, disabled: nil)
  results = @elements.select { |e| e.role == role.to_sym }
  results = results.select { |e| e.text.to_s.include?(text.to_s) } if text
  results = results.select { |e| e.checked == checked } unless checked.nil?
  results = results.select { |e| e.disabled == disabled } unless disabled.nil?
  results
end

#get_by_text(text) ⇒ Object

Find elements by visible text (partial match).



28
29
30
# File 'lib/tans_parser/selector.rb', line 28

def get_by_text(text)
  @elements.select { |e| e.text&.include?(text) }
end

#input(**filters) ⇒ Object



93
94
95
# File 'lib/tans_parser/selector.rb', line 93

def input(**filters)
  inputs(**filters).first
end

#inputs(**filters) ⇒ Object



56
57
58
# File 'lib/tans_parser/selector.rb', line 56

def inputs(**filters)
  get_by_role(:input, **filters)
end

#label(**filters) ⇒ Object



97
98
99
# File 'lib/tans_parser/selector.rb', line 97

def label(**filters)
  labels(**filters).first
end

#labels(**filters) ⇒ Object



60
61
62
# File 'lib/tans_parser/selector.rb', line 60

def labels(**filters)
  get_by_role(:label, **filters)
end


101
102
103
# File 'lib/tans_parser/selector.rb', line 101

def menu(**filters)
  menus(**filters).first
end


64
65
66
# File 'lib/tans_parser/selector.rb', line 64

def menus(**filters)
  get_by_role(:menu, **filters)
end

#progress_bar(**filters) ⇒ Object



113
114
115
# File 'lib/tans_parser/selector.rb', line 113

def progress_bar(**filters)
  progress_bars(**filters).first
end

#progress_bars(**filters) ⇒ Object



76
77
78
# File 'lib/tans_parser/selector.rb', line 76

def progress_bars(**filters)
  get_by_role(:progress, **filters)
end

#statusbar(**filters) ⇒ Object



109
110
111
# File 'lib/tans_parser/selector.rb', line 109

def statusbar(**filters)
  statusbars(**filters).first
end

#statusbars(**filters) ⇒ Object



72
73
74
# File 'lib/tans_parser/selector.rb', line 72

def statusbars(**filters)
  get_by_role(:statusbar, **filters)
end

#tab(**filters) ⇒ Object



105
106
107
# File 'lib/tans_parser/selector.rb', line 105

def tab(**filters)
  tabs(**filters).first
end

#tabs(**filters) ⇒ Object



68
69
70
# File 'lib/tans_parser/selector.rb', line 68

def tabs(**filters)
  get_by_role(:tab, **filters)
end

#within(element, &block) ⇒ Object

Scope subsequent searches to a specific element’s bounding box.



118
119
120
121
122
123
124
125
# File 'lib/tans_parser/selector.rb', line 118

def within(element, &block)
  scoped = ScopedSelector.new(self, element)
  if block
    yield scoped
  else
    scoped
  end
end