Module: ReportPortal

Defined in:
lib/report_portal/rspec/formatter.rb,
lib/reportportal.rb,
lib/report_portal/version.rb,
lib/report_portal/settings.rb,
lib/report_portal/event_bus.rb,
lib/report_portal/http_client.rb,
lib/report_portal/logging/logger.rb,
lib/report_portal/cucumber/report.rb,
lib/report_portal/models/test_item.rb,
lib/report_portal/cucumber/formatter.rb,
lib/report_portal/logging/log4r_outputter.rb,
lib/report_portal/cucumber/parallel_report.rb,
lib/report_portal/logging/logging_appender.rb,
lib/report_portal/models/item_search_options.rb,
lib/report_portal/cucumber/parallel_formatter.rb,
lib/report_portal/events/prepare_start_item_request.rb

Overview

TODO: Screenshots TODO: Logs

Defined Under Namespace

Modules: Cucumber, Events, RSpec Classes: EventBus, HttpClient, ItemSearchOptions, Log4rOutputter, LoggingAppender, Settings, TestItem

Constant Summary collapse

LOG_LEVELS =
{ error: 'ERROR', warn: 'WARN', info: 'INFO', debug: 'DEBUG', trace: 'TRACE', fatal: 'FATAL', unknown: 'UNKNOWN' }.freeze
VERSION =
'0.7'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_scenarioObject

Returns the value of attribute current_scenario.



20
21
22
# File 'lib/reportportal.rb', line 20

def current_scenario
  @current_scenario
end

.launch_idObject

Returns the value of attribute launch_id.



20
21
22
# File 'lib/reportportal.rb', line 20

def launch_id
  @launch_id
end

Class Method Details

.close_child_items(parent_id) ⇒ Object

needed for parallel formatter



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/reportportal.rb', line 144

def close_child_items(parent_id)
  path = if parent_id.nil?
           "item?filter.eq.launch=#{@launch_id}&filter.size.path=0&page.page=1&page.size=100"
         else
           "item?filter.eq.parent=#{parent_id}&page.page=1&page.size=100"
         end
  ids = []
  loop do
    data = send_request(:get, path)
    if data.key?('links')
      link = data['links'].find { |i| i['rel'] == 'next' }
      url = link.nil? ? nil : link['href']
    else
      url = nil
    end
    data['content'].each do |i|
      ids << i['id'] if i['has_childs'] && i['status'] == 'IN_PROGRESS'
    end
    break if url.nil?
  end

  ids.each do |id|
    close_child_items(id)
    finish_item(TestItem.new(id: id))
  end
end

.delete_items(item_ids) ⇒ Object

Parameters:

  • item_ids (Array<String>)

    an array of items to remove (represented by ids)



126
127
128
# File 'lib/reportportal.rb', line 126

def delete_items(item_ids)
  send_request(:delete, 'item', params: { ids: item_ids })
end

