module Reportir

TODO: RSpec::Core::Formatters.register

Constants

VERSION

Public Instance Methods

s3_screenshot(method) click to toggle source
# File lib/reportir.rb, line 32
def s3_screenshot(method)
  create_current_test  #TODO: before-filter??
  @@step = @@step+=1
  image_name = "#{@@step}-#{method}" 
  local_path = "#{local_test_root}/#{image_name}.png"
  FileUtils.mkdir_p(local_test_root) unless Dir.exists?(local_test_root)
  @browser.screenshot.save(local_path)
  current_test[:screenshots] << { name: image_name, src: "#{public_path_for_this_test}/#{image_name}.png" }
end
save_report_locally() click to toggle source
# File lib/reportir.rb, line 16
def save_report_locally
  copy_template_to_temp_dir
  write_javascript_models
  save_final_markup
  puts "Upload to s3 not possible. Missing ENV vars. Report saved to #{local_root}"
end
upload_everything_to_s3() click to toggle source
# File lib/reportir.rb, line 23
def upload_everything_to_s3
  upload_template unless template_uploaded?
  clear_previous_results_from_s3
  save_final_markup
  write_javascript_models
  upload_to_s3
  delete_tmp_files_for_this_test
end
upload_result_to_s3_as_static_site() click to toggle source
# File lib/reportir.rb, line 11
def upload_result_to_s3_as_static_site
  return upload_everything_to_s3 if upload_to_s3_possible?
  save_report_locally
end

Private Instance Methods

array_of_test_names() click to toggle source
# File lib/reportir.rb, line 147
def array_of_test_names
  @@tests.map{|t| t[:name] }
end
aws_config() click to toggle source
# File lib/reportir.rb, line 71
def aws_config
  {
    bucket: ENV['AWS_DEFAULT_BUCKET'],
    secret: ENV['AWS_SECRET_ACCESS_KEY'],
    key: ENV['AWS_ACCESS_KEY_ID'],
    region: ENV['AWS_DEFAULT_REGION']
  }
end
bucket() click to toggle source
# File lib/reportir.rb, line 120
def bucket
  @bucket ||= ::Aws::S3::Resource.new.bucket(aws_config[:bucket])
end
check_for_env_vars() click to toggle source
# File lib/reportir.rb, line 64
def check_for_env_vars
  raise Reportir::Error.new 'Missing ENV AWS_DEFAULT_BUCKET' unless aws_config[:bucket]
  raise Reportir::Error.new 'Missing ENV AWS_SECRET_ACCESS_KEY' unless aws_config[:secret]
  raise Reportir::Error.new 'Missing ENV AWS_ACCESS_KEY_ID' unless aws_config[:key]
  raise Reportir::Error.new 'Missing ENV AWS_DEFAULT_REGION' unless aws_config[:region]
end
clear_previous_results_from_s3() click to toggle source
# File lib/reportir.rb, line 100
def clear_previous_results_from_s3
  puts "deleting all previous test data from s3"
  ::Aws::S3::Bucket.new(aws_config[:bucket]).delete_objects({
    delete: {
      objects: [{ key: "#{public_path_for_this_test}" }],
      quiet: true
    }
  })
end
copy_template_to_temp_dir() click to toggle source
# File lib/reportir.rb, line 44
def copy_template_to_temp_dir
  FileUtils.cp_r(static_site_template_path+'/.', local_root)
end
create_current_test() click to toggle source
# File lib/reportir.rb, line 141
def create_current_test
  if !current_test || current_test.empty? 
    @@tests << { name: test_name, screenshots: [], add_links: [] }
  end
end
current_test() click to toggle source
# File lib/reportir.rb, line 137
def current_test
  @@tests.select{ |test| test[:name] == test_name }.first
end
delete_tmp_files_for_this_test() click to toggle source
# File lib/reportir.rb, line 80
def delete_tmp_files_for_this_test
  FileUtils.rm_rf local_test_root
end
local_model_file_path() click to toggle source
# File lib/reportir.rb, line 151
def local_model_file_path
  "#{local_root}/js/models.js"
end
local_root() click to toggle source
# File lib/reportir.rb, line 159
def local_root
  "./tmp/reportir"
end
local_test_root() click to toggle source
# File lib/reportir.rb, line 155
def local_test_root
  "#{local_root}/#{public_path_for_this_test}"
end
public_artifact_root() click to toggle source
# File lib/reportir.rb, line 167
def public_artifact_root
  "test_artifacts"
end
public_path_for_this_test() click to toggle source
# File lib/reportir.rb, line 163
def public_path_for_this_test
  "#{public_artifact_root}/#{test_name}"
end
save_final_markup() click to toggle source
# File lib/reportir.rb, line 84
def save_final_markup
  s3_screenshot('final')
  path = "#{local_test_root}/final.html"
  File.open(path, 'w') { |f| f.write @browser.html }
  current_test[:add_links] << { name: 'Markup from Last Page', path: "#{public_path_for_this_test}/final.html" }
end
static_site_template_path() click to toggle source
# File lib/reportir.rb, line 133
def static_site_template_path
  Gem.find_files('reportir')[1] +'/static_site_template'
end
static_site_url() click to toggle source
# File lib/reportir.rb, line 175
def static_site_url
  # TODO: use aws-sdk for this.
  "http://#{aws_config[:bucket]}.s3-website-#{aws_config[:region]}.amazonaws.com"
end
template_uploaded?() click to toggle source
# File lib/reportir.rb, line 53
def template_uploaded?
  bucket.object('index.html').exists?
end
test_name() click to toggle source
# File lib/reportir.rb, line 171
def test_name
  ::RSpec.current_example.metadata[:test_name] || ::RSpec.current_example.metadata[:full_description].gsub(/[^\w\s]/,'').gsub(/\s/,'_')
end
upload_directory(local, remote) click to toggle source
# File lib/reportir.rb, line 124
def upload_directory(local, remote)
  ::S3Uploader.upload_directory(local, aws_config[:bucket], { 
    :destination_dir => remote, 
    :threads => 5, 
    s3_key: aws_config[:key], 
    s3_secret: aws_config[:secret], 
    region: aws_config[:region] })
end
upload_template() click to toggle source
# File lib/reportir.rb, line 48
def upload_template
  puts '====== UPLOADING TEMPLATE ========='
  upload_directory(static_site_template_path, '')   
end
upload_to_s3() click to toggle source
# File lib/reportir.rb, line 110
def upload_to_s3
  puts '====== UPLOADING RESULTS ========='
  bucket.object('js/models.js').upload_file(local_model_file_path)
  puts "Uploading #{local_model_file_path} to /js/models.js"
  upload_directory(local_test_root, public_path_for_this_test)
  puts '====== S3 UPLOAD COMPLETE ========='
  puts 'URL: ' + static_site_url
  puts '==================================='
end
upload_to_s3_possible?() click to toggle source
# File lib/reportir.rb, line 57
def upload_to_s3_possible?
  check_for_env_vars
  true
rescue Reportir::Error
  return false
end
write_javascript_models() click to toggle source
# File lib/reportir.rb, line 91
def write_javascript_models
  string = %{
    var navigation = #{array_of_test_names.to_json};
    var tests = #{@@tests.to_json};
  }
  FileUtils.mkdir_p("#{local_root}/js")
  File.open(local_model_file_path, "w") { |f| f.write(string) }
end