Class: SodaReporter

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

Overview

SodaReporter – Class

Params:

testfile: This is a soda XML test file.
savehtml: This tells Soda to save html files that caused an issue.
resultsdir: This is the directory where you want to store all results.
debug: Setting this to true will cause this class to print full debug 
   messages.

simple reporter class that tracks asserts, exceptions, and log messages

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(testfile, savehtml = false, resultsdir = nil, debug = 0, callback = nil, rerun = false) ⇒ SodaReporter

Returns a new instance of SodaReporter.



52
53
54
55
56
57
58
59
60
61
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
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
# File 'lib/SodaReporter.rb', line 52

def initialize(testfile, savehtml = false, resultsdir = nil, debug = 0,
      callback = nil, rerun = false)
   @sodatest_file = testfile
   @saveHtmlFiles = savehtml
   @debug = debug
   @start_time = nil
   @end_time = nil
   @start_time_obj = nil
   @end_time_obj = nil
   @js_error_count = 0
   @css_error_count = 0
   @asserts_count = 0
   @assertFails_count = 0
   @exception_count = 0 
   @test_count = 0
   @test_skip_count = 0
   @test_blocked_count = 0
   @test_failed_count = 0
   @test_passed_count = 0
   @test_watchdog_count = 0
   @test_warning_count = 0
   @test_total_count = 0
   @fatals = 0
   @total = 0
   @failureCount = 0
   @savedPages = 0
   @ResultsDir = "#{Dir.pwd}"
   @path = nil
   @htmllog_filename = nil
   @log_filename = nil
   @print_callback = callback
   hostname = "#{ENV['HOSTNAME']}"

   if (resultsdir != nil)
      @ResultsDir = resultsdir
   end 

   if ( (hostname.empty?) || (hostname.length < 5) )
      hostname = `hostname`
      hostname = hostname.chomp()
   end

   SodaUtils.PrintSoda("Debugging: => #{debug}\n")
   SodaUtils.PrintSoda("Soda Test File: => #{@sodatest_file}\n")
   base_testfile_name = File.basename(@sodatest_file, '.xml')

   if (rerun)
      base_testfile_name << "-SodaRerun"
   end

   now = Time.now().strftime("%d-%m-%Y-%H-%M")
  
   if (resultsdir == nil)
      @ResultsDir = @ResultsDir + "/#{base_testfile_name}-#{now}-results"
   end

   FileUtils.mkdir_p(@ResultsDir)
   @path = "#{@ResultsDir}/#{base_testfile_name}"
   
   if (File.exist?("#{@path}.log"))
      t = Time.now()
      t = t.strftime("%Y%m%d%H%M%S")
      base_testfile_name << "-#{t}"
      @path = "#{@ResultsDir}/#{base_testfile_name}"
   end

   if (@path =~ /sugarinit/i)
      @log_filename = "#{@path}-#{hostname}.log"
      @htmllog_filename = "#{@ResultsDir}/Report-#{base_testfile_name}"+
         "-#{hostname}.html"
   else
      @htmllog_filename = "#{@ResultsDir}/Report-#{base_testfile_name}.html"
      @log_filename = "#{@path}.log"
   end

   @logfile = File.new(@log_filename, 'w+')
   @logfile.sync = true # force buffers to write to disk asap! #
   SodaUtils.PrintSoda("Created log file: => #{@log_filename}\n")
   log("[New Test]\n")
   log("Starting soda test: #{@sodatest_file}\n")
   log("Saving HTML files => #{@saveHtmlFiles.to_s()}.\n")

   # this is because ruby 1.8.7 strftime doesn't support fractions of a
   # second.  Lame...
   now = Time.now()
   @start_time_obj = now
   time_str = now.strftime("%m/%d/%Y-%H:%M:%S")
   time_str = "#{time_str}.#{now.usec}"
   @start_time = time_str
end

Instance Attribute Details

#assertFails_countObject

Returns the value of attribute assertFails_count.



48
49
50
# File 'lib/SodaReporter.rb', line 48

def assertFails_count
  @assertFails_count
end

#asserts_countObject

Returns the value of attribute asserts_count.



48
49
50
# File 'lib/SodaReporter.rb', line 48

def asserts_count
  @asserts_count
end

#css_error_countObject

Returns the value of attribute css_error_count.



48
49
50
# File 'lib/SodaReporter.rb', line 48

def css_error_count
  @css_error_count
end

#exception_countObject

Returns the value of attribute exception_count.



48
49
50
# File 'lib/SodaReporter.rb', line 48

def exception_count
  @exception_count
end

#js_error_countObject