.finish_item(item, status = nil, end_time = nil, force_issue = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/reportportal.rb', line 65

def finish_item(item, status = nil, end_time = nil, force_issue = nil)
  unless item.nil? || item.id.nil? || item.closed
    data = { end_time: end_time.nil? ? now : end_time }
    data[:status] = status unless status.nil?
    if force_issue && status != :passed # TODO: check for :passed status is probably not needed
      data[:issue] = { issue_type: 'AUTOMATION_BUG', comment: force_issue.to_s }
    elsif status == :skipped
      data[:issue] = { issue_type: 'NOT_ISSUE' }
    end
    send_request(:put, "item/#{item.id}", json: data)
    item.closed = true
  end
end

.finish_launch(end_time = now) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/reportportal.rb', line 46

def finish_launch(end_time = now)
  data = { end_time: end_time }
  @finished_launch = send_request(:put, "launch/#{@launch_id}/finish", json: data)
  @launch_link = @finished_launch['link']
  if Settings.instance.logLaunchLink
  	print "Launch ID ReportPortal: #{@launch_link}"
  end
end

.get_items(filter_options = {}) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/reportportal.rb', line 108

def get_items(filter_options = {})
  page_size = 100
  max_pages = 100
  all_items = []
  1.step.each do |page_number|
    raise 'Too many pages with the results were returned' if page_number > max_pages

    options = ItemSearchOptions.new({ page_size: page_size, page_number: page_number }.merge(filter_options))
    page_items = send_request(:get, 'item', params: options.query_params)['content'].map do |item_params|
      TestItem.new(item_params)
    end
    all_items += page_items
    break if page_items.size < page_size
  end
  all_items
end

.item_id_of(name, parent_node) ⇒ Object

needed for parallel formatter



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/reportportal.rb', line 131

def item_id_of(name, parent_node)
  path = if parent_node.is_root? # folder without parent folder
           "item?filter.eq.launch=#{@launch_id}&filter.eq.name=#{CGI.escape(name)}&filter.size.path=0"
         else
           "item?filter.eq.parent=#{parent_node.content.id}&filter.eq.name=#{CGI.escape(name)}"
         end
  data = send_request(:get, path)
  if data.key? 'content'
    data['content'].empty? ? nil : data['content'][0]['id']
  end
end

.nowObject



22
23
24
# File 'lib/reportportal.rb', line 22

def now
  (current_time.to_f * 1000).to_i
end

.on_event(name, &proc) ⇒ Object

Registers an event. The proc will be called back with the event object.



172
173
174
# File 'lib/reportportal.rb', line 172

def on_event(name, &proc)
  event_bus.on(name, &proc)
end

.patch_loggerObject

Monkey-patch for built-in Logger class



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
# File 'lib/report_portal/logging/logger.rb', line 6

def patch_logger
  Logger.class_eval do
    alias_method :orig_add, :add
    alias_method :orig_write, :<<
    def add(severity, message = nil, progname = nil, &block)
      ret = orig_add(severity, message, progname, &block)

      unless severity < @level
        progname ||= @progname
        if message.nil?
          if block_given?
            message = yield
          else
            message = progname
            progname = @progname
          end
        end
        ReportPortal.send_log(format_severity(severity), format_message(format_severity(severity), Time.now, progname, message.to_s), ReportPortal.now)
      end
      ret
    end

    def <<(msg)
      ret = orig_write(msg)
      ReportPortal.send_log(ReportPortal::LOG_LEVELS[:unknown], msg.to_s, ReportPortal.now)
      ret
    end
  end
end

.send_file(status, path_or_src, label = nil, time = now, mime_type = 'image/png') ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/reportportal.rb', line 88

def send_file(status, path_or_src, label = nil, time = now, mime_type = 'image/png')
  str_without_nils = path_or_src.to_s.gsub("\0", '') # file? does not allow NULLs inside the string
  if File.file?(str_without_nils)
    send_file_from_path(status, path_or_src, label, time, mime_type)
  else
    if mime_type =~ /;base64$/
      mime_type = mime_type[0..-8]
      path_or_src = Base64.decode64(path_or_src)
    end
    extension = ".#{MIME::Types[mime_type].first.extensions.first}"
    Tempfile.open(['report_portal', extension]) do |tempfile|
      tempfile.binmode
      tempfile.write(path_or_src)
      tempfile.rewind
      send_file_from_path(status, tempfile.path, label, time, mime_type)
    end
  end
end

.send_log(status, message, time) ⇒ Object

TODO: implement force finish



81
82
83
84
85
86
# File 'lib/reportportal.rb', line 81

def send_log(status, message, time)
  unless @current_scenario.nil? || @current_scenario.closed # it can be nil if scenario outline in expand mode is executed
    data = { item_id: @current_scenario.id, time: time, level: status_to_level(status), message: message.to_s }
    send_request(:post, 'log', json: data)
  end
end

.start_item(item_node) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/reportportal.rb', line 55

def start_item(item_node)
  path = 'item'
  path += "/#{item_node.parent.content.id}" unless item_node.parent&.is_root?
  item = item_node.content
  data = { start_time: item.start_time, name: item.name[0, 255], type: item.type.to_s, launch_id: @launch_id, description: item.description }
  data[:tags] = item.tags unless item.tags.empty?
  event_bus.broadcast(:prepare_start_item_request, request_data: data)
  send_request(:post, path, json: data)['id']
end

.start_launch(description, start_time = now) ⇒ Object



39
40
41
42
43
44
# File 'lib/reportportal.rb', line 39

def start_launch(description, start_time = now)
  required_data = { name: Settings.instance.launch, start_time: start_time, description:
      description, mode: Settings.instance.launch_mode }
  data = prepare_options(required_data, Settings.instance)
  @launch_id = send_request(:post, 'launch', json: data)['id']
end

.status_to_level(status) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/reportportal.rb', line 26

def status_to_level(status)
  case status
  when :passed
    LOG_LEVELS[:info]
  when :failed, :undefined, :pending, :error
    LOG_LEVELS[:error]
  when :skipped
    LOG_LEVELS[:warn]
  else
    LOG_LEVELS.fetch(status, LOG_LEVELS[:info])
  end
end