Class: ChemistryKit::RSpec::HtmlFormatter

Inherits:
RSpec::Core::Formatters::BaseTextFormatter
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/chemistrykit/rspec/html_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ HtmlFormatter

Returns a new instance of HtmlFormatter.



14
15
16
17
18
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 14

def initialize(output)
  super(output)
  @example_group_number = 0
  @example_number = 0
end

Instance Method Details

#build_fragmentObject

def extra_failure_content(exception)

super + "<h1>Ya'll know we be failing.</h1>"

end



242
243
244
245
246
247
248
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 242

def build_fragment
  final = Nokogiri::HTML::DocumentFragment.parse ''
  Nokogiri::HTML::Builder.with(final) do |doc|
    yield doc
  end
  final.to_html
end

#dump_failuresObject



219
220
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 219

def dump_failures
end

#dump_pendingObject



222
223
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 222

def dump_pending
end

#dump_summary(duration, example_count, failure_count, pending_count) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 225

def dump_summary(duration, example_count, failure_count, pending_count)
  output = build_fragment do |doc|
    doc.div(
      class: 'results',
      'data-count' => example_count.to_s,
      'data-duration' => duration.to_s,
      'data-failures' => failure_count.to_s,
      'data-pendings' => pending_count.to_s
      ) { doc << @output_html }
  end
  @output.puts output
end

#example_failed(example) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 74

def example_failed(example)
  super(example)
  exception = example.[:execution_result][:exception]
  @example_group_status = 'failing'
  @example_group_html += render_example('failing', example) do |doc|
    doc.div(class: 'row exception') do
      doc.div(class: 'large-12 columns') do
        doc.pre do
          message = exception.message if exception
          doc.text message
        end
      end
    end
    doc.div(class: 'row code-snippet') do
      doc.div(class: 'large-12 columns') do
        doc << render_code(exception)
      end
    end
    doc << render_extra_content(example)
  end
end

#example_group_finished(example_group) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 35

def example_group_finished(example_group)
  @output_html << build_fragment do |doc|
    show = @example_group_status == 'passing' ? 'show' : ''
    doc.div(class: "row example-group #{@example_group_status} #{show}") do
      doc.div(class: 'large-12 columns') do
        doc.h3 do
          doc.i(class: 'icon-beaker')
          doc.text ' ' + example_group.description
        end
        doc.div(class: 'examples') do
          doc << @example_group_html
        end
      end
    end
  end
end

#example_group_started(example_group) ⇒ Object



28
29
30
31
32
33
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 28

def example_group_started(example_group)
  @example_group = example_group
  @example_group_html = ''
  @example_group_number += 1
  @example_group_status = 'passing'
end

#example_passed(example) ⇒ Object



57
58
59
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 57

def example_passed(example)
  @example_group_html += render_example('passing', example) {}
end

#example_pending(example) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 61

def example_pending(example)
  super(example)
  @example_group_html += render_example('pending', example) do |doc|
    doc.div(class: 'row exception') do
      doc.div(class: 'large-12 columns') do
        doc.pre do
          doc.text "PENDING: #{example.[:execution_result][:pending_message]}"
        end
      end
    end
  end
end

#example_started(example) ⇒ Object



52
53
54
55
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 52

def example_started(example)
  super(example)
  @example_number += 1
end

#message(message) ⇒ Object



20
21
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 20

def message(message)
end

#render_code(exception) ⇒ Object



177
178
179
180
181
182
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 177

def render_code(exception)
  backtrace = exception.backtrace.map { |line| backtrace_line(line) }
  backtrace.compact!
  @snippet_extractor ||= ::RSpec::Core::Formatters::SnippetExtractor.new
  "<pre class=\"ruby\"><code>#{@snippet_extractor.snippet(backtrace)}</code></pre>"
end

#render_dom_html_if_found(example) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 116

