Module: Testivai::ElementMap

Defined in:
lib/testivai/element_map.rb

Overview

Loads the canonical element-map collector and builds the expression the adapter injects.

element_map.js is GENERATED from packages/witness/src/capture/element-map.ts by scripts/generate-element-map-asset.js, and CI fails when it is stale. Every adapter — TypeScript, Python, Java, Ruby — injects that identical function. That matters because they all write into one shared .testivai/baselines/ directory: two languages producing subtly different maps for the same page would show up as a phantom regression.

Constant Summary collapse

DEFAULT_MAX_ELEMENTS =

Matches DEFAULT_MAX_ELEMENTS in the TypeScript source.

3000
ASSET =
Pathname.new(__dir__).join("element_map.js")

Class Method Summary collapse

Class Method Details

.expression(max_elements, ignore_selectors) ⇒ Object

Wrapped exactly as buildElementMapExpression does on the TypeScript side. WebDriver needs the explicit return.



44
45
46
47
# File 'lib/testivai/element_map.rb', line 44

def expression(max_elements, ignore_selectors)
  "return (#{source})(document, window, #{max_elements.to_i}, " \
    "#{JSON.generate(Array(ignore_selectors))});"
end

.sourceObject

Collector source with the generated banner stripped — that comment helps a reader but is pure weight on the wire, and this string ships to the browser on every capture.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/testivai/element_map.rb', line 27

def source
  @source ||= begin
    # Explicit UTF-8: the generated asset contains non-ASCII characters,
    # and Pathname#read would otherwise use the default external
    # encoding (US-ASCII on some systems), making `strip` raise.
    raw = ASSET.read(encoding: "UTF-8")
    start = raw.index("function collectElementMap")
    if start.nil?
      raise "element_map.js is malformed: collectElementMap not found. " \
            "Regenerate with scripts/generate-element-map-asset.js"
    end
    raw[start..].strip
  end
end