Class: ResultsKeeper

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/results_keeper.rb

Constant Summary collapse

DEFAULT_PORT =
3000
DEFAULT_HOST =
'localhost'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResultsKeeper

Returns a new instance of ResultsKeeper.



16
17
18
19
# File 'lib/results_keeper.rb', line 16

def initialize
  @revision_name = ENV['REVISION_NAME'] || "Result for #{Time.now}"
  @revision_project = ENV['PROJECT_NAME'] || 'Default'
end

Instance Attribute Details

#revision_nameObject (readonly)

Returns the value of attribute revision_name.



11
12
13
# File 'lib/results_keeper.rb', line 11

def revision_name
  @revision_name
end

#revision_projectObject (readonly)

Returns the value of attribute revision_project.



11
12
13
# File 'lib/results_keeper.rb', line 11

def revision_project
  @revision_project
end

Instance Method Details

#console_message(text) ⇒ Object



96
97
98
# File 'lib/results_keeper.rb', line 96

def console_message(text)
  puts "    Results Keeper: #{text}".blue unless ENV['HIDE_RK_MESSAGES'] == 'true'
end

#project_nameObject



92
93
94
# File 'lib/results_keeper.rb', line 92

def project_name
  ENV['PROJECT_NAME']
end

#save_screenshot(scenario) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/results_keeper.rb', line 51

def save_screenshot(scenario)
  if scenario.exception
    screenshot_name = "#{Time.now.to_i}_#{rand(1000..9999)}.png"
    @file_path = "tmp/screenshots/#{screenshot_name}"
    Capybara.page.save_screenshot(@file_path)
    @file_path
  end
end

#send_json(body, path, message_to_client = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/results_keeper.rb', line 60

def send_json(body, path, message_to_client=nil)
  @host = ENV['REPORT_SERVER_HOST'] || DEFAULT_HOST
  @port = ENV['REPORT_SERVER_PORT'] || DEFAULT_PORT

  body['secret_key'] = ENV['RK_SECRET_KEY']
  @path = "/api/#{path}"
  @body = body.to_json

  request = Net::HTTP::Post.new(@path, initheader = {'Content-Type' =>'application/json'})
  request.body = @body
  response = Net::HTTP.new(@host, @port).start {|http| http.request(request) }
  begin
    message_to_client ? console_message("#{message_to_client} - #{response.code} - #{response.message}") : console_message("#{response.code} - #{response.message}")
    results_hash = JSON.parse(response.body)
    results_hash
  rescue
    console_message('There seems to be a problem. Are you authorized?')
  end
end

#send_revisionObject



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

def send_revision
  data = { revision: @revision_name, project: project_name }
  @revision = send_json(data, 'revisions', "#{@revision_project} > #{@revision_name} started")
end

#send_revision_completeObject



26
27
28
29
# File 'lib/results_keeper.rb', line 26

def send_revision_complete
  data = { revision: @revision_name, project: project_name, completed: 1 }
  send_json(data, 'revisions', "#{@revision_name} completed")
end

#send_screenshot(screenshot_path) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/results_keeper.rb', line 80

def send_screenshot(screenshot_path)
  if ENV['SEND_SCREENSHOTS']
    t1 = Thread.new do
      params = { project: @revision['project_id'], revision: @revision['revision_id'], test: @test['id'] }
      RestClient.post("http://#{@host}:#{@port}/api/tests/upload_screenshot",
                      :name_of_file_param => File.new(screenshot_path), :body => params)
      File.delete(screenshot_path)
    end
    t1.join
  end
end

#send_test(scenario) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/results_keeper.rb', line 31

def send_test(scenario)
  test_duration = Time.now - @test_started_at
  @screenshot_path = save_screenshot(scenario) if ENV['SEND_SCREENSHOTS']
  scenario_steps = scenario.test_steps.map{|s| "And #{s.to_s}"} unless ENV['NO_STEPS']
  scenario_error = scenario.exception.message if scenario.exception
  run_path = "cucumber #{scenario.location.file}:#{scenario.location.line}"
  data = { name: scenario.name,
   status: scenario.status,
   feature_name: scenario.feature.name,
   run_path: run_path,
   tags: scenario.tags.map(&:name).join(', '),
   error: scenario_error,
   revision_id: @revision['revision_id'],
   steps: scenario_steps,
   duration: test_duration
  }
  @test = send_json(data, 'tests')
  send_screenshot(@screenshot_path) if @screenshot_path
end

#test_started(scenarion = nil) ⇒ Object



100
101
102
# File 'lib/results_keeper.rb', line 100

def test_started(scenarion=nil)
  @test_started_at = Time.now
end