def render_dom_html_if_found(example)
  # TODO: pull out the common code for checking if the log file exists
  beaker_folder = slugify(@example_group.description)
  example_folder = slugify(@example_group.description + '_' + example.description)
  paths = Dir.glob(File.join(Dir.getwd, 'evidence', beaker_folder, example_folder, 'dom_*.html'))
  number = 0
  sections = ''
  paths.each do |path|
    if File.exist?(path)
      sections << render_section("Dom HTML #{number}") do |doc|
        doc << Pygments.highlight(File.read(path), lexer: 'html')
      end
      number += 1
    end
  end
  sections
end

#render_example(status, example) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 198

def render_example(status, example)
  build_fragment do |doc|
    show = status == 'passing' ? 'hide' : ''
    doc.div(class: "row example #{status} #{show}") do
      doc.div(class: 'large-12 columns') do
        doc.div(class: 'row example-heading') do
          doc.div(class: 'large-9 columns') do
            doc.p { doc.text example.description.capitalize }
          end
          doc.div(class: 'large-3 columns text-right') do
            doc.p { doc.text sprintf('%.0i', example.execution_result[:run_time]) + 's' }
          end
        end
        doc.div(class: 'row example-body') do
          doc.div(class: 'large-12 columns') { yield doc }
        end
      end
    end
  end
end

#render_extra_content(example) ⇒ Object

TODO: put the right methods private, or better yet, pull this stuff out into its own set of classes



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 98

def render_extra_content(example)
  build_fragment do |doc|
    doc.div(class: 'row extra-content') do
      doc.div(class: 'large-12 columns') do
        doc.div(class: 'section-container auto', 'data-section' => '') do
          doc << render_failshot_if_found(example)
          doc << render_stack_trace(example)
          doc << render_log_if_found(example, 'server.log')
          doc << render_log_if_found(example, 'chromedriver.log')
          doc << render_log_if_found(example, 'firefox.log')
          doc << render_log_if_found(example, 'sauce_job.log')
          doc << render_dom_html_if_found(example)
        end
      end
    end
  end
end

#render_failshot_if_found(example) ⇒ Object

TODO: replace the section id with a uuid or something.…



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 135

def render_failshot_if_found(example)
  beaker_folder = slugify(@example_group.description)
  example_folder = slugify(@example_group.description + '_' + example.description)

  path = File.join(Dir.getwd, 'evidence', beaker_folder, example_folder, 'failshot.png')
  if File.exist?(path)
    render_section('Failure Screenshot') do |doc|
       # if this is a jenkins job this variable is set and we can use it to get the right path to the images
      if ENV['JOB_NAME']
        path = File.join("/job/#{ENV['JOB_NAME']}/ws", 'evidence', beaker_folder, example_folder, 'failshot.png')
      end
      doc.img(src: path)
    end
  end
end

#render_log_if_found(example, log) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 151

def render_log_if_found(example, log)
  beaker_folder = slugify(@example_group.description)
  example_folder = slugify(@example_group.description + '_' + example.description)
  log_path = File.join(Dir.getwd, 'evidence', beaker_folder, example_folder, log)
  if File.exist?(log_path)
    render_section(log.capitalize) do |doc|
      doc.pre do
        doc.text File.open(log_path, 'rb') { |file| file.read }
      end
    end
  end
end

#render_section(title) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 184

def render_section(title)
  panel_id = SecureRandom.uuid
  build_fragment do |doc|
    doc.section do
      doc.p(class: 'title', 'data-section-title' => '') do
        doc.a(href: "#panel#{panel_id}") { doc.text title }
      end
      doc.div(class: 'content', 'data-section-content' => '') do
        yield doc
      end
    end
  end
end

#render_stack_trace(example) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 168

def render_stack_trace(example)
  exception = example.[:execution_result][:exception]
  render_section('Stack Trace') do |doc|
    doc.pre do
      doc.text format_backtrace(exception.backtrace, example).join("\n")
    end
  end
end

#slugify(string) ⇒ Object



164
165
166
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 164

def slugify(string)
  string.downcase.strip.gsub(' ', '_').gsub(/[^\w-]/, '')
end

#start(example_count) ⇒ Object



23
24
25
26
# File 'lib/chemistrykit/rspec/html_formatter.rb', line 23

def start(example_count)
  super(example_count)
  @output_html = ''
end