awetestlib 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ module Legacy
2
+
3
+ #--
4
+ ##def open_log
5
+ # start = Time.now.to_f.to_s
6
+ #
7
+ # logTS = Time.at(@myRun.launched.to_f).strftime("%Y%m%d%H%M%S")
8
+ # xls = @myAppEnv.xls_name.gsub('.xls', '') + '_' if @myAppEnv.xls_name.length > 0
9
+ # @logFileSpec = "#{@myRoot}/#{logdir}/#{@myName}_#{@targetBrowser.abbrev}_#{xls}#{logTS}.log"
10
+ # init_logger(@logFileSpec, @myName)
11
+ #
12
+ # # message_tolog( self.inspect )
13
+ # message_to_log("#{@myName} launched at [#{@myRun.launched.to_f.to_s}][#{@myScript.id}][#{@myRun.id}][#{@myChild.id}]")
14
+ # debug_to_log("pid: #{$$}")
15
+ # message_to_log("#{@myName} begin at [#{start}]")
16
+ # message_to_log("#{@myName} environment [#{@myAppEnv.name}]")
17
+ # message_to_log("#{@myName} xls_name [#{@myAppEnv.xls_name}]") if @myAppEnv.xls_name.length > 0
18
+ # message_to_log("#{@myName} rootDir [#{@myRoot}]")
19
+ # message_to_log("#{@myName} Target Browser [#{@targetBrowser.name}]")
20
+ # mark_testlevel(@myParent.name, @myParent.level) # Module
21
+ # mark_testlevel(@myChild.name, @myChild.level) # SubModule
22
+ #
23
+ #end
24
+ #++
25
+
26
+ #def find_me(where, how, what)
27
+ # me = where.element(how, what)
28
+ # puts me.inspect
29
+ #rescue
30
+ # error_to_log("#{where.inspect} doesn't seem to respond to element() #{$!}")
31
+ #end
32
+
33
+ # def click_me(element)
34
+ # element.click
35
+ # rescue
36
+ # error_to_log("#{element.inspect} doesn't seem to respond to click() #{$!}")
37
+ # end
38
+
39
+
40
+ end
@@ -0,0 +1,444 @@
1
+ #require 'screencap'
2
+ module Logging
3
+
4
+ def self.included(mod)
5
+ # puts "RegressionSupport::Logging extended by #{mod}"
6
+ end
7
+
8
+ def log_message(severity, message, tag = '', lnbr = nil, addts = 1, exception=nil)
9
+ # caller = get_caller(lnbr, exception)
10
+
11
+ # @sequence ||= log_properties ? log_properties.fetch('sequence', 0) : 0
12
+ # @sequence += 1
13
+
14
+ t = Time.now.utc
15
+ @last_t ||= t
16
+
17
+ # if log_properties
18
+ # log_args = {
19
+ # :cycle => @cycle,
20
+ # :browser_sequence => @browser_sequence,
21
+ # :session_num => @session_num,
22
+ # :sequence => @sequence,
23
+ # :job_id => log_properties['job_id'],
24
+ ## :project_version_id => log_properties['project_version_id'],
25
+ # :test_run_id => log_properties['test_run_id'],
26
+ # :script_id => log_properties['script_id'],
27
+ # :caller => caller.split(":")[0] || 'unknown',
28
+ # :caller_line => caller.split(":")[1].to_i,
29
+ # :caller_method => caller.split(":")[2],
30
+ # :severity => severity,
31
+ # :message => message.gsub(/[\x80-\xff]/,"?"),
32
+ # :detail_timestamp => t.to_f.to_s,
33
+ # :duration => t.to_f-@last_t.to_f,
34
+ # :created_at => t,
35
+ ### :company_id => log_properties['company_id'],
36
+ # :project_id => log_properties['project_id'],
37
+ # :level => tag.andand.is_a?(Fixnum) ? tag : nil,
38
+ # :pass => pass_code_for(tag),
39
+ # :test_category_id => log_properties['test_category_id'],
40
+ # :test_case_id => log_properties['test_case_id'],
41
+ # :application_role_id => nil, # not implemented yet
42
+ # :screen_path => nil
43
+ # }
44
+ # Resque::Job.create(log_queue.to_sym, log_class, log_args) if log_queue && log_class
45
+ #
46
+ # ::Screencap.capture(Shamisen::BROWSER_MAP[@browser],
47
+ # log_properties['test_run_id'], @sequence, root_path) if @screencap_path
48
+ #end
49
+
50
+ @last_t = t
51
+
52
+ dt = t.strftime("%Y%m%d %H%M%S")+' '+t.usec.to_s[0, 4]
53
+ mySev = translate_severity(severity)
54
+ myCaller = get_caller(lnbr) || 'unknown'
55
+
56
+ myMsg = "%-8s" % mySev
57
+ myMsg << '[' + dt + ']:'
58
+ if tag
59
+ if tag.is_a? Fixnum
60
+ tag = '-LVL' + tag.to_s
61
+ end
62
+ end
63
+
64
+ myMsg << "[%-5s]:" % tag
65
+ #myMsg << '[' + t.to_f.to_s + ']:'
66
+ #myMsg << '[' + myCaller + ']:'
67
+ #myMsg << "#{get_call_list[-1]}#{get_call_list[-2]} "
68
+ myMsg << get_call_list_new.to_s
69
+ myMsg << ' '+message
70
+ # myMsg << " {#{lnbr}} " if lnbr
71
+
72
+ # # TODO This is broken: @myBrowser is not necessarily populated
73
+ # if @screenCaptureOn and is_browser?(@myBrowser)
74
+ # if severity >= @options['screenshot'] andand
75
+ # tag.match(/PASS|FAIL/)
76
+ # then
77
+ # capture_screen(@myBrowser, t)
78
+ # end
79
+ # end
80
+
81
+ @myLog.add(severity, myMsg) if @myLog # add persistent logging for awetestlib. pmn 05jun2012
82
+ puts myMsg+"\n"
83
+
84
+ nil # so method doesn't return whole @output.
85
+ end
86
+
87
+ #private log_message
88
+
89
+ def pass_code_for(tag)
90
+ case
91
+ when tag =~ /PASS/
92
+ 'P'
93
+ when tag =~ /FAIL/
94
+ 'F'
95
+ #when tag =~ /\d+/ # avoid having to require andand for awetestlib. pmn 05jun2012
96
+ when tag.andand.is_a?(Fixnum)
97
+ 'H'
98
+ when tag =~ /DONE/
99
+ 'D'
100
+ when tag =~ /role/
101
+ 'R'
102
+ end
103
+ end
104
+
105
+ def log_sikuli_output(output_file, passed)
106
+ output_lines = File.open(output_file, 'r') { |f| f.readlines }
107
+ puts "IM FAILING?! #{passed}"
108
+
109
+ # if passed
110
+
111
+ log_messages = ['[log]', '[error]']
112
+ output_lines = output_lines.select { |l| log_messages } #.detect{|msg| l.include?(msg)} }
113
+ while line = output_lines.shift do
114
+ puts "line to be logged: #{line}"
115
+ if line.include? '[log]'
116
+ passed_to_log line
117
+ elsif line.include? '[error]'
118
+ failed_to_log line
119
+ elsif line.match /\s*Exception/
120
+ failed_to_log output_lines.join("\n")
121
+ break
122
+ else
123
+ debug_tolog line
124
+ end
125
+ end
126
+
127
+ # else
128
+ # failed_to_log "SIKULI LOG:\n\n #{output_lines.join('\n')}"
129
+ # end
130
+
131
+ return { :result => passed, :msg => output_str }
132
+ end
133
+
134
+ # Put status message to the log and output window
135
+ # TODO: figure out a way to do the leveling automatically based on actual call depth within script (and project library?)
136
+ # When using to mark test groupings, include
137
+ # level 'lvl' (numeric literal, 1 through 9, usually 1-4)
138
+ # indicating test grouping hierarchy:
139
+ # 0 lowest level test case, a single validation
140
+ # a.k.a TEST CASE, VALIDATION
141
+ # not normally used in scripts as it is
142
+ # implied by method with 'validate' in name
143
+ # 1 group of closely related level 0 validations
144
+ # a.k.a TEST GROUP
145
+ # should never be followed by another level 1
146
+ # or higher level message without intervening
147
+ # level 0 validations.
148
+ # 2 group of closely related level 1 validation sets.
149
+ # a.k.a TEST SET, SUBMODULE, USE CASE
150
+ # should never be followed by another level 2
151
+ # or higher level message without intervening
152
+ # lower levels.
153
+ # 3 group of closely related level 2 validation sets.
154
+ # a.k.a TEST SET, TEST SUITE, MODULE, USE CASE
155
+ # should never be followed by another level 3
156
+ # or higher level message without intervening
157
+ # lower levels.
158
+ # 4 group of closely related level 3 validation sets.
159
+ # a.k.a TEST SUITE, APPLICATION UNDER TEST, PLAN, PROJECT
160
+ # should never be followed by another level 4
161
+ # or higher level message without intervening
162
+ # lower levels. Will seldom appear directly in
163
+ # scripts
164
+
165
+ =begin rdoc
166
+ category: Logging
167
+ tags: report, log, test level
168
+ =end
169
+ def mark_testlevel(message, lvl, desc = '', dbg = nil)
170
+ strg = ''
171
+ strg << message
172
+ strg << " [#{desc}]" if desc.length > 0
173
+ strg << " \n#{get_debug_list}" if dbg or @debug_calls
174
+ log_message(INFO, strg, lvl, 1)
175
+ rescue
176
+ failed_to_log("#{__method__}: #{$!}")
177
+ end
178
+
179
+ alias mark_test_level mark_testlevel
180
+
181
+ =begin rdoc
182
+ category: Logging
183
+ tags: log
184
+ =end
185
+ def info_to_log(message, lnbr = __LINE__)
186
+ log_message(INFO, message, nil, lnbr)
187
+ end
188
+
189
+ alias message_tolog info_to_log
190
+ alias message_to_log info_to_log
191
+ alias info_tolog info_to_log
192
+
193
+ =begin rdoc
194
+ category: Logging
195
+ tags: log, debug
196
+ =end
197
+ def debug_to_log(message, lnbr = __LINE__, dbg = false)
198
+ message << " \n#{get_debug_list}" if dbg or @debug_calls # and not @debug_calls_fail_only)
199
+ log_message(DEBUG, "#{message}", nil, lnbr)
200
+ end
201
+
202
+ alias debug_tolog debug_to_log
203
+
204
+ =begin rdoc
205
+ category: Logging
206
+ tags: log, error
207
+ Do not use for failed validations.
208
+ =end
209
+ def error_to_log(message, lnbr = __LINE__)
210
+ log_message(ERROR, message, nil, lnbr)
211
+ end
212
+
213
+ alias error_tolog error_to_log
214
+
215
+ =begin rdoc
216
+ category: Logging
217
+ tags: log, error, pass, reference, tag, report
218
+ =end
219
+ def passed_to_log(message, lnbr = __LINE__, dbg = false)
220
+ message << " \n#{get_debug_list}" if dbg or @debug_calls # and not @debug_calls_fail_only)
221
+ @my_passed_count += 1 if @my_passed_count
222
+ parse_error_references(message)
223
+ log_message(INFO, "#{message}", PASS, lnbr)
224
+ end
225
+
226
+ alias validate_passed_tolog passed_to_log
227
+ alias validate_passed_to_log passed_to_log
228
+ alias passed_tolog passed_to_log
229
+ alias pass_tolog passed_to_log
230
+ alias pass_to_log passed_to_log
231
+
232
+ =begin rdoc
233
+ category: Logging
234
+ tags: log, error, fail, reference, tag, report
235
+ =end
236
+ def failed_to_log(message, lnbr = __LINE__, dbg = false)
237
+ message << " \n#{get_debug_list}" # if dbg or @debug_calls or @debug_calls_fail_only
238
+ @my_failed_count += 1 if @my_failed_count
239
+ parse_error_references(message, true)
240
+ log_message(WARN, "#{message}" + " (#{lnbr})]", FAIL, lnbr)
241
+ #debugger if debug_on_fail
242
+ end
243
+
244
+ alias validate_failed_tolog failed_to_log
245
+ alias validate_failed_to_log failed_to_log
246
+ alias failed_tolog failed_to_log
247
+ alias fail_tolog failed_to_log
248
+ alias fail_to_log failed_to_log
249
+
250
+ =begin rdoc
251
+ category: Logging
252
+ tags: log, error, fail, reference, tag, fatal, report
253
+ =end
254
+ def fatal_to_log(message, lnbr = __LINE__, dbg = false)
255
+ message << " \n#{get_debug_list}" # if dbg or (@debug_calls and not @debug_calls_fail_only)
256
+ @my_failed_count += 1 if @my_failed_count
257
+ parse_error_references(message, true)
258
+ debug_to_report("#{__method__}:\n#{dump_caller(lnbr)}")
259
+ log_message(FATAL, "#{message} (#{lnbr})", FAIL, lnbr)
260
+ end
261
+
262
+ #def fatal_to_log(message, lnbr = __LINE__)
263
+ # log_message(FATAL, "#{message} (#{lnbr})", FAIL, lnbr)
264
+ # log_message(DEBUG, "\n#{dump_caller(lnbr)}")
265
+ #end
266
+
267
+ alias fatal_tolog fatal_to_log
268
+
269
+ =begin rdoc
270
+ category: Logging
271
+ tags: log, report
272
+ =end
273
+ def message_to_report(message, dbg = false)
274
+ mark_testlevel("#{message}", 0, '', dbg)
275
+ end
276
+
277
+ =begin rdoc
278
+ category: Logging
279
+ tags: log, debug, report
280
+ =end
281
+ def debug_to_report(message, dbg = false)
282
+ mark_testlevel("(DEBUG): \n", 0, "#{message}", dbg)
283
+ end
284
+
285
+ def translate_severity(severity)
286
+ mySev = ''
287
+ case
288
+ when severity == 0
289
+ mySev = 'DEBUG'
290
+ when severity == 1
291
+ mySev = 'INFO'
292
+ when severity == 2
293
+ mySev = 'WARN'
294
+ when severity == 3
295
+ mySev = 'ERROR'
296
+ when severity == 4
297
+ mySev = 'FATAL'
298
+ when severity > 4
299
+ mySev = 'UNKNOWN'
300
+ end
301
+ mySev
302
+ end
303
+
304
+ =begin rdoc
305
+ category: Debug
306
+ tags: log, caller, trace, report
307
+ =end
308
+ def get_caller(lnbr=nil, exception=nil)
309
+ script_name ||= File.basename(script_file)
310
+ if lnbr && script_type.eql?("Selenium")
311
+ [script_name, lnbr, 'in run()'].join(":")
312
+ elsif lnbr && script_type.eql?("MobileNativeApp")
313
+ [script_name, lnbr, 'in scenario()'].join(":")
314
+ else
315
+ caller_object = exception ? exception.backtrace : Kernel.caller
316
+ call_frame = caller_object.detect do |frame|
317
+ frame.match(/#{script_name}/) or (library && frame.match(/#{library}/))
318
+ end
319
+ unless call_frame.nil?
320
+ call_frame.gsub!(/^C:/, '')
321
+ file, line, method = call_frame.split(":")
322
+ [File.basename(file), line, method].join(":")
323
+ else
324
+ 'unknown'
325
+ end
326
+ end
327
+ end
328
+
329
+ def init_logger(logFile, scriptName = nil)
330
+ if File.exist?(logFile)
331
+ puts "==> Logfile already exists: #{logFile}. Replacing it."
332
+ begin
333
+ File.delete(logFile)
334
+ rescue
335
+ puts "#{scriptName}: init_logger RESCUE: #{$!}"
336
+ end
337
+ end
338
+ logger = ActiveSupport::BufferedLogger.new(logFile)
339
+ logger.level = Logger::DEBUG
340
+ logger.auto_flushing = (true)
341
+ logger.add(INFO, "#{logFile}\n#{ENV["OS"]}")
342
+ logger
343
+ end
344
+
345
+ #private init_logger
346
+
347
+ =begin rdoc
348
+ category: Logging
349
+ tags: error, fail, reference, tag
350
+ =end
351
+ def start_run(ts = nil)
352
+ @start_timestamp = Time.now unless ts
353
+ utc_ts = @start_timestamp.getutc
354
+ loc_tm = "#{@start_timestamp.strftime("%H:%M:%S")} #{@start_timestamp.zone}"
355
+ mark_testlevel(">> Starting #{@myName.titleize} #{utc_ts} (#{loc_tm})", 9)
356
+ end
357
+
358
+ alias start_to_log start_run
359
+
360
+ =begin rdoc
361
+ category: Logging
362
+ tags: log, begin, error, reference, validation, pass, fail, tallies, tag
363
+ =end
364
+ def finish_run(ts = nil)
365
+ timestamp = Time.now unless ts
366
+
367
+ mark_testlevel(">> #{@myName.titleize} duration: #{sec2hms(timestamp - @start_timestamp)}", 0)
368
+
369
+ mark_testlevel(">> #{@myName.titleize} validations: #{@my_passed_count + @my_failed_count} "+
370
+ "fail: #{@my_failed_count}]", 0) if @my_passed_count and @my_failed_count
371
+
372
+ tally_error_references
373
+
374
+ utc_ts = timestamp.getutc
375
+ loc_tm = "#{timestamp.strftime("%H:%M:%S")} #{timestamp.zone}"
376
+ mark_testlevel(">> End #{@myName.titleize} #{utc_ts} (#{loc_tm})", 9)
377
+
378
+ end
379
+
380
+ alias finish_to_log finish_run
381
+
382
+ =begin rdoc
383
+ category: Logging
384
+ tags: log, error, reference, tag, tallies
385
+ =end
386
+ def tally_error_references(list_tags = @report_all_refs)
387
+ tags_tested = 0
388
+ tags_hit = 0
389
+ if @my_error_hits and @my_error_hits.length > 0
390
+ mark_testlevel("Tagged Error Hits:", 0)
391
+ tags_hit = @my_error_hits.length
392
+ @my_error_hits.each_key do |ref|
393
+ mark_testlevel("#{ref} - #{@my_error_hits[ref]}", 0)
394
+ end
395
+ end
396
+ if list_tags
397
+ if @my_error_references and @my_error_references.length > 0
398
+ mark_testlevel("Error and Test Case Tags:", 0)
399
+ tags_tested = @my_error_references.length
400
+ @my_error_references.each_key do |ref|
401
+ mark_testlevel("#{ref} - #{@my_error_references[ref]}", 0)
402
+ end
403
+ mark_testlevel("Fails were hit on #{tags_hit} of #{tags_tested} error/test case references", 0)
404
+ else
405
+ mark_testlevel("No Error or Test Case References found.", 0)
406
+ end
407
+ end
408
+ end
409
+
410
+ =begin rdoc
411
+ category: Logging
412
+ tags: error, reference, tag, tallies
413
+ =end
414
+ def parse_error_references(message, fail = false)
415
+ msg = message.dup
416
+ while msg =~ /(\*\*\*\s+[\w\d_\s,-:;\?]+\s+\*\*\*)/
417
+ capture_error_reference($1, fail)
418
+ msg.sub!($1, '')
419
+ end
420
+ end
421
+
422
+ =begin rdoc
423
+ category: Logging
424
+ tags: error, fail, hits, reference, tag, tallies
425
+ =end
426
+ def capture_error_reference(ref, fail)
427
+ if fail
428
+ @my_error_hits = Hash.new unless @my_error_hits
429
+ if @my_error_hits[ref]
430
+ @my_error_hits[ref] += 1
431
+ else
432
+ @my_error_hits[ref] = 1
433
+ end
434
+ #debug_to_report("#{__method__}: error hits:\n#{@my_error_hits.to_yaml}")
435
+ end
436
+ @my_error_references = Hash.new unless @my_error_references
437
+ if @my_error_references[ref]
438
+ @my_error_references[ref] += 1
439
+ else
440
+ @my_error_references[ref] = 1
441
+ end
442
+ end
443
+
444
+ end