Class: Cukerail::JunitSender

Inherits:
Object
  • Object
show all
Defined in:
lib/junit_sender.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(junit_file) ⇒ JunitSender

Returns a new instance of JunitSender.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/junit_sender.rb', line 9

def initialize(junit_file)
  if %w(BASE_URL USER PASSWORD).map{|e| ENV["TESTRAIL_#{e}"]}.any?{|e| e=='' || !e}
    raise 'You need to setup Testrail environment parameters see https://bbcworldwide.atlassian.net/wiki/display/BAR/Installing+and+Running+Cukerail' 
  end
  @project_id = ENV['PROJECT_ID']
  @suite_id = ENV['SUITE_ID']
  @testrail_api_client = TestRail::APIClient.new(ENV['TESTRAIL_BASE_URL'],ENV['TESTRAIL_USER'],ENV['TESTRAIL_PASSWORD'])
  @results   = Nokogiri::XML(File.read(junit_file))
  @testcases = Hash.new
  @run_id   = ENV['TESTRUN']
end

Instance Attribute Details

#project_idObject (readonly)

Returns the value of attribute project_id.



8
9
10
# File 'lib/junit_sender.rb', line 8

def project_id
  @project_id
end

#resultsObject (readonly)

Returns the value of attribute results.



8
9
10
# File 'lib/junit_sender.rb', line 8

def results
  @results
end

#run_idObject (readonly)

Returns the value of attribute run_id.



8
9
10
# File 'lib/junit_sender.rb', line 8

def run_id
  @run_id
end

#suite_idObject (readonly)

Returns the value of attribute suite_id.



8
9
10
# File 'lib/junit_sender.rb', line 8

def suite_id
  @suite_id
end

#testcasesObject (readonly)

Returns the value of attribute testcases.



8
9
10
# File 'lib/junit_sender.rb', line 8

def testcases
  @testcases
end

#testrail_api_clientObject (readonly)

Returns the value of attribute testrail_api_client.



8
9
10
# File 'lib/junit_sender.rb', line 8

def testrail_api_client
  @testrail_api_client
end

Instance Method Details

#get_section_id(testsuite, parent_id = nil) ⇒ Object

return nil if a section cannot be fouind or created



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

def get_section_id(testsuite,parent_id=nil)
  # some elements don't have names
  return nil if  testsuite.attributes['name'].value.empty?
  section = sections.detect{|s| s['name'] == testsuite.attributes['name'].value}
  unless section
    section =  testrail_api_client.send_post("add_section/#{project_id}",{suite_id:suite_id,name:testsuite.attributes['name'].value,parent_id:parent_id})
    sections << section
  end
  section['id']
end

#get_sectionsObject



59
60
61
# File 'lib/junit_sender.rb', line 59

def get_sections
  sections = testrail_api_client.send_get("get_sections/#{project_id}&suite_id=#{suite_id}")
end

#get_testcase_id(testcase, section_id) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/junit_sender.rb', line 63

def get_testcase_id(testcase,section_id)
  unless testcases[section_id]
    testcases[section_id] = testrail_api_client.send_get("get_cases/#{project_id}&suite_id=#{suite_id}&section_id=#{section_id}")
  end
  unless testcases[section_id].detect{|tc| tc['title'] == testcase.attributes['name'].value}
    new_tc = testrail_api_client.send_post("add_case/#{section_id}",{title:testcase.attributes['name'].value,type_id:1})
    testcases[section_id] << new_tc
  end
  testcases[section_id].detect{|tc| tc['title'] == testcase.attributes['name'].value}['id']
end

#loadObject



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

def load
  recurse_down(results)
end

#recurse_down(element, level = 0, parent_id = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/junit_sender.rb', line 25

def recurse_down(element,level=0,parent_id=nil)
  if element.name == 'document'
    element.children.each do |child|
      recurse_down(child)
    end
  end
  if element.name == 'testsuite'
    section_id = get_section_id(element,parent_id) 
  end
  element.children.select{|n| n.name=='testsuite'}.each do |testsuite|
    recurse_down(testsuite,level+1,(section_id || parent_id))
  end
  element.children.select{|n| n.name=='testcase'}.each do |testcase|
    case_id = get_testcase_id(testcase,(section_id || parent_id))
    report_result(case_id,testcase) if run_id
  end
end

#report_result(case_id, testcase) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/junit_sender.rb', line 74

def report_result(case_id,testcase)
  if testcase.children.any?{|c| c.name=='failure'}
    failure_text = testcase.children.select{|c| c.name=='failure'}.map{|c| c.text}.join("\n")
    result = {status_id: 5,comment: failure_text}
  else
    result = {status_id: 1,comment: 'passed'}
  end
  testrail_api_client.send_post("add_result_for_case/#{run_id}/#{case_id}",result)
end

#sectionsObject



55
56
57
# File 'lib/junit_sender.rb', line 55

def sections
  @all_sections ||= get_sections
end