Class: Sapphire::Testing::HtmlReporter

Inherits:
Reporter show all
Defined in:
lib/sapphire/Testing/HtmlReporter.rb

Instance Attribute Summary collapse

Attributes inherited from Reporter

#broken_count, #failing_count, #passing_count, #pending_count, #test_count, #time

Instance Method Summary collapse

Constructor Details

#initializeHtmlReporter

Returns a new instance of HtmlReporter.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 10

def initialize()
  @failures = []
  @example_group_number = 0
  @passing_count = 0
  @failing_count = 0
  @pending_count = 0
  @broken_count = 0
  @test_count = 0
  $output ||= $stdout
  @output = $output
  @file = ""
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



8
9
10
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 8

def file
  @file
end

Instance Method Details

#BeginTestingObject



116
117
118
119
120
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 116

def BeginTesting
  @start = Time.now
  @output.puts html_header
  @output.puts report_header
end

#file_path(file_name) ⇒ Object



446
447
448
449
450
451
452
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 446

def file_path(file_name)
  extension = File.extname(file_name)
  basename = File.basename(file_name, extension)
  dir = File.dirname(@file)
  file_path = File.join(dir, "", "#{basename}_#{Time.now.strftime("%H%M%S")}#{extension}")
  file_path
end

#global_scriptsObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 201

def global_scripts
  <<-EOF
function addClass(element_id, classname) {
  document.getElementById(element_id).className += (" " + classname);
}

function removeClass(element_id, classname) {
  var elem = document.getElementById(element_id);
  var classlist = elem.className.replace(classname,'');
  elem.className = classlist;
}

function moveProgressBar(percentDone) {
  document.getElementById("rspec-header").style.width = percentDone +"%";
}

function makeRed(element_id) {
  removeClass(element_id, 'passed');
  removeClass(element_id, 'not_implemented');
  removeClass(element_id, 'broken');
  addClass(element_id,'failed');
}

function makeYellow(element_id) {
  if (element_id == "rspec-header" && document.getElementById(element_id).style.background != '#C40D0D')
  {
    document.getElementById(element_id).style.background = '#FAF834';
    document.getElementById(element_id).style.color = '#000000';
  }
  else
  {
    document.getElementById(element_id).style.background = '#FAF834';
    document.getElementById(element_id).style.color = '#000000';
  }
}

function makeOrange(element_id) {
  if (element_id == "rspec-header" && document.getElementById(element_id).style.background != '#C40D0D')
  {
    document.getElementById(element_id).style.background = '#FF8000';
    document.getElementById(element_id).style.color = '#000000';
  }
  else
  {
    document.getElementById(element_id).style.background = '#FF8000';
    document.getElementById(element_id).style.color = '#000000';
  }
}

function apply_filters() {
  var passed_filter = document.getElementById('passed_checkbox').checked;
  var failed_filter = document.getElementById('failed_checkbox').checked;
  var pending_filter = document.getElementById('pending_checkbox').checked;
  var broken_filter = document.getElementById('broken_checkbox').checked;

  assign_display_style("spec passed", passed_filter);
  assign_display_style("spec failed", failed_filter);
  assign_display_style("spec not_implemented", pending_filter);
  assign_display_style("spec broken", broken_filter);

}

function get_display_style(display_flag) {
  var style_mode = 'none';
  if (display_flag == true) {
    style_mode = 'block';
  }
  return style_mode;
}

function assign_display_style(classname, display_flag) {
  var style_mode = get_display_style(display_flag);
  var elems = document.getElementsByClassName(classname)
  for (var i=0; i<elems.length;i++) {
    elems[i].style.display = style_mode;
  }
}

function assign_display_style_for_group(classname, display_flag, subgroup_flag) {
  var display_style_mode = get_display_style(display_flag);
  var subgroup_style_mode = get_display_style(subgroup_flag);
  var elems = document.getElementsByClassName(classname)
  for (var i=0; i<elems.length;i++) {
    var style_mode = display_style_mode;
    if ((display_flag != subgroup_flag) && (elems[i].getElementsByTagName('dt')[0].innerHTML.indexOf(", ") != -1)) {
      elems[i].style.display = subgroup_style_mode;
    } else {
      elems[i].style.display = display_style_mode;
    }
  }
}
EOF
end

#global_stylesObject



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 295

def global_styles
  <<-EOF
#rspec-header {
  background: #65C400; color: #fff; height: 4em;
}

.rspec-report h1 {
  margin: 0px 10px 0px 10px;
  padding: 10px;
  font-family: "Lucida Grande", Helvetica, sans-serif;
  font-size: 1.8em;
  position: absolute;
}

#summary {
  margin: 0; padding: 5px 10px;
  font-family: "Lucida Grande", Helvetica, sans-serif;
  text-align: right;
  top: 0px;
  right: 0px;
  float:right;
}

#summary p {
  margin: 0 0 0 2px;
}

#summary #totals {
  font-size: 1.2em;
}

