class KrakenMobile::Reporter

Constants

FAILED
PASSED

Public Class Methods

new(execution_id, options) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 15
def initialize(execution_id, options)
  @execution_id = execution_id
  @options = options
end

Public Instance Methods

branches(features_report) click to toggle source

0: create 1: commit 2: merge

# File lib/kraken-mobile/helpers/reporter.rb, line 241
def branches features_report
  branches = {}
  features_report.keys.each do |key|
    report = features_report[key]
    branches[report["hash"]] = {} if !branches[report["hash"]]
    branches[report["hash"]]["steps"]= [] if !branches[report["hash"]]["steps"]
    devices = report["devices"]
    devices.keys.each do |device_key|
      branches[report["hash"]]["steps"] << { type: 0, name: device_key }
      feature_steps = devices[device_key][0]["steps"] if devices[device_key].count > 0 && devices[device_key][0]["steps"]
      feature_steps.each do |step|
        hash_step = { type: 1, name: device_key}
        hash_step[:image] = step["after"][0]["embeddings"][0] if step["after"].count > 0 && step["after"][0]["embeddings"] && step["after"][0]["embeddings"].count > 0
        branches[report["hash"]]["steps"] << hash_step
      end
    end
  end
  branches
end
failed_features(features) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 281
def failed_features features
  features.select{ |feature| failed_scenarios(feature) == feature["elements"].count }
end
failed_scenarios(feature) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 310
def failed_scenarios feature
  scenarios = feature["elements"]
  scenarios.select{ |scenario|
    steps = scenario["steps"]
    steps.any?{ |step| step["result"] && step["result"]["status"] != PASSED }
  }
end
feature_duration(feature) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 285
def feature_duration feature
  scenarios = feature["elements"]
  how_long = 0
  scenarios.each do |scenario|
    how_long += scenario_duration(scenario)
  end
  how_long
end
feature_failed_scenarios_percentage(feature) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 354
def feature_failed_scenarios_percentage feature
  (failed_scenarios(feature).count.to_f/feature["elements"].count.to_f).round(2) * 100.00
end
feature_id(feature) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 273
def feature_id feature
  Digest::SHA256.hexdigest("#{feature["id"].strip}#{feature["uri"].strip}")
end
feature_passed?(feature) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 374
def feature_passed? feature
  passed_scenarios(feature).count == feature["elements"].count
end
feature_passed_scenarios_percentage(feature) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 350
def feature_passed_scenarios_percentage feature
  (passed_scenarios(feature).count.to_f/feature["elements"].count.to_f).round(2) * 100.00
end
fetures_from_report_by_devices(report_by_devices) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 83
def fetures_from_report_by_devices report_by_devices
  features = {}
  report_by_devices.keys.each do |user_key|
    report = report_by_devices[user_key]
    report.each do |feature|
      features[feature["id"]] = {} if !features[feature["id"]]
      features[feature["id"]]["name"] = feature["name"] if !features[feature["id"]]["name"] && feature["name"]
      features[feature["id"]]["devices"] = {} if !features[feature["id"]]["devices"]
      if feature["elements"] && feature["elements"].count > 0
        features[feature["id"]]["devices"][user_key] = []
        if feature["elements"].first["steps"]
          failed = false
          feature["elements"].first["steps"].each do |step|
            next if failed
            failed = step["result"]["status"] != PASSED
            image = nil
            image = step["after"].first["embeddings"].first["data"] if step["after"] && step["after"].count > 0 && step["after"].first["embeddings"] && step["after"].first["embeddings"].count > 0
            features[feature["id"]]["devices"][user_key] << {
              name: "#{step['keyword']} #{step['name']}",
              duration: step["result"]["duration"],
              image: image,
              device_model: feature["device_model"],
              status: failed ? FAILED : PASSED
            }
          end
        end
      end
    end
  end
  features
