Class: TUITD::TestRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/tui_td/test_runner.rb

Overview

Executes TUI tests defined in JSON format.

plan = File.read("test/hello.json")
results = TUITD::TestRunner.new(plan).run
puts results[:passed]  # => true

JSON format:

{
  "name": "My test",
  "rows": 24, "cols": 80, "timeout": 10,
  "steps": [
    {"start": "my_tui"},
    {"wait_for_text": "> "},
    {"send": "hello\n"},
    {"assert_text": "hello"},
    {"assert_fg": [0, 0], "is": "cyan"},
    {"close": true}
  ]
}

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ TestRunner

Returns a new instance of TestRunner.



29
30
31
32
33
# File 'lib/tui_td/test_runner.rb', line 29

def initialize(source)
  raw = source.is_a?(String) ? JSON.parse(source) : source
  @plan = raw.transform_keys(&:to_sym)
  @plan[:steps] = @plan[:steps].map { |s| s.transform_keys(&:to_sym) }
end

Instance Method Details

#runObject



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/tui_td/test_runner.rb', line 35

def run
  results = []
  all_passed = true
  driver = nil
  rows = @plan[:rows] || 40
  cols = @plan[:cols] || 120
  timeout = @plan[:timeout] || 30
  chdir = @plan[:chdir]

  @plan[:steps].each do |step|
    action = step.keys.first.to_s
    value = step.values.first
    start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

    begin
      r = case action
          when "start"
            driver&.close
            driver = Driver.new(value.to_s, rows: rows, cols: cols, timeout: timeout, chdir: chdir)
            driver.start
            Result.new(step: action, passed: true, message: "Started: #{value}")

          when "send"
            ensure_driver!(driver)
            driver.send(value.to_s)
            Result.new(step: action, passed: true, message: "Sent #{value.to_s.length} characters")

          when "send_key"
            ensure_driver!(driver)
            driver.send_keys(value.to_s.to_sym)
            Result.new(step: action, passed: true, message: "Sent key: #{value}")

          when "wait_for_text"
            ensure_driver!(driver)
            driver.wait_for_text(value.to_s)
            Result.new(step: action, passed: true, message: "Found: #{value}")

          when "wait_for_stable"
            ensure_driver!(driver)
            driver.wait_for_stable
            Result.new(step: action, passed: true, message: "Stable")

          when "assert_text"
            ensure_driver!(driver)
            state = State.new(driver.state_data)
            if state.find_text(value.to_s).any?
              Result.new(step: action, passed: true, message: "Text found: #{value}")
            else
              Result.new(step: action, passed: false, message: "Text NOT found: #{value}")
            end

          when "assert_fg"
            ensure_driver!(driver)
            row, col = coords(step)
            expected = step[:is] || step["is"]
            state = State.new(driver.state_data)
            actual = state.foreground_at(row, col)
            if actual == expected
              Result.new(step: action, passed: true, message: "FG at [#{row},#{col}] is #{expected}")
            else
              Result.new(step: action, passed: false, message: "FG at [#{row},#{col}] is #{actual}, expected #{expected}")
            end

          when "assert_bg"
            ensure_driver!(driver)
            row, col = coords(step)
            expected = step[:is] || step["is"]
            state = State.new(driver.state_data)
            actual = state.background_at(row, col)
            if actual == expected
              Result.new(step: action, passed: true, message: "BG at [#{row},#{col}] is #{expected}")
            else
              Result.new(step: action, passed: false, message: "BG at [#{row},#{col}] is #{actual}, expected #{expected}")
            end

          when "assert_style"
            ensure_driver!(driver)
            row, col = coords(step)
            state = State.new(driver.state_data)
            actual = state.style_at(row, col)
            expected = {}
            expected[:bold] = step[:bold] unless step[:bold].nil?
            expected[:italic] = step[:italic] unless step[:italic].nil?
            expected[:underline] = step[:underline] unless step[:underline].nil?
            match = expected.all? { |k, v| actual[k] == v }
            if match
              Result.new(step: action, passed: true, message: "Style at [#{row},#{col}] matches #{expected}")
            else
              Result.new(step: action, passed: false, message: "Style at [#{row},#{col}] is #{actual}, expected #{expected}")
            end

          when "screenshot"
            ensure_driver!(driver)
            path = value.is_a?(String) ? value : "/tmp/tui_td_#{Time.now.to_i}.png"
            driver.screenshot(path)
            Result.new(step: action, passed: true, message: "Saved: #{path}")

          when "html"
            ensure_driver!(driver)
            path = value.is_a?(String) ? value : "/tmp/tui_td_#{Time.now.to_i}.html"
            HtmlRenderer.new(driver.state_data).render(path)
            Result.new(step: action, passed: true, message: "Saved: #{path}")

          when "close"
            driver&.close
            driver = nil
            Result.new(step: action, passed: true, message: "Closed")

          else
            Result.new(step: action, passed: false, message: "Unknown action: #{action}")
          end

    rescue StandardError => e
      r = Result.new(step: action, passed: false, message: "#{e.class}: #{e.message}")
    end

    results << r
    all_passed &&= r.passed
  end

  driver&.close

  {
    name: @plan[:name] || "(unnamed)",
    passed: all_passed,
    results: results.map(&:to_h)
  }
end