.example_group {
  margin: 0 10px 5px;
  background: #fff;
}

dl {
  margin: 0; padding: 0 0 5px;
  font: normal 11px "Lucida Grande", Helvetica, sans-serif;
}

dt {
  padding: 3px;
  background: #65C400;
  color: #fff;
  font-weight: bold;
}

dd {
  margin: 5px 0 5px 5px;
  padding: 3px 3px 3px 18px;
}

dd.spec.passed {
  border-left: 5px solid #65C400;
  border-bottom: 1px solid #65C400;
  background: #DBFFB4; color: #3D7700;
}

dd.spec.failed {
  border-left: 5px solid #C20000;
  border-bottom: 1px solid #C20000;
  color: #C20000; background: #FFFBD3;
}

dd.spec.not_implemented {
  border-left: 5px solid #FAF834;
  border-bottom: 1px solid #FAF834;
  background: #FCFB98; color: #131313;
}

dd.spec.broken {
  border-left: 5px solid #FF8000;
  border-bottom: 1px solid #FF8000;
  background: #FCFB98; color: #131313;
}

dd.spec.pending_fixed {
  border-left: 5px solid #0000C2;
  border-bottom: 1px solid #0000C2;
  color: #0000C2; background: #D3FBFF;
}

.backtrace {
  color: #000;
  font-size: 12px;
}

a {
  color: #BE5C00;
}

/* Ruby code, style similar to vibrant ink */
.ruby {
  font-size: 12px;
  font-family: monospace;
  color: white;
  background-color: black;
  padding: 0.1em 0 0.2em 0;
}

#display-filters {
  float:left;
  padding: 28px 0 0 40%;
  font-family: "Lucida Grande", Helvetica, sans-serif;
}

