Class: Plushie::Test::Session
- Inherits:
-
Object
- Object
- Plushie::Test::Session
- Defined in:
- lib/plushie/test/session.rb
Overview
Test session driving the Elm loop against a pooled renderer. Each test gets its own Session with isolated model state.
Interactions (click, type_text, toggle, etc.) are sent via the renderer's interact protocol; resulting events are fed through the app's update cycle so the model stays in sync.
See the Elixir SDK's mock renderer backend for reference.
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Current app model.
Instance Method Summary collapse
-
#click(selector) ⇒ Object
Click a button widget.
-
#element_text(element) ⇒ String?
Extract text content from an element hash.
-
#find(selector) ⇒ Hash?
Find a widget by selector.
-
#find!(selector) ⇒ Hash
Find a widget by selector.
-
#initialize(app_class, pool:, session_id:) ⇒ Session
constructor
A new instance of Session.
-
#move_to(x, y) ⇒ Object
Move cursor to coordinates.
-
#press(key) ⇒ Object
Press a key (key down).
-
#release(key) ⇒ Object
Release a key (key up).
-
#reset ⇒ Object
Reset the session to initial state.
-
#screenshot(name, width: 1024, height: 768) ⇒ Hash
Capture a screenshot.
-
#select(selector, value) ⇒ Object
Select a value from pick_list, combo_box, or radio.
-
#slide(selector, value) ⇒ Object
Slide a slider to a value.
-
#stop ⇒ Object
Stop the session and release the renderer session.
-
#submit(selector) ⇒ Object
Submit a text_input (press Enter).
-
#toggle(selector) ⇒ Object
Toggle a checkbox or toggler.
-
#tree ⇒ Hash
Get the full tree from the renderer.
-
#tree_hash(name) ⇒ Hash
Capture a structural tree hash.
-
#type_key(key) ⇒ Object
Type a key (press + release).
-
#type_text(selector, text) ⇒ Object
Type text into a text_input or text_editor.
Constructor Details
#initialize(app_class, pool:, session_id:) ⇒ Session
Returns a new instance of Session.
22 23 24 25 26 27 28 29 30 |
# File 'lib/plushie/test/session.rb', line 22 def initialize(app_class, pool:, session_id:) @app = app_class.new @pool = pool @session_id = session_id @format = pool.format @model = nil @tree = nil init_app end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns current app model.
17 18 19 |
# File 'lib/plushie/test/session.rb', line 17 def model @model end |
Instance Method Details
#click(selector) ⇒ Object
Click a button widget.
36 37 38 |
# File 'lib/plushie/test/session.rb', line 36 def click(selector) interact("click", selector) end |
#element_text(element) ⇒ String?
Extract text content from an element hash. Checks content, label, value, placeholder in order.
193 194 195 196 197 |
# File 'lib/plushie/test/session.rb', line 193 def element_text(element) return nil unless element props = element["props"] || element[:props] || {} props["content"] || props["label"] || props["value"] || props["placeholder"] end |
#find(selector) ⇒ Hash?
Find a widget by selector. Returns the node hash or nil.
108 109 110 111 112 113 114 115 116 |
# File 'lib/plushie/test/session.rb', line 108 def find(selector) id = SecureRandom.hex(4) response = @pool.send_and_wait( {type: "query", id: id, target: "find", selector: build_selector(selector)}, @session_id, :query_response ) data = response[:data] || response["data"] (data.nil? || data.empty?) ? nil : data end |
#find!(selector) ⇒ Hash
Find a widget by selector. Raises with a helpful error if not found, including the current tree IDs and similar ID suggestions.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/plushie/test/session.rb', line 124 def find!(selector) result = find(selector) unless result all_ids = Tree.ids(@tree) target = selector.start_with?("#") ? selector[1..] : selector suggestions = find_similar_ids(target, all_ids) msg = "Widget not found: #{selector}\n" msg << "\n Did you mean: #{suggestions.map { "##{_1}" }.join(", ")}\n" if suggestions.any? msg << "\n Current tree IDs: #{all_ids.join(", ")}" raise Plushie::Error, msg end result end |
#move_to(x, y) ⇒ Object
Move cursor to coordinates.
99 100 101 |
# File 'lib/plushie/test/session.rb', line 99 def move_to(x, y) interact("move_to", nil, x: x, y: y) end |
#press(key) ⇒ Object
Press a key (key down).
80 81 82 |
# File 'lib/plushie/test/session.rb', line 80 def press(key) interact("press", nil, key: key) end |
#release(key) ⇒ Object
Release a key (key up).
86 87 88 |
# File 'lib/plushie/test/session.rb', line 86 def release(key) interact("release", nil, key: key) end |
#reset ⇒ Object
Reset the session to initial state.
175 176 177 178 179 |
# File 'lib/plushie/test/session.rb', line 175 def reset @model = nil @tree = nil init_app end |
#screenshot(name, width: 1024, height: 768) ⇒ Hash
Capture a screenshot.
166 167 168 169 170 171 172 |
# File 'lib/plushie/test/session.rb', line 166 def screenshot(name, width: 1024, height: 768) id = SecureRandom.hex(4) @pool.send_and_wait( {type: "screenshot", id: id, name: name, width: width, height: height}, @session_id, :screenshot_response ) end |
#select(selector, value) ⇒ Object
Select a value from pick_list, combo_box, or radio.
67 68 69 |
# File 'lib/plushie/test/session.rb', line 67 def select(selector, value) interact("select", selector, value: value) end |
#slide(selector, value) ⇒ Object
Slide a slider to a value.
74 75 76 |
# File 'lib/plushie/test/session.rb', line 74 def (selector, value) interact("slide", selector, value: value) end |
#stop ⇒ Object
Stop the session and release the renderer session.
182 183 184 185 186 187 |
# File 'lib/plushie/test/session.rb', line 182 def stop @pool.unregister(@session_id) rescue => e # Swallow errors during cleanup warn "plushie test: error during session cleanup: #{e.message}" if $DEBUG end |
#submit(selector) ⇒ Object
Submit a text_input (press Enter).
49 50 51 52 53 54 |
# File 'lib/plushie/test/session.rb', line 49 def submit(selector) # Read current value from local tree node = find_in_local_tree(selector) value = node_prop(node, "value") || "" interact("submit", selector, value: value) end |
#toggle(selector) ⇒ Object
Toggle a checkbox or toggler.
58 59 60 61 62 |
# File 'lib/plushie/test/session.rb', line 58 def toggle(selector) node = find_in_local_tree(selector) current = node_prop(node, "checked") || node_prop(node, "active") || false interact("toggle", selector, value: !current) end |
#tree ⇒ Hash
Get the full tree from the renderer.
141 142 143 144 145 146 147 148 |
# File 'lib/plushie/test/session.rb', line 141 def tree id = SecureRandom.hex(4) response = @pool.send_and_wait( {type: "query", id: id, target: "tree", selector: {}}, @session_id, :query_response ) response[:data] || response["data"] end |
#tree_hash(name) ⇒ Hash
Capture a structural tree hash.
153 154 155 156 157 158 159 |
# File 'lib/plushie/test/session.rb', line 153 def tree_hash(name) id = SecureRandom.hex(4) @pool.send_and_wait( {type: "tree_hash", id: id, name: name}, @session_id, :tree_hash_response ) end |
#type_key(key) ⇒ Object
Type a key (press + release).
92 93 94 |
# File 'lib/plushie/test/session.rb', line 92 def type_key(key) interact("type_key", nil, key: key) end |
#type_text(selector, text) ⇒ Object
Type text into a text_input or text_editor.
43 44 45 |
# File 'lib/plushie/test/session.rb', line 43 def type_text(selector, text) interact("type_text", selector, text: text) end |