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("#{APP_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, APP_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



57
58
59
60
# File 'lib/thread_context.rb', line 57

def const(key)
  # TODO: add support to consts with no env
  @config[@current_test.env.to_s][key.to_s]
end

#runObject



62
63
64
65
66
67
68
69
70
71
72
73
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
# File 'lib/thread_context.rb', line 62

def run
  @tests.each do |test|
    @runner.environments.each do |env|
      params_path = "#{test.dir}/#{test.filename}.yml"
      params_all = [{}]
      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 = YAML.load_file(params_path) if File.exists?(params_path)
      params_all.each do |params|
        # TODO: add filtering out params that are specific to certain envs
        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"
        # TODO: remove log files from previous execution
        @logger = Logger.new(File.open(@log_path, File::WRONLY | File::TRUNC | File::CREAT))
        # TODO: make logger level configurable
        @logger.level = Moto::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}"
        begin
          test.run
        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
      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