.ruby .keyword { color: #FF6600; }
.ruby .constant { color: #339999; }
.ruby .attribute { color: white; }
.ruby .global { color: white; }
.ruby .module { color: white; }
.ruby .class { color: white; }
.ruby .string { color: #66FF00; }
.ruby .ident { color: white; }
.ruby .method { color: #FFCC00; }
.ruby .number { color: white; }
.ruby .char { color: white; }
.ruby .comment { color: #9933CC; }
.ruby .symbol { color: white; }
.ruby .regex { color: #44B4CC; }
.ruby .punct { color: white; }
.ruby .escape { color: white; }
.ruby .interp { color: white; }
.ruby .expr { color: white; }

.ruby .offending { background-color: gray; }
.ruby .linenum {
  width: 75px;
  padding: 0.1em 1em 0.2em 0;
  color: #000000;
  background-color: #FFFBD3;
}
EOF
end

#html_headerObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 142

def html_header
  <<-EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>RSpec results</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Expires" content="-1" />
  <meta http-equiv="Pragma" content="no-cache" />
  <style type="text/css">
  body {
    margin: 0;
    padding: 0;
    background: #fff;
    font-size: 80%;
  }
  </style>
  <script type="text/javascript">
    // <![CDATA[
#{global_scripts}
    // ]]>
  </script>
  <style type="text/css">
#{global_styles}
  </style>
</head>
<body>
EOF
end

#Indent(test) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 27

def Indent(test)
  indentation = ""

  count = 0

  count = 1 if test.item.is_a? When or (test.item.is_a? And and test.parent.item.is_a? When)
  count = 2 if test.item.is_a? Then or (test.item.is_a? And and test.parent.item.is_a? Then)

  count.times do
    indentation << "<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>"
  end

  indentation
end


454
455
456
457
458
459
460
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 454

def link_for(file_name)
  return unless file_name && File.exists?(file_name)

  path = Pathname.new(file_name)

  "<a href='#{File.basename(path)}'>Screenshot</a>&nbsp;"
end

#OutputResultsObject



134
135
136
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 134

def OutputResults()

end

#report_headerObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 175

def report_header
  <<-EOF
<div class="rspec-report">

<div id="rspec-header">
  <div id="label">
    <h1>Sapphire Test Results</h1>
  </div>

  <div id="display-filters">
    <input id="passed_checkbox" name="passed_checkbox" type="checkbox" checked="" onchange="apply_filters()" value="1"> <label for="passed_checkbox">Passed</label>
    <input id="failed_checkbox" name="failed_checkbox" type="checkbox" checked="" onchange="apply_filters()" value="2"> <label for="failed_checkbox">Failed</label>
    <input id="pending_checkbox" name="pending_checkbox" type="checkbox" checked="" onchange="apply_filters()" value="3"> <label for="pending_checkbox">Pending</label>
    <input id="broken_checkbox" name="broken_checkbox" type="checkbox" checked="" onchange="apply_filters()" value="4"> <label for="broken_checkbox">Broken</label>
  </div>

  <div id="summary">
    <p id="totals">&nbsp;</p>
    <p id="duration">&nbsp;</p>
  </div>
</div>

<div class="results">
EOF
end

#save_screenshotObject



431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 431

def save_screenshot
  if(!$driver)
    return
  end

  begin
    file_name = file_path("image.png")
    $driver.Screenshot(file_name)
  rescue => e
    $stderr.puts "saving of screenshot failed."
    $stderr.puts e.backtrace
  end
  file_name
end

#ScenarioComplete(scenario) ⇒ Object



138
139
140
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 138

def ScenarioComplete(scenario)

end

#ScenarioStart(scenario) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 122

def ScenarioStart(scenario)
  @example_group_red = false
  @example_group_number += 1
  unless @example_group_number == 1
    @output.puts "  </dl>"
    @output.puts "</div>"
  end
  @output.puts "<div class=\"example_group\">"
  @output.puts "  <dl>"
  @output.puts "  <dt id=\"example_group_#{@example_group_number}\">#{scenario.text} - #{scenario.file_name}</dt>"
end

#TestBroken(test) ⇒ Object



94
95
96
97
98
99
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 94

def TestBroken(test)
  @broken_count = @broken_count + 1
  @output.puts "    <script type=\"text/javascript\">makeOrange('rspec-header');</script>" unless @header_red
  @output.puts "    <script type=\"text/javascript\">makeOrange('example_group_#{@example_group_number}');</script>" unless @example_group_red
  @output.puts "    <dd class=\"spec broken\"><span class=\"broken_spec_name\">#{Indent(test) + test.text} (BROKEN: ### Broken ###)</span></dd>"
end

#TestFailed(test) ⇒ Object



47
48
49
50
51
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
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 47

def TestFailed(test)

  @failing_count = @failing_count + 1
  failure_style = 'failed'
  @output.puts "    <script type=\"text/javascript\">makeRed('rspec-header');</script>" unless @header_red
  @header_red = true
  @output.puts "    <script type=\"text/javascript\">makeRed('example_group_#{@example_group_number}');</script>" unless @example_group_red
  @example_group_red = true
  @output.puts "    <dd class=\"spec #{failure_style}\">"
  @output.puts "      <span class=\"failed_spec_name\">#{Indent(test) + test.text}</span>"
  @output.puts "      <div class=\"failure\" id=\"failure_#{@test_count}\">"

  if test.messages.is_a? Array
    message_block = ""
    test.messages.each do |message|
      message_block += Indent(test) + message + "<br>"
    end
    @output.puts "        <div class=\"message\"><pre>#{message_block}</pre></div>"
  else
    @output.puts "        <div class=\"message\"><pre>#{Indent(test) + test.messages}</pre></div>"
  end

  test.stack.each do |line|
    if (!line.include? "sapphire" and ! line.include? "-e:1:in")
      @output.puts "        <div class=\"backtrace\"><pre>#{Indent(test) + line}</pre></div>"
    end
  end

  content = []
  content << "<span>"

  file_name = save_screenshot
  content << Indent(test) + link_for(file_name)

  content << "</span>"
  @output.puts content
  @output.puts "      </div>"
  @output.puts "    </dd>"
end

#TestingCompleteObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 101

def TestingComplete
  @end = Time.now

  totals = "#{@test_count} example#{'s' unless @test_count == 1}, #{@failing_count} failure#{'s' unless @failing_count == 1}"
  totals << ", #{@pending_count} pending" if @pending_count > 0
  totals << ", #{@broken_count} broken" if @broken_count > 0

  @output.puts "<script type=\"text/javascript\">document.getElementById('duration').innerHTML = \"Finished in <strong>#{(@end - @start).round(2).to_s} seconds</strong>\";</script>"
  @output.puts "<script type=\"text/javascript\">document.getElementById('totals').innerHTML = \"#{totals}\";</script>"
  @output.puts "</div>"
  @output.puts "</div>"
  @output.puts "</body>"
  @output.puts "</html>"
end

#TestPassed(test) ⇒ Object



42
43
44
45
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 42

def TestPassed(test)
  @passing_count = @passing_count + 1
  @output.puts "    <dd class=\"spec passed\"><span class=\"passed_spec_name\">#{Indent(test) + test.text}</span></dd>"
end

#TestPending(test) ⇒ Object



87
88
89
90
91
92
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 87

def TestPending(test)
  @pending_count = @pending_count + 1
  @output.puts "    <script type=\"text/javascript\">makeYellow('rspec-header');</script>" unless @header_red
  @output.puts "    <script type=\"text/javascript\">makeYellow('example_group_#{@example_group_number}');</script>" unless @example_group_red
  @output.puts "    <dd class=\"spec not_implemented\"><span class=\"not_implemented_spec_name\">#{Indent(test) + test.text} (PENDING: ### Not Yet Implemented ###)</span></dd>"
end

#TestStarted(test) ⇒ Object



23
24
25
# File 'lib/sapphire/Testing/HtmlReporter.rb', line 23

def TestStarted(test)
  @test_count = @test_count + 1
end