Class: Browserctl::WorkflowContext

Inherits:
Object
  • Object
show all
Includes:
ContextualPersistence
Defined in:
lib/browserctl/workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ContextualPersistence

#fetch, #load_state, #save_state, #store

Constructor Details

#initialize(params, client, replay_context: nil) ⇒ WorkflowContext

Returns a new instance of WorkflowContext.



79
80
81
82
83
# File 'lib/browserctl/workflow.rb', line 79

def initialize(params, client, replay_context: nil)
  @params = params
  @client = client
  @replay_context = replay_context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



85
86
87
88
89
# File 'lib/browserctl/workflow.rb', line 85

def method_missing(name, *args)
  return @params[name] if @params.key?(name)

  super
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



77
78
79
# File 'lib/browserctl/workflow.rb', line 77

def client
  @client
end

#paramsObject (readonly)

Returns the value of attribute params.



77
78
79
# File 'lib/browserctl/workflow.rb', line 77

def params
  @params
end

#replay_contextObject (readonly)

Returns the value of attribute replay_context.



77
78
79
# File 'lib/browserctl/workflow.rb', line 77

def replay_context
  @replay_context
end

Instance Method Details

#ask(prompt) ⇒ Object



113
114
115
116
# File 'lib/browserctl/workflow.rb', line 113

def ask(prompt)
  $stderr.print("[browserctl] #{prompt} ")
  $stdin.gets.chomp
end

#assert(condition, msg = "assertion failed") ⇒ Object

Raises:



130
131
132
# File 'lib/browserctl/workflow.rb', line 130

def assert(condition, msg = "assertion failed")
  raise WorkflowError, msg unless condition
end

#assert_snapshot_stable(page_name, expected_digest:) ⇒ Object

Snapshots the named page and compares its digest against ‘expected_digest`. Under `workflow run –check` (a replay context is attached), a mismatch is recorded as a drift event with reason “post-snapshot mismatch” and the step still passes. Outside –check, mismatch raises WorkflowError so the workflow fails fast.

Raises:



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/browserctl/workflow.rb', line 139

def assert_snapshot_stable(page_name, expected_digest:)
  res = @client.snapshot(page_name.to_s, format: "elements")
  snapshot = res[:snapshot]
  actual = Replay::SnapshotDiff.digest(snapshot)
  return if actual == expected_digest

  msg = "post-snapshot mismatch on :#{page_name} — expected #{expected_digest}, got #{actual}"
  raise WorkflowError, msg unless @replay_context

  @replay_context.record(command: :assert_snapshot_stable, selector: page_name.to_s,
                         matched_ref: nil, score: nil, reason: "post-snapshot mismatch")
  warn "[browserctl replay] #{msg}"
end

#close_page(page_name) ⇒ Object

Raises:



106
107
108
109
110
111
# File 'lib/browserctl/workflow.rb', line 106

def close_page(page_name)
  res = @client.page_close(page_name.to_s)
  raise WorkflowError, res[:error] if res[:error]

  res
end

#composeObject

Raises:



153
154
155
156
157
# File 'lib/browserctl/workflow.rb', line 153

def compose(*)
  raise WorkflowError,
        "`compose` must be called at the workflow definition level, not inside a step block. " \
        "Did you mean `invoke`?"
end

#invoke(target_name, page: nil, **override_params) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/browserctl/workflow.rb', line 118

def invoke(target_name, page: nil, **override_params)
  name = target_name.to_s
  guard_circular!(name)

  flow = lookup_flow_target(name)
  if flow
    track_invoke(name) { run_invoked_flow(flow, page_name: page, **override_params) }
  else
    track_invoke(name) { run_nested(target_name, **override_params) }
  end
end

#open_page(page_name, url: nil) ⇒ Object

Raises:



99
100
101
102
103
104
# File 'lib/browserctl/workflow.rb', line 99

def open_page(page_name, url: nil)
  res = @client.page_open(page_name.to_s, url: url)
  raise WorkflowError, res[:error] if res[:error]

  res
end

#page(name) ⇒ Object



95
96
97
# File 'lib/browserctl/workflow.rb', line 95

def page(name)
  PageProxy.new(name.to_s, @client, replay_context: @replay_context)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/browserctl/workflow.rb', line 91

def respond_to_missing?(name, include_private = false)
  @params.key?(name) || super
end