Class: Btape::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/btape/executor.rb

Overview

Performs the parsed commands against a browser, turning any failure into a ScriptError that points back at the line it came from.

It lives apart from Runner because Runner's job is the recording session around the script — the browser, the frames, the GIF — while this is the script itself, and only this grows with each new command.

Constant Summary collapse

HANDLERS =

Commands that do something at run time, and the method that does it. Output, Viewport and Set are read before the run starts and have no behaviour of their own here.

{
  'Goto' => :goto, 'Click' => :click, 'Type' => :enter, 'Sleep' => :pause,
  'Screenshot' => :screenshot, 'Evaluate' => :evaluate,
  'WaitFor' => :wait_for_element, 'WaitForJS' => :wait_for_js,
  'Frame' => :frame, 'Press' => :press
}.freeze
MAIN_FRAME =
'main'

Instance Method Summary collapse

Constructor Details

#initialize(browser:, recorder:, settings:, lock: Monitor.new, logger: NullLogger.new) ⇒ Executor

Returns a new instance of Executor.



27
28
29
30
31
32
33
34
35
36
# File 'lib/btape/executor.rb', line 27

def initialize(browser:, recorder:, settings:, lock: Monitor.new, logger: NullLogger.new)
  @browser = browser
  # Elements and JavaScript are looked up in the current frame, which
  # starts as the page itself and moves when a Frame command says so.
  @target = browser
  @recorder = recorder
  @settings = settings
  @lock = lock
  @logger = logger
end

Instance Method Details

#call(commands) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/btape/executor.rb', line 38

def call(commands)
  commands.each do |command|
    @logger.debug("btape: line #{command.line_number}: #{command.name}")
    perform(command)
  # The run's own deadline arrives asynchronously and usually lands inside
  # a command, which would otherwise be reported as that command failing.
  # A caller has to be able to tell "the run outlasted Set Timeout" from
  # "this line is broken", so it goes out as it came in.
  rescue TimeoutError
    raise
  rescue StandardError => e
    raise ScriptError.new(command.line_number, "#{command.name} failed: #{e.message}")
  end
end