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
BROWSERS =
%w(chrome firefox ie edge safari).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.



13
14
15
16
17
18
# File 'lib/rainforest_ruby_runtime/runner.rb', line 13

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

#browserObject

Returns the value of attribute browser.



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

def browser
  @browser
end

#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



69
70
71
# File 'lib/rainforest_ruby_runtime/runner.rb', line 69

def current_browser
  current_driver.browser
end

#driver_klassObject



62
63
64
65
66
67
# File 'lib/rainforest_ruby_runtime/runner.rb', line 62

def driver_klass
  {
    'selenium' => Drivers::Selenium,
    'sauce' => Drivers::Sauce,
  }.fetch(driver_type)
end

#driver_typeObject



58
59
60
# File 'lib/rainforest_ruby_runtime/runner.rb', line 58

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

#run(codes) ⇒ Object



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

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

  setup_scope_registry!

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

  tests = codes.map { |code| dsl.run_code(code) }
  if tests.all? { |test| test.is_a?(Test) }
    describe = driver_klass.new(config_options).to_rspec(tests)
    run_rspec(describe)
  else
    raise WrongReturnValueError, tests.reject { |test| test.is_a?(Test) }
  end
  tests
ensure
  terminate_session!
end

#run_rspec(describe) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rainforest_ruby_runtime/runner.rb', line 41

def run_rspec(describe)
  if ENV['RUNTIME_ENV'] == 'test' && ENV['SHOW_OUTPUT'] != 'true'
    # if we're in tests, don't mix output from here with tests output
    # and don't include this describe block in the test count
    describe.run
    RSpec.world.example_groups.pop
  else
    RSpec.configure do |config|
      config.color = true
      config.formatter = :documentation
    end
    RSpec.configuration.reporter.report(RSpec.world.example_count([describe])) do |reporter|
      describe.run(reporter)
    end
  end
end

#session_idObject



73
74
75
76
77
78
79
# File 'lib/rainforest_ruby_runtime/runner.rb', line 73

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