Class: Moto::ThreadContext
- Inherits:
-
Object
- Object
- Moto::ThreadContext
- Defined in:
- lib/thread_context.rb
Instance Attribute Summary collapse
-
#current_test ⇒ Object
readonly
Returns the value of attribute current_test.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#runner ⇒ Object
readonly
all resources specific for single thread will be initialized here.
Instance Method Summary collapse
- #client(name) ⇒ Object
- #const(key) ⇒ Object
-
#initialize(runner, test, test_reporter) ⇒ ThreadContext
constructor
A new instance of ThreadContext.
- #run ⇒ Object
- #try_client(name, dir) ⇒ Object
Constructor Details
#initialize(runner, test, test_reporter) ⇒ ThreadContext
Returns a new instance of ThreadContext.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/thread_context.rb', line 11 def initialize(runner, test, test_reporter) @runner = runner @test = test @clients = {} @test.context = self @test_reporter = test_reporter #TODO temporary fix Thread.current['context']= self @config = {} Dir.glob("config/*.yml").each do |f| @config.deep_merge! YAML.load_file(f) end end |
Instance Attribute Details
#current_test ⇒ Object (readonly)
Returns the value of attribute current_test.
9 10 11 |
# File 'lib/thread_context.rb', line 9 def current_test @current_test end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/thread_context.rb', line 8 def logger @logger end |
#runner ⇒ Object (readonly)
all resources specific for single thread will be initialized here. E.g. browser session
7 8 9 |
# File 'lib/thread_context.rb', line 7 def runner @runner end |
Instance Method Details
#client(name) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/thread_context.rb', line 27 def client(name) return @clients[name] if @clients.key? name name_app = 'MotoApp::Lib::Clients::' + name name_moto = 'Moto::Clients::' + name c = try_client(name_app, "#{MotoApp::DIR}/") unless c.nil? @clients[name] = c return c end c = try_client(name_moto, "#{Moto::DIR}/lib") unless c.nil? @clients[name] = c return c end raise "Could not find client class for name #{name}" end |
#const(key) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/thread_context.rb', line 65 def const(key) key = key.to_s key = "#{@current_test.env.to_s}.#{key}" if @current_test.env != :__default code = if key.include? '.' "@config#{key.split('.').map { |a| "['#{a}']" }.join('')}" else "@config['#{key}']" end begin v = eval code raise if v.nil? rescue raise "There is no const defined for key: #{key}. Environment: #{ (@current_test.env == :__default) ? '<none>' : @current_test.env }" end v end |
#run ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/thread_context.rb', line 82 def run # remove log/screenshot files from previous execution Dir.glob("#{@test.dir}/*.{log,png}").each { |f| File.delete f } max_attempts = @runner.my_config[:max_attempts] || 1 sleep_time = @runner.my_config[:sleep_before_attempt] || 0 @runner.environments.each do |env| params_all = [{}] # RB Config files params_path = "#{@test.dir}/#{@test.filename}" params_all = eval(File.read(params_path)) if File.exists?(params_path) params_all.each_with_index do |params, params_index| # Filtering out param sets that are specific to certain envs unless params['__env'].nil? allowed_envs = params['__env'].is_a?(String) ? [params['__env']] : params['__env'] next unless allowed_envs.include? env end @test.init(env, params, params_index) # @test.log_path = "#{@test.dir}/#{@test.name.gsub(/\s+/, '_').gsub(':', '_').gsub('::', '_').gsub('/', '_')}.log" @test.log_path = "#{@test.dir}/#{@test.name.demodulize.gsub('/', '_')}.log" # TODO: log path might be specified (to some extent) by the configuration @logger = Logger.new(File.open(@test.log_path, File::WRONLY | File::TRUNC | File::CREAT)) @logger.level = @runner.my_config[:log_level] || Logger::DEBUG @current_test = @test (1..max_attempts).each do |attempt| # Reporting: start_test if attempt == 1 @test_reporter.report_start_test(@test.status) end @clients.each_value { |c| c.start_test(@test) } @test.before @logger.info "Start: #{@test.name} attempt #{attempt}/#{max_attempts}" begin @test.run_test rescue Exceptions::TestForcedPassed, Exceptions::TestForcedFailure, Exceptions::TestSkipped => e @logger.info(e.) rescue Exception => e @logger.error("#{e.class.name}: #{e.}") @logger.error(e.backtrace.join("\n")) @clients.each_value { |c| c.handle_test_exception(@test, e) } end @test.after @clients.each_value { |c| c.end_test(@test) } @logger.info("Result: #{@test.status.results.last.code}") # test should have another attempt only in case of an error # pass, skip and fail statuses end attempts if @test.status.results.last.code != Moto::Test::Result::ERROR break end # don't go to sleep in the last attempt if attempt < max_attempts sleep sleep_time end end # Make another attempt # Close and flush stream to file @logger.close # Reporting: end_test @test_reporter.report_end_test(@test.status) end end @clients.each_value { |c| c.end_run } end |
#try_client(name, dir) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/thread_context.rb', line 47 def try_client(name, dir) begin a = name.underscore.split('/') client_path = a[1..-1].join('/') require "#{dir}/#{client_path}" client_const = name.constantize instance = client_const.new(self) instance.init instance.start_run instance.start_test(@current_test) return instance rescue Exception => e # puts e # puts e.backtrace return nil end end |