Class: Res::Parsers::Junitcasper

Inherits:
Object
  • Object
show all
Defined in:
lib/res/parsers/junitcasper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(junit_xml) ⇒ Junitcasper

Returns a new instance of Junitcasper.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/res/parsers/junitcasper.rb', line 10

def initialize(junit_xml)
  file = File.open(junit_xml, "rb")
  begin
    junit = Ox.parse(file.read)
  rescue Ox::ParseError => e
    raise "Invalid xunit XML format. Error: #{e}"
  end
  file.close
  @test_suites = create_test_suites(packagenames(junit))
  junit.nodes.collect do |test_suites|
    create_multiple_test_suite(test_suites)
  end
  ir = ::Res::IR.new(:type => 'Casper',
                     :started => Time.now(),
                     :finished => Time.now(),
                     :results => @test_suites
  )

  @io = File.open("./junitcasper.res", "w")
  @io.puts ir.json
  @io.close
end

Instance Attribute Details

#ioObject

Returns the value of attribute io.



8
9
10
# File 'lib/res/parsers/junitcasper.rb', line 8

def io
  @io
end

Instance Method Details

#create_multiple_test_suite(suites) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/res/parsers/junitcasper.rb', line 52

def create_multiple_test_suite(suites)
  i = 0
  suites.nodes.each_with_index do |suite, index|
    test_suite = Hash.new
    test_suite[:type] = "Casper::testsuite"
    test_suite[:name] = suite.attributes[:name]
    test_suite[:children] = Array.new
    test_suite[:children] = test_case(suite)
    i = @test_suites.index { |test_suites|
      test_suites[:name] == suite.attributes[:package]
    }
    @test_suites[i][:children].push(test_suite)
  end
end

#create_test_suites(pkgnames) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/res/parsers/junitcasper.rb', line 67

def create_test_suites (pkgnames)
  test_suites= Array.new
  pkgnames.each_with_index do |pkgname, index|
    test_suites[index] = Hash.new
    test_suites[index][:type]= "Casper::testsuites"
    test_suites[index][:name]= pkgname
    test_suites[index][:children] = Array.new
  end
  test_suites
end

#packagenames(junit) ⇒ Object



79
80
81
82
83
84
# File 'lib/res/parsers/junitcasper.rb', line 79

def packagenames (junit)
  packagename=junit.nodes[0].nodes.collect do |node|
    node.attributes[:package]
  end
  packagename.uniq
end

#test_case(suite) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/res/parsers/junitcasper.rb', line 33

def test_case(suite)
  testcases = Hash.new
  testcases = suite.nodes.collect do |node|
    if node.value == "testcase"
      testcase = Hash.new
      testcase[:type] = "Casper::" + node.value
      testcase[:name] = truncateName(node.attributes[:name])
      testcase["duration"] = node.attributes[:time]
      testcase["status"] = "passed"
      if node.nodes[0] != nil
        testcase["status"] = "failed" if node.nodes[0].value == "failure" or node.nodes[0].value == "error"
        testcase["status"] = "notrun" if node.nodes[0].value == "skipped"
      end
      testcase
    end
  end
  testcases.compact
end

#truncateName(inputText) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/res/parsers/junitcasper.rb', line 86

def truncateName(inputText)
  if inputText.to_s.empty? ==false
    return inputText[0..253]#.gsub(/\s+$/, '')
  else
    return inputText
  end
end