Class: Capybara::Simulated::Trace

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/simulated/trace.rb

Overview

Per-test trace of Capybara actions with DOM snapshots, console output, and network requests interleaved. JSON output, one file per test — downstream tooling builds whatever viewer it wants.

Off by default. ‘CSIM_TRACE_DIR=/path/to/dir` enables auto-mode via `Browser#record_action`; the RSpec hook in `csim_rspec.rb` persists with a slugged filename. Programmatic activation is via `Driver#start_tracing` / `#stop_tracing`.

Defined Under Namespace

Classes: Step

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata: {}) ⇒ Trace

Returns a new instance of Trace.



34
35
36
37
38
39
40
41
# File 'lib/capybara/simulated/trace.rb', line 34

def initialize(metadata: {})
  @steps        = []
  @metadata     = 
  @started_at   = monotonic_ms
  @console_buf  = []
  @network_buf  = []
  @open_step    = nil
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



32
33
34
# File 'lib/capybara/simulated/trace.rb', line 32

def 
  @metadata
end

#stepsObject (readonly)

Returns the value of attribute steps.



32
33
34
# File 'lib/capybara/simulated/trace.rb', line 32

def steps
  @steps
end

Instance Method Details

#begin_step(kind, description:, url_before: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/capybara/simulated/trace.rb', line 57

def begin_step(kind, description:, url_before: nil)
  finish_step if @open_step
  @open_step = {
    kind:        kind,
    description: description,
    url_before:  url_before,
    start_ms:    monotonic_ms
  }
  @console_buf = []
  @network_buf = []
end

#empty?Boolean

Returns:

  • (Boolean)


90
# File 'lib/capybara/simulated/trace.rb', line 90

def empty? = @steps.empty?

#finish_step(url_after: nil, dom_after: nil, error: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/capybara/simulated/trace.rb', line 69

def finish_step(url_after: nil, dom_after: nil, error: nil)
  return unless @open_step
  s = @open_step
  @steps << Step.new(
    index:        @steps.size,
    kind:         s[:kind],
    description:  s[:description],
    url_before:   s[:url_before],
    url_after:    url_after,
    dom_after:    dom_after,
    console:      @console_buf,
    network:      @network_buf,
    elapsed_ms:   (s[:start_ms] - @started_at).round,
    duration_ms:  (monotonic_ms - s[:start_ms]).round,
    error:        error
  )
  @open_step   = nil
  @console_buf = []
  @network_buf = []
end

#log_console(severity, message) ⇒ Object

Pushed from ‘Browser#log_console` (the JS bridge’s ‘console.*` host-fn target) and `Browser#rack_request` (network). Entries land on the currently-open step; outside a step they’re dropped (boot noise, post-test cleanup).



47
48
49
50
# File 'lib/capybara/simulated/trace.rb', line 47

def log_console(severity, message)
  return unless @open_step
  @console_buf << {severity: severity.to_s, message: message.to_s}
end

#log_network(method, url, status) ⇒ Object



52
53
54
55
# File 'lib/capybara/simulated/trace.rb', line 52

def log_network(method, url, status)
  return unless @open_step
  @network_buf << {method: method.to_s, url: url.to_s, status: status}
end

#to_hObject



92
93
94
# File 'lib/capybara/simulated/trace.rb', line 92

def to_h
  {version: 1, metadata: @metadata, steps: @steps.map(&:to_h)}
end

#to_json(*args) ⇒ Object



96
# File 'lib/capybara/simulated/trace.rb', line 96

def to_json(*args) = JSON.generate(to_h, *args)

#write_json(path) ⇒ Object



98
99
100
101
102
# File 'lib/capybara/simulated/trace.rb', line 98

def write_json(path)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, to_json)
  path
end