-
Notifications
You must be signed in to change notification settings - Fork 0
/
results_XML_handler.rb
74 lines (64 loc) · 2.58 KB
/
results_XML_handler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
# This XML handler converges all JUnit XML reports produced by the
# cucumber scripts into a single XML file suitable for HTML report generation.
# An XSLT file is then applied to generate a summarized HTML report in JUnit format.
# Maintainer: ruifengm@sg.ibm.com
# Date: 2016-July-05
require 'nokogiri'
require 'rexml/document'
include REXML
result_directory = "#{Dir.home}/#{ENV['APP_BUILD']}/#{ENV['TEST_PHASE']}/cucumber-result"
junit_directory = "#{result_directory}/junit"
log_directory = "#{result_directory}/logs"
report_directory = "#{result_directory}/cuke-report"
xml_all_doc = Document.new
# Add XML declaration
xml_all_doc << XMLDecl.new
# Add element tree
testsuites = Element.new "testsuites"
testsuite_id = 0
testsuites.add_attribute("logs", (log_directory + "/").gsub("#{Dir.home}", ""))
Dir["#{junit_directory}/TEST-*.xml"].each do |file_path|
xml_file = File.new(file_path)
xml_doc = Document.new(xml_file)
xml_doc.elements.each("testsuite") do |e|
# Add useful attributes
feature_name = e.attribute("name").value
e.add_attribute("id", testsuite_id)
e.add_attribute("package", feature_name)
log_link = String.new
Dir["#{log_directory}/*cuke_trace.log"].each do |file_path|
# if file_path =~ Regexp.new(feature_name.gsub(/\s+/, "-"), Regexp::IGNORECASE)
if file_path.split("/")[-1].gsub("-cuke_trace.log", "") == feature_name.gsub(/\s+/, "-")
log_link = file_path.gsub("#{Dir.home}", "")
break
end
end
e.add_attribute("log", log_link)
report_link = String.new
Dir["#{report_directory}/*.html"].each do |file_path|
# if file_path =~ Regexp.new(feature_name.gsub(/\s+/, "-"), Regexp::IGNORECASE)
if file_path.split("/")[-1].gsub(".html", "") == feature_name.gsub(/\s+/, "-")
report_link = file_path.gsub("#{Dir.home}", "")
break
end
end
e.add_attribute("cuke_report", report_link)
# Add test properties
properties = Element.new "properties"
property = Element.new "property"
property.add_attribute("name", "test_env")
property.add_attribute("value", "docker-cucumber")
properties.add_element property
e.add_element properties
testsuites.add_element e
testsuite_id = testsuite_id + 1
end
end
xml_all_doc.add_element testsuites
# Write summary XML file
xml_all_doc.write(:output => File.open("#{result_directory}/summary.xml", "w"), :indent => 2)
# Write summary HTML file
document = Nokogiri::XML(xml_all_doc.to_s)
template = Nokogiri::XSLT(File.read('junit-noframe.xsl'))
transformed_document = template.transform(document)
File.open("#{result_directory}/summary.html", "w").write(transformed_document)