Class: ReportPortal::Cucumber::JSONSlurper

Inherits:
Object
  • Object
show all
Defined in:
lib/report_portal/cucumber/json_slurper.rb

Overview

Helper class to parse and post test launch data generated by Cucumber’s JSON formatter Supports ONLY reports generated with -x switch (i.e. expanded outlines)

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ JSONSlurper

Returns a new instance of JSONSlurper.



31
32
33
34
35
# File 'lib/report_portal/cucumber/json_slurper.rb', line 31

def initialize(filename)
  @json = JSON.parse(File.read(filename))
  fail "Cannot process #{filename} because it was generated without -x/--expand Cucumber option!" unless expanded?
  calculate_start_time
end

Instance Method Details

#runObject



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/report_portal/cucumber/json_slurper.rb', line 37

def run
  ReportPortal.start_launch('', get_time)

  @json.each do |feature|
    current_feature = ReportPortal::TestItem.new("Feature: #{feature['name']}",
                                                 :TEST,
                                                 nil,
                                                 get_time,
                                                 feature['uri'],
                                                 nil,
                                                 tags(feature))
    current_feature_node = Tree::TreeNode.new(SecureRandom.hex, current_feature)
    current_feature.id = ReportPortal.start_item(current_feature_node)
    current_element_name = nil
    current_outline_row = 0

    feature['elements'].each do |element|
      type = element['type'] == 'background' ? :BEFORE_CLASS : :STEP

      element_name = "#{element['keyword']}: #{element['name']}"
      if element['keyword'] == 'Scenario Outline'
        if element['name'] == current_element_name
          current_outline_row += 1
        else
          current_element_name = element['name']
          current_outline_row = 1
        end
        element_name << " [#{current_outline_row}]"
      end

      ReportPortal.current_scenario = ReportPortal::TestItem.new(element_name,
                                                                 type,
                                                                 nil,
                                                                 get_time,
                                                                 "#{feature['uri']}:#{element['line']}",
                                                                 nil,
                                                                 tags(element))
      current_scenario_node = Tree::TreeNode.new(SecureRandom.hex, ReportPortal.current_scenario)
      ReportPortal.current_scenario.id = ReportPortal.start_item(current_scenario_node)

      statuses = report_hooks(element, 'before')
      forced_issue = nil
      element['steps'].each do |step|
        name = decorate("#{step['keyword']}#{step['name']}")
        if step['rows']
          name << step['rows'].reduce("\n") { |acc, row| acc << decorate("| #{row['cells'].join(' | ')} |") << "\n" }
        end
        if step['doc_string']
          name << %(\n"""\n#{step['doc_string']['value']}\n""")
        end

        ReportPortal.send_log(:passed, name, get_time)
        step['output'].each { |o| ReportPortal.send_log(:passed, o, get_time) } unless step['output'].nil?
        error = step['result']['error_message']
        ReportPortal.send_log(:failed, error, get_time) if error
        (step['embeddings'] || []).each do |embedding|
          ReportPortal.send_file(:failed, embedding['data'], 'Embedding', get_time, embedding['mime_type'])
        end
        statuses << step['result']['status']
        forced_issue ||= case step['result']['status']
                         when 'pending'
                           error
                         when 'undefined'
                           "Undefined step #{step['name']} at #{step['match']['location']}"
                         else
                           nil
                         end

        ReportPortal.send_log(step['result']['status'].to_sym,
                              "STEP #{step['result']['status'].upcase}",
                              get_time(step['result']['duration'].to_i / 1_000_000))
      end
      statuses += report_hooks(element, 'after')

      status = if statuses.any? { |s| %w(failed undefined pending).include? s }
                 :failed
               elsif statuses.all? { |s| s == 'passed' }
                 :passed
               else
                 :skipped
               end

      ReportPortal.finish_item(ReportPortal.current_scenario, status, get_time, forced_issue)
      ReportPortal.current_scenario = nil
    end

    ReportPortal.finish_item(current_feature, nil, get_time)
  end
  ReportPortal.finish_item(root, nil, get_time)
  ReportPortal.finish_launch(get_time)
end