class Reporter

Constants

AMBIGUOUS
FAILED
NOT_DEFINED
PASSED
PENDING
SKIPPED

Attributes

test_scenario[RW]

Public Class Methods

new(test_scenario:) click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 20
def initialize(test_scenario:)
  @test_scenario = test_scenario
end

Public Instance Methods

create_devices_execution_report_folder() click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 170
def create_devices_execution_report_folder
  devices.each do |device|
    Dir.mkdir(
      "#{KrakenMobile::Constants::REPORT_PATH}/#{test_execution_id}/"\
      "#{device.id}"
    )
  end
end
create_report_execution_report_folder() click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 151
def create_report_execution_report_folder
  Dir.mkdir(K::REPORT_PATH) unless File.exist?(K::REPORT_PATH)
  Dir.mkdir("#{K::REPORT_PATH}/#{test_execution_id}")
  Dir.mkdir(screenshot_path)
  FileUtils.cp_r(
    File.expand_path(K::REPORT_ASSETS_PATH, __FILE__),
    "#{K::REPORT_PATH}/#{test_execution_id}/"
  )
end
create_report_folder_requirements() click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 27
def create_report_folder_requirements
  create_report_execution_report_folder
  create_devices_execution_report_folder
  save_execution_devices_list
end
generate_device_report(device, id) click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 74
def generate_device_report(device, id)
  if device.is_a? AndroidDevice
    generate_mobile_report(device, id)
  elsif device.is_a? WebDevice
    generate_web_report(device, id)
  else
    raise 'ERROR: Platform not supported'
  end
end
generate_each_device_report() click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 68
def generate_each_device_report
  devices.each_with_index do |device, index|
    generate_device_report(device, index + 1)
  end
end
generate_general_report() click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 38
def generate_general_report
  erb_file = File.join(
    File.expand_path('../../../../reporter', __FILE__),
    'index.html.erb'
  )
  html_file = File.join(
    File.expand_path("#{K::REPORT_PATH}/#{test_execution_id}/"),
    'index.html'
  )
  report_file = open(
    "#{K::REPORT_PATH}/#{test_execution_id}/#{K::DEVICES_REPORT_FILE_NAME}"
  )
  content = report_file.read
  report_file.close
  devices_report = report_by_devices
  @features_report = fetures_from_report_by_devices(devices_report)
  data_hash = feature_by_nodes_and_links @features_report
  open(
    "#{K::REPORT_PATH}/#{test_execution_id}/assets/js/#{K::D3_DATA_FILE_NAME}",
    'w'
  ) do |file|
    file.puts(data_hash.to_json)
  end
  template = File.read(erb_file)
  result = ERB.new(template).result(binding)
  File.open(html_file, 'w+') do |f|
    f.write result
  end
end
generate_mobile_report(device, id) click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 84
def generate_mobile_report(device, id)
  process = MobileProcess.new(
    id: id,
    device: device,
    test_scenario: @test_scenario
  )
  @apk_path = process.apk_path
  report_file = open("#{K::REPORT_PATH}/#{test_execution_id}/#{device.id}/#{K::FILE_REPORT_NAME}")
  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("#{K::REPORT_PATH}/#{test_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_web_report(device, id) click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 113
def generate_web_report(device, id)
  process = WebProcess.new(
    id: id,
    device: device,
    test_scenario: @test_scenario
  )
  @apk_path = nil
  report_file = open("#{K::REPORT_PATH}/#{test_execution_id}/#{device.id}/#{K::FILE_REPORT_NAME}")
  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("#{K::REPORT_PATH}/#{test_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
save_execution_devices_list() click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 161
def save_execution_devices_list
  open(
    "#{K::REPORT_PATH}/#{test_execution_id}/#{K::DEVICES_REPORT_FILE_NAME}",
    'w'
  ) do |file|
    file.puts(devices_json.to_json)
  end
end
save_report() click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 33
def save_report
  generate_each_device_report
  generate_general_report
end
screenshot_path() click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 179
def screenshot_path
  "#{K::REPORT_PATH}/#{test_execution_id}/screenshots/"
end
test_execution_id() click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 145
def test_execution_id
  raise 'ERROR: Invalid test scenario' if @test_scenario.nil?

  @test_scenario.execution_id
end

Private Instance Methods

devices() click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 363
def devices
  raise 'ERROR: Invalid test scenario' if @test_scenario.nil?

  @test_scenario.devices.compact
end
devices_json() click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 369
def devices_json
  devices.map.with_index do |device, index|
    height, width = device.screen_size
    {
      user: (index + 1), id: device.id,
      model: device.model, screen_height: height,
      screen_width: width, sdk: device.sdk_version,
      type: device.type
    }
  end
end
failed_features(features) click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 468
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/utils/reporter.rb, line 390
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/utils/reporter.rb, line 472
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/utils/reporter.rb, line 434
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/utils/reporter.rb, line 497
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/utils/reporter.rb, line 454
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/utils/reporter.rb, line 430
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/utils/reporter.rb, line 227
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/utils/reporter.rb, line 458
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_feature_report(feature, device) click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 191
def generate_feature_report feature, device
  Dir.mkdir("#{K::REPORT_PATH}/#{test_execution_id}/#{device.id}/features_report") unless File.exists?("#{K::REPORT_PATH}/#{test_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("#{K::REPORT_PATH}/#{test_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/utils/reporter.rb, line 185
def generate_features_report features, device
  features.each do |feature|
    generate_feature_report feature, device
  end
end
isReadSignal(step) click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 343
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/utils/reporter.rb, line 348
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/utils/reporter.rb, line 464
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/utils/reporter.rb, line 489
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() click to toggle source
# File lib/kraken-mobile/utils/reporter.rb, line 206
def report_by_devices
  devices_report = {}
  devices_json.each do |device|
    unless File.exist?(
      "#{K::REPORT_PATH}/#{test_execution_id}/#{device[:id]}/#{K::FILE_REPORT_NAME}"
    )
      next
    end

    report_file = open(
      "#{K::REPORT_PATH}/#{test_execution_id}/#{device[:id]}/#{K::FILE_REPORT_NAME}"
    )
    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/utils/reporter.rb, line 481
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/utils/reporter.rb, line 353
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/utils/reporter.rb, line 358
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/utils/reporter.rb, line 422
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/utils/reporter.rb, line 450
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/utils/reporter.rb, line 406
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/utils/reporter.rb, line 446
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/utils/reporter.rb, line 414
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/utils/reporter.rb, line 442
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/utils/reporter.rb, line 398
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/utils/reporter.rb, line 438
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/utils/reporter.rb, line 381
def total_scenarios features
  how_many = 0
  features.each do |feature|
    scenarios = feature["elements"]
    how_many += scenarios.count if scenarios
  end
  how_many
end