Returns the value of attribute js_error_count.



48
49
50
# File 'lib/SodaReporter.rb', line 48

def js_error_count
  @js_error_count
end

#test_blocked_countObject

Returns the value of attribute test_blocked_count.



48
49
50
# File 'lib/SodaReporter.rb', line 48

def test_blocked_count
  @test_blocked_count
end

#test_skip_countObject

Returns the value of attribute test_skip_count.



48
49
50
# File 'lib/SodaReporter.rb', line 48

def test_skip_count
  @test_skip_count
end

#test_watchdog_countObject

Returns the value of attribute test_watchdog_count.



48
49
50
# File 'lib/SodaReporter.rb', line 48

def test_watchdog_count
  @test_watchdog_count
end

Instance Method Details

#AddEventCountObject

AddEventCount – Method

This function incerments the internal event counter.

Params:

None.

Results:

None.


466
467
468
# File 'lib/SodaReporter.rb', line 466

def AddEventCount
   @total += 1
end

#Assert(exp, msg = "", file = "", line_number = "") ⇒ Object

Assert – Method

This method assert that the exp is equal to TRUE, and reports the results

Params:

exp: The expression to evaulate.
msg: The message to report about the assertion.

Results:

returns -1 on assert failed, or 0 on success.


604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/SodaReporter.rb', line 604

def Assert(exp, msg = "", file = "", line_number = "")
   result = 0
   @asserts_count += 1
   url = nil

   url = "#{$curSoda.browser.url}"
   if ( (url.empty?) || (url.length < 1) || (url == "") )
      url = "Unknown URL casued this Assert!"
   end

   if (file.empty?)
      file = "No file provided."
   end

   if (msg.empty?)
      msg = "No Assert message provided!"
   end 

   if ( (line_number == nil) || (line_number.empty?))
      line_number = "Unknown line number"
   end

   if (!exp)
      ass_msg = "Assertion: Failed!:--#{url}--#{file}" +
      "--Assertion Message: #{msg}--Line: #{line_number}"
      ass_msg = ass_msg.sub(/\n/,"")
      ass_msg << "\n"

      log(ass_msg, 1)
      SavePage(msg)
      @assertFails_count += 1
      result = -1
   else
      if (msg.empty?)
         log("Assertion: Passed.\n")
      else
         log("Assertion: Passed: #{msg}.\n")
      end
      result = 0
   end

   return result
end

#AssertNot(exp, msg = "", file = "") ⇒ Object

AssertNot – Method

This method asserts if the exp is equal to FALSE, and reports the results

Params:

exp: The expression to evaulate.
msg: The message to report with the assert.


658
659
660
# File 'lib/SodaReporter.rb', line 658

def AssertNot(exp, msg = "", file = "")
   Assert(!exp, msg, file)
end

#EndTestReportObject

EndTestReport – Method

This function is to be called after a test finishes running, so that the
proper formatting is do to allow for an easy to parse raw log file.

Params:

None.

Results:

None.


482
483
484
485
486
487
488
489
490
491
# File 'lib/SodaReporter.rb', line 482

def EndTestReport 
   log("Soda test: #{@sodatest_file} finished.\n")
   log("[End Test]\n")
   @logfile.close()

   now = Time.now()
   now_str = now.strftime("%m/%d/%Y-%H:%M:%S")
   @end_time_obj = now
   @end_time = "#{now_str}.#{now.usec}"
end

#GetRawResultsObject

GetRawResults – Method

This method gets test results data.

Input:

None.

Output:

returns a hash of test report data.


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/SodaReporter.rb', line 154

def GetRawResults()

   if (@end_time == nil)
      now = Time.now()
      @end_time_obj = now
      @end_time = now.strftime("%m/%d/%Y-%H:%M:%S")
      @end_time = "#{@end_time}.#{now.usec}"
   end

   total_time = (@end_time_obj - @start_time_obj)

   results = {
      'Test Failure Count' => @failureCount,
      'Test Assert Count' => @asserts_count,
      'Test JavaScript Error Count' => @js_error_count,
      'Test CSS Error Count' => @css_error_count,
      'Test Assert Failures' => @assertFails_count,
      'Test Exceptions' => @exception_count,
      'Test Skip Count' => @test_skip_count,
      'Test Blocked Count' => @test_blocked_count,
      'Test WatchDog Count' => @test_watchdog_count,
      'Test Warning Count' => @test_warning_count,
      'Test Event Count' => @total,
      'Test Start Time' => @start_time,
      'Test Stop Time' => @end_time,
      'Test Log File' => @log_filename
   }

   return results
end

#GetResultDirObject

GetResultDir – Method