end
format_duration(nanoseconds) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 378
def format_duration(nanoseconds)
  duration_in_seconds = nanoseconds.to_f/1000000000.0
  m, s = duration_in_seconds.divmod(60)
  "#{m}m #{format('%.3f', s)}s"
end
generate_device_report(device) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 45
def generate_device_report device
  @apk_path = device.config["apk_path"] ? device.config["apk_path"] : @options[:apk_path]
  report_file = open("#{KrakenMobile::Constants::REPORT_PATH}/#{@execution_id}/#{device.id}/#{KrakenMobile::Constants::REPORT_FILE_NAME}.json")
  content = report_file.read
  report_file.close
  @features = JSON.parse(content)
  @total_scenarios = total_scenarios @features
  @device = device
  @total_failed_scenarios_percentage = total_failed_scenarios_percentage @features
  @total_passed_scenarios_percentage = total_passed_scenarios_percentage @features
  @total_passed_features_percentage = total_passed_features_percentage @features
  @total_failed_features_percentage = total_failed_features_percentage @features
  erb_file = File.join(File.expand_path("../../../../reporter/", __FILE__), "feature_report.html.erb")
  html_file = File.join(File.expand_path("#{KrakenMobile::Constants::REPORT_PATH}/#{@execution_id}/#{device.id}/"), File.basename(erb_file, '.erb')) #=>"page.html"
  # Variables
  template = File.read(erb_file)
  result = ERB.new(template).result(binding)
  # write result to file
  File.open(html_file, 'w+') do |f|
    f.write result
  end
  generate_features_report @features, device
end
generate_feature_report(feature, device) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 225
def generate_feature_report feature, device
  Dir.mkdir("#{KrakenMobile::Constants::REPORT_PATH}/#{@execution_id}/#{device.id}/features_report") unless File.exists?("#{KrakenMobile::Constants::REPORT_PATH}/#{@execution_id}/#{device.id}/features_report")
  file_name = feature_id feature
  erb_file = File.join(File.expand_path("../../../../reporter/", __FILE__), "scenario_report.html.erb")
  html_file = File.join(File.expand_path("#{KrakenMobile::Constants::REPORT_PATH}/#{@execution_id}/#{device.id}/features_report"), "#{file_name}.html") #=>"page.html"
  # Variables
  @feature = feature
  template = File.read(erb_file)
  result = ERB.new(template).result(binding)
  # write result to file
  File.open(html_file, 'w+') do |f|
    f.write result
  end
end
generate_features_report(features, device) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 219
def generate_features_report features, device
  features.each do |feature|
    generate_feature_report feature, device
  end
end
generate_general_report() click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 23
def generate_general_report
  erb_file = File.join(File.expand_path("../../../../reporter/", __FILE__), "index.html.erb")
  html_file = File.join(File.expand_path("#{KrakenMobile::Constants::REPORT_PATH}/#{@execution_id}/"), "index.html")
  # Variables
  report_file = open("#{KrakenMobile::Constants::REPORT_PATH}/#{@execution_id}/#{KrakenMobile::Constants::REPORT_DEVICES_FILE_NAME}.json")
  content = report_file.read
  report_file.close
  @devices = JSON.parse(content)
  devices_report = report_by_devices(@devices)
  @features_report = fetures_from_report_by_devices devices_report
  data_hash = feature_by_nodes_and_links @features_report
  file = open("#{KrakenMobile::Constants::REPORT_PATH}/#{@execution_id}/assets/js/#{KrakenMobile::Constants::D3_DATA_FILE_NAME}.json", 'w')
  file.puts(data_hash.to_json)
  file.close
  template = File.read(erb_file)
  result = ERB.new(template).result(binding)
  # write result to file
  File.open(html_file, 'w+') do |f|
    f.write result
  end
end
isReadSignal(step) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 199
def isReadSignal step
  line = step.split(' ')[1..-1].join(' ')
  (line =~ /^I wait for a signal containing "([^\"]*)"$/ ? true : false) || (line =~ /^I wait for a signal containing "([^\"]*)" for (\d+) seconds$/ ? true : false)
