Class: RainforestRubyRuntime::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rainforest_ruby_runtime/runner.rb

Constant Summary collapse

FAILURE_EXCEPTIONS =
[
  RSpec::Expectations::ExpectationNotMetError,
  Capybara::ElementNotFound,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.



10
11
12
13
14
15
# File 'lib/rainforest_ruby_runtime/runner.rb', line 10

def initialize(options = {})
  @config_options = options.dup.freeze
  @step_variables = options[:step_variables]
  @callback = NilDelegator.new(options.fetch(:callback) { Empty.new })
  @logger = options.fetch(:logger) { Logger.new(StringIO.new) }
end

Instance Attribute Details

#config_optionsObject (readonly)

Returns the value of attribute config_options.



3
4
5
# File 'lib/rainforest_ruby_runtime/runner.rb', line 3

def config_options
  @config_options
end

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/rainforest_ruby_runtime/runner.rb', line 3

def logger
  @logger
end

Instance Method Details

#current_browserObject



75
76
77
# File 'lib/rainforest_ruby_runtime/runner.rb', line 75

def current_browser
  current_driver.browser
end

#driverObject



71
72
73
# File 'lib/rainforest_ruby_runtime/runner.rb', line 71

def driver
  ENV.fetch("CAPYBARA_DRIVER") { "selenium" }
end

#extract_results(code, fake_session_id: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rainforest_ruby_runtime/runner.rb', line 38

def extract_results(code, fake_session_id: nil)
  stdout = stderr = nil
  payload = nil
  begin
    stdout, stderr = capture_output2 do
      run(code)
    end
  rescue *FAILURE_EXCEPTIONS => e
    payload = exception_to_payload e, status: 'failed'
  rescue StandardError => e
    payload = exception_to_payload e, status: 'error'
  rescue SyntaxError, Exception => e
    payload = exception_to_payload e, status: 'fatal_error'
  end

  payload ||= { status: 'passed' }

  sid = fake_session_id || session_id

  payload = payload.merge({
    stdout: stdout,
    stderr: stderr,
    session_id: sid,
    driver: driver,
  })


  logger.debug("Payload")
  logger.debug(payload.inspect)

  payload
end

#run(code) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rainforest_ruby_runtime/runner.rb', line 17

def run(code)
  logger.debug "Running code:\n#{code}\nDriver: #{driver}"
  Capybara.default_driver = :"#{driver}"
  Capybara.default_max_wait_time = wait_time

  apply_config!
  setup_scope_registery!

  dsl = RainforestRubyRuntime::DSL.new(callback: @callback)

  test = dsl.run_code(code)
  if Test === test
    test.run
  else
    raise WrongReturnValueError, test
  end
  test
ensure
  terminate_session!
end

#session_idObject



79
80
81
82
83
84
85
# File 'lib/rainforest_ruby_runtime/runner.rb', line 79

def session_id
  current_browser.session_id if current_driver.browser.respond_to?(:session_id)
rescue Selenium::WebDriver::Error::WebDriverError => e
  logger.error "Can't retrieve session id"
  logger.error "#{e.class} #{e.message}\n#{e.backtrace.join("\n")}"
  nil
end