Class: Moto::ThreadContext

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner, tests) ⇒ ThreadContext

Returns a new instance of ThreadContext.



10
11
12
13
14
15
16
17
18
19
# File 'lib/thread_context.rb', line 10

def initialize(runner, tests)
  @runner = runner
  @tests = tests
  @clients = {}
  @tests.each do |t|
    t.context = self
  end
  # TODO: add all *.yml files from that dir

  @config = YAML.load_file("#{MotoApp::DIR}/config/const.yml")
end

Instance Attribute Details

#current_testObject (readonly)

Returns the value of attribute current_test.



8
9
10
# File 'lib/thread_context.rb', line 8

def current_test
  @current_test
end

#log_pathObject (readonly)

Returns the value of attribute log_path.



7
8
9
# File 'lib/thread_context.rb', line 7

def log_path
  @log_path
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/thread_context.rb', line 6

def logger
  @logger
end

#runnerObject (readonly)

all resources specific for single thread will be initialized here. E.g. browser session



5
6
7
# File 'lib/thread_context.rb', line 5

def runner
  @runner
end

Instance Method Details

#client(name) ⇒ Object



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

def client(name)
  return @clients[name] if @clients.key? name
  
  name_app = 'MotoApp::Clients::' + name
  name_moto = 'Moto::Clients::' + name
  
  c = try_client(name_app, "#{MotoApp::DIR}/lib")
  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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/thread_context.rb', line 57

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

#runObject



74
75
76
77
78
79
80
81
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
# File 'lib/thread_context.rb', line 74

def run
  @tests.each do |test|
    # remove log files from previous execution

    Dir.glob("#{test.dir}/*.log").each {|f| File.delete f }
    max_attempts = @runner.my_config[:max_attempts] || 1
    @runner.environments.each do |env|
      params_path = "#{test.dir}/#{test.filename}.yml"
      params_all = [{}]
      params_all = YAML.load_file(params_path) if File.exists?(params_path)
      # or convert keys to symbols?

      # params_all = YAML.load_file(params_path).map{|h| Hash[ h.map{|k,v| [ k.to_sym, v ] } ] } if File.exists?(params_path)

      params_all.each do |params|
        # TODO: add filtering out params that are specific to certain envs

        (1..max_attempts).each do |attempt|
          test.init(env, params)
          # TODO: log path might be specified (to some extent) by the configuration

          @log_path = "#{test.dir}/#{test.name.gsub(/\s+/, '_').gsub('::', '_').gsub('/', '_')}.log"
          @logger = Logger.new(File.open(@log_path, File::WRONLY | File::TRUNC | File::CREAT))
          # TODO: make logger level configurable

          @logger.level = @runner.my_config[:log_level]
          @current_test = test
          @runner.listeners.each { |l| l.start_test(test) }
          @clients.each_value { |c| c.start_test(test) }
          test.before
          @logger.info "Start: #{test.name} attempt #{attempt}/#{max_attempts}"
          begin
            test.run
          rescue Exceptions::TestForcedPassed, Exceptions::TestForcedFailure, Exceptions::TestSkipped => e
            logger.info(e.message)
            @runner.result.add_error(test, e)
          rescue Exception => e  
            @logger.error("#{e.class.name}: #{e.message}")  
            @logger.error(e.backtrace.join("\n"))
            @runner.result.add_error(test, e)
          end 
          test.after
          @clients.each_value { |c| c.end_test(test) }
          @runner.listeners.each { |l| l.end_test(test) }
          @logger.info("Result: #{test.result}")
          @logger.close
          break unless [Result::FAILURE, Result::ERROR].include? test.result
        end # RETRY

      end
    end
  end
  @clients.each_value { |c| c.end_run }
end

#try_client(name, dir) ⇒ Object



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

def try_client(name, dir)
  begin      
    a = name.underscore.split('/')
    client_path = a[1..20].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  
    return nil
  end
end