Class: GreenPepper::HtmlDocumentWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/greenpepper/writer/htmldocwriter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_html) ⇒ HtmlDocumentWriter

Returns a new instance of HtmlDocumentWriter.



18
19
20
21
22
23
24
25
# File 'lib/greenpepper/writer/htmldocwriter.rb', line 18

def initialize(base_html)
  @html_doc = (LibXML::XML::HTMLParser.string base_html, 
         :encoding => LibXML::XML::Encoding::UTF_8,
         :options => LibXML::XML::HTMLParser::Options::NOBLANKS).parse
  @table_index = 0
  @modified_doc = ""
  @tables = @html_doc.find '//table'
end

Instance Attribute Details

#modified_docObject (readonly)

Returns the value of attribute modified_doc.



17
18
19
# File 'lib/greenpepper/writer/htmldocwriter.rb', line 17

def modified_doc
  @modified_doc
end

Instance Method Details

#write(results) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/greenpepper/writer/htmldocwriter.rb', line 27

def write(results)
  table = @tables[@table_index]
  w = create_writer results, table
  @modified_doc = (w.write results)
  @modified_doc = @modified_doc.doc.root.to_s unless @modified_doc == nil

  @table_index += w.table_count
  @modified_doc
end

#write_results(all_results) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/greenpepper/writer/htmldocwriter.rb', line 37

def write_results(all_results)
  stats_doc = ""

  # We define another XML tree, since <table> is already the node of
  # html_doc. We can't add another table to the actual one
  stats = LibXML::XML::Parser.string(
    "<table><tr><td>Successes: #{all_results.successes}</td>" +
    "<td>Failures: #{all_results.failures}</td>" +
    "<td>Errors: #{all_results.errors}</td></tr></table>").
    parse.root.copy(true)
    

  node = @html_doc.find_first 'body'
  node = @html_doc.root if node == nil
  if node.first?
    node.first.prev = stats
  else
    node << stats
  end
  @modified_doc = @html_doc.to_s
end