This method returns the current result dir.

Input:

None.

Output:

returns the current result directory.


226
227
228
# File 'lib/SodaReporter.rb', line 226

def GetResultDir()
   return @ResultsDir
end

#IncBlockedTestObject

IncBlockedTest – Method

This method incerments the count by 1 for tests that were blocked.

Input:

None.

Output:

None.


273
274
275
# File 'lib/SodaReporter.rb', line 273

def IncBlockedTest()
   @test_blocked_count += 1
end

#IncFailedTestObject

IncFailedTest – Method

This method incerments the count by 1 for tests that failed.

Input:

None.

Output:

None.


289
290
291
# File 'lib/SodaReporter.rb', line 289

def IncFailedTest()
   @test_failed_count += 1
end

#IncSkippedTestObject

IncSkippedTest – Method

This method incerments the count by 1 for tests that were skipped.

Input:

None.

Output:

None.


241
242
243
# File 'lib/SodaReporter.rb', line 241

def IncSkippedTest()
   @test_skip_count += 1
end

#IncTestCountObject

IncTestCount – Method

This method incerments the count by 1 for tests that were ran

Input:

None.

Output:

None.


305
306
307
# File 'lib/SodaReporter.rb', line 305

def IncTestCount()
   @test_count += 1
end

#IncTestPassedCountObject

IncTestPassedCount – Method

This method incerments the count by 1 for tests that passed.

Input:

None.

Output:

None.


337
338
339
# File 'lib/SodaReporter.rb', line 337

def IncTestPassedCount()
   @test_passed_count += 1
end

#IncTestTotalCount(n = 1) ⇒ Object

IncTestTotalCount – Method

This method incerments the count by 1 for tests that were skipped.

Input:

n: the number to inc by, 1 is the default.

Output:

None.


257
258
259
# File 'lib/SodaReporter.rb', line 257

def IncTestTotalCount(n = 1)
   @test_total_count += 1
end

#IncTestWarningCountObject

IncTestWarningCount – Method

This method incerments the count by 1 for tests that were ran

Input:

None.

Output:

None.


321
322
323
# File 'lib/SodaReporter.rb', line 321

def IncTestWarningCount()
   @test_warning_count += 1
end

#IncTestWatchDogCountObject

IncTestWatchDogCount – Method

This method incerments the count by 1 for tests that watchdog'd.

Input:

None.

Output:

None.


353
354
355
# File 'lib/SodaReporter.rb', line 353

def IncTestWatchDogCount()
   @test_watchdog_count += 1
end

#log(msg, error = 0) ⇒ Object

log – Method

This method will log soda message to both stdout & the report's log file.

Params:

msg: This is a string message to log.

error: Default is false, setting to true will format the message as an
   error.

Results:

None.


545
546
547
548
# File 'lib/SodaReporter.rb', line 545

def log(msg, error = 0)
   SodaUtils.PrintSoda(msg, error, @logfile, @debug)
   SodaUtils.PrintSoda(msg, error, nil, @debug, 1, @print_callback)
end

#NFSRenameHack(old_file, new_file) ⇒ Object

NFSRenameHack – hack!!!

This is a total hack because of the very lame ass way hudson was setup
to run soda tests using an nfs mount as a writing point for test
results!!!  This hack will be taken out as soon as hudson is updated.


501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/SodaReporter.rb', line 501

def NFSRenameHack(old_file, new_file)
   err = false
   count = 0

   while (err != true)
      err = @logfile.closed?()
      count += 1
      sleep(1)
      break if (count > 20)
   end

   tmp_log = File.open(old_file, "r")
   new_log = File.new(new_file, "w+")
   line = nil
   while (line = tmp_log.gets)
      new_log.write(line)
   end
   tmp_log.close()
   new_log.close()

   # using this because on Windows File.unlink doesn't want to remove the
   # file.  There might be a file descripter hanging around but I can't
   # find it and it is working fine one linux.  This shoudl be looked into
   # at a later time...
   begin
      File.unlink(old_file)
   rescue Exception => e
   end
end

#ReportException(sodaException, file = false) ⇒ Object

ReportException – Method

This method reports an Soda Exceptions from the Soda class.

Params:

sodaException: This is the exception that is passed from Soda.
file: The soda test file that the exception was raised by durring the
   test.

Results:

None.

Notes:

The major param seems totally useless and was left over from the org
code from this file.  Soda.rb only ever calls this method with major.
So I will be killer this param soon...


568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/SodaReporter.rb', line 568