end
isWriteSignal(step) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 204
def isWriteSignal step
  line = step.split(' ')[1..-1].join(' ')
  line =~ /^I send a signal to user (\d+) containing "([^\"]*)"$/ ? true : false
end
passed_features(features) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 277
def passed_features features
  features.select{ |feature| passed_scenarios(feature) == feature["elements"].count }
end
passed_scenarios(feature) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 302
def passed_scenarios feature
    scenarios = feature["elements"]
    scenarios.select{ |scenario|
      steps = scenario["steps"]
      steps.all?{ |step| step["result"] && step["result"]["status"] == PASSED }
    }
end
report_by_devices(devices) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 69
def report_by_devices devices
  devices_report = {}
  devices.each do |device|
    next if !File.exists?("#{KrakenMobile::Constants::REPORT_PATH}/#{@execution_id}/#{device['id']}/#{KrakenMobile::Constants::REPORT_FILE_NAME}.json")
    report_file = open("#{KrakenMobile::Constants::REPORT_PATH}/#{@execution_id}/#{device['id']}/#{KrakenMobile::Constants::REPORT_FILE_NAME}.json")
    content = report_file.read
    report_file.close
    devices_report[device['user']] = JSON.parse(content)
    devices_report[device['user']].each do |d| d["device_model"] = device["model"] if !d["device_model"] end
    devices_report[device['user']].each do |d| d["device_id"] = device["id"] if !d["device_id"] end
  end
  devices_report
end
scenario_duration(scenario) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 294
def scenario_duration scenario
  how_long = 0
  scenario["steps"].each do |step|
    how_long += step["result"]["duration"] if step["result"] && step["result"]["duration"]
  end
  how_long
end
signalContent(step) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 209
def signalContent step
  line = step.split(' ')[1..-1].join(' ')
  line.scan(/"([^\"]*)"/).first.first if line.scan(/"([^\"]*)"/).first
end
signalReceiver(step) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 214
def signalReceiver step
  line = step.split(' ')[1..-1].join(' ')
  line.scan(/(\d+)/).first.first if line.scan(/(\d+)/).first
end
total_failed_features(features) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 342
def total_failed_features features
  how_many = 0
  features.each do |feature|
    how_many += 1 if !feature_passed?(feature)
  end
  how_many
end
total_failed_features_percentage(features) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 370
def total_failed_features_percentage features
  (total_failed_features(features).to_f/features.count.to_f).round(2) * 100.00
end
total_failed_scenarios(features) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 326
def total_failed_scenarios features
  how_many = 0
  features.each do |feature|
    how_many += failed_scenarios(feature).count
  end
  how_many
end
total_failed_scenarios_percentage(features) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 366
def total_failed_scenarios_percentage features
  (total_failed_scenarios(features).to_f/total_scenarios(features).to_f).round(2) * 100.00
end
total_passed_features(features) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 334
def total_passed_features features
  how_many = 0
  features.each do |feature|
    how_many += 1 if feature_passed?(feature)
  end
  how_many
end
total_passed_features_percentage(features) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 362
def total_passed_features_percentage features
  (total_passed_features(features).to_f/features.count.to_f).round(2) * 100.00
end
total_passed_scenarios(features) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 318
def total_passed_scenarios features
  how_many = 0
  features.each do |feature|
    how_many += passed_scenarios(feature).count
  end
  how_many
end
total_passed_scenarios_percentage(features) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 358
def total_passed_scenarios_percentage features
  (total_passed_scenarios(features).to_f/total_scenarios(features).to_f).round(2) * 100.00
end
total_scenarios(features) click to toggle source
# File lib/kraken-mobile/helpers/reporter.rb, line 264
def total_scenarios features
  how_many = 0
  features.each do |feature|
    scenarios = feature["elements"]
    how_many += scenarios.count if scenarios
  end
  how_many
end