def ReportException(sodaException, file = false)
   msg = nil
   @exception_count += 1

   if (sodaException.message.empty?)
      msg = "No exception message found!"
   else
      msg = sodaException.message
   end

   if (file)
      log("Exception raised for file: #{file}\n", SodaUtils::ERROR)
   else
      log("Exception raised: #{msg}\n", SodaUtils::ERROR)
   end

   bt = "--Exception Backtrace: " + sodaException.backtrace.join("--") +
      "\n"
   btm = "--Exception Message: #{msg}\n"
   log("Exception raised for file: #{file}" + btm + bt, 
      SodaUtils::ERROR)
end

#ReportFailure(msg) ⇒ Object

ReportFailure – Method

This function reports and counts general Soda failures.

Params:

msg: This is the string failure message to be reported.

Results:

None.


391
392
393
394
# File 'lib/SodaReporter.rb', line 391

def ReportFailure(msg)
   @failureCount += 1
   log("#{msg}", SodaUtils::ERROR)
end

#ReportHTMLObject

ReportHTML – Method

This function will generate an html report from the raw soda log file.

Params:

None.

Results:

None.


369
370
371
372
373
374
375
376
377
378
# File 'lib/SodaReporter.rb', line 369

def ReportHTML
   slr = SodaLogReporter.new(@log_filename, @htmllog_filename)
   slr.GenerateReport()
   msg = "Created new html report: #{@htmllog_filename}\n"
   SodaUtils.PrintSoda(msg)

   if (@print_callback != nil)
      @print_callback.call(msg)
   end
end

#ReportJavaScriptError(msg, skipcssreport = false) ⇒ Object

ReportJavaScriptError – Method

This function reports and counts javascript failures.

Params:

msg: This is the string failure message to be reported. 
skipcssreport: This skipps reporting CSS errors.

Results:

Always returns 0.


408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/SodaReporter.rb', line 408

def ReportJavaScriptError(msg, skipcssreport = false)

   if ( (msg =~ /Cat::CSS\s+Parser/i) && (skipcssreport != true) )
      msg = msg.gsub(/javascript\s+error:/i, "Css Error:")
      log("#{msg}", SodaUtils::WARN)
      @css_error_count += 1
   elsif ( (msg =~ /Cat::CSS\s+Parser/i) && (skipcssreport != false) )
      @css_error_count += 1
      return 0
   else
      log("#{msg}", SodaUtils::ERROR)
      @js_error_count += 1
   end

   return 0
end

#SavePage(reason = "") ⇒ Object

SavePage – Method

This method saves an given HTML page from the browser for later use.

Params:

reason:  Just a string so when this methos is called the caller can
   give a reason in the log file.  I'd hope this is used to report
   errors.

Results:

always returns 0.


438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/SodaReporter.rb', line 438

def SavePage(reason = "")
   if (@saveHtmlFiles != true)
      return 0
   end

   @savedPages += 1
   save_file_name = "#{@path}page#{@savedPages}.html"
   page = File.new(save_file_name, 'w+')
   page.write($curSoda.browser.url + "\n<br>#{reason}\n<br>" + 
         $curSoda.browser.html)
   page.close()

   log("HTML Saved: #{save_file_name}\n")

   return 0
end

#SodaPrintCurrentReportObject

SodaPrintCurrentReport – Method

This method is used for printing out the current results of the report.

Params:

None.

Results:

None.


673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/SodaReporter.rb', line 673

def SodaPrintCurrentReport()
   msg = "Soda Test Report:" + 
      "--Test File:#{@sodatest_file}" +
      "--Test Failure Count:#{@failureCount}" +
      "--Test CSS Error Count:#{@css_error_count}" +
      "--Test JavaScript Error Count:#{@js_error_count}" +
      "--Test Assert Failures:#{@assertFails_count}" +
      "--Test Event Count:#{@total}" +
      "--Test Assert Count:#{@asserts_count}" +
      "--Test Exceptions:#{@exception_count}\n"
   log(msg)
end

#ZeroTestResultsObject

ZeroTestResults – Method

This method zero's out needed reported values for a test when running
a suite.  Really this is only needed until SugarCRM internally starts
using real suites and I can go back to undo the hack's put in place to
do suite reporting when using the --test option to run suites.

Input:

None.

Output:

None.


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/SodaReporter.rb', line 199

def ZeroTestResults()
      @asserts_count = 0
      @js_error_count = 0
      @css_error_count = 0
      @assertFails_count = 0
      @exception_count = 0
      @test_skip_count = 0
      @test_blocked_count = 0
      @test_watchdog_count = 0
      @test_warning_count = 0
      @total = 0
      @start_time = nil
      @end_time = nil
      @failureCount = 0
end