Class: Testrail::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Client

Returns a new instance of Client.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/testrail_export/client.rb', line 32

def initialize(args)
  @projects = nil
  @sections = Hash.new
  @suite   = Hash.new
  @suites   = Hash.new

  @client = APIClient.new args[:url]
  %w(user password project).each do |key|
    raise Exception.new("TestRail configuration key :#{key} not set. Cannot continue without it.") if args[key.intern].nil?
    @client.send "#{key}=", args[key.intern] if %w(user password).include? key
  end
end

Instance Method Details

#add_project(project_name, suite_mode = 1) ⇒ Object



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

def add_project(project_name, suite_mode=1)
  @projects = nil # invalidate cached stuff
  @client.send_post('add_project', { name: project_name,
                                     announcement: AUTOMATED_DESCRIPTION,
                                     show_anouncement: true,
                                     suite_mode: suite_mode })
end

#add_results_for_cases(run_id, results) ⇒ Object



147
148
149
# File 'lib/testrail_export/client.rb', line 147

def add_results_for_cases(run_id, results)
  @client.send_post("add_results_for_cases/#{run_id}", { results: results })
end

#create_case(title, section_id) ⇒ Object



133
134
135
# File 'lib/testrail_export/client.rb', line 133

def create_case(title, section_id)
  @client.send_post("add_case/#{section_id}", { title: title })
end

#create_run(suite, prefix = nil) ⇒ Object

—————————————————-> runs <——————————————————



139
140
141
142
143
144
145
# File 'lib/testrail_export/client.rb', line 139

def create_run(suite, prefix=nil)
  trailer = suite['name'] == 'Master' ? nil : suite['name']
  run_name = [prefix || nice_time_now, trailer].compact.join(' - ')
  @client.send_post("add_run/#{suite['project_id']}", { suite_id: suite['id'],
                                                        name: run_name,
                                                        description: 'Automated tests execution'})
end

#create_section(name, suite, parent_id) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/testrail_export/client.rb', line 110

def create_section(name, suite, parent_id)
  @sections.delete(suite['id']) # invalidate cached values
  # TODO: check if JSON created have null for nil parent_id
  @client.send_post("add_section/#{suite['project_id']}", { name: name,
                                                            suite_id: suite['id'],
                                                            description: AUTOMATED_DESCRIPTION,
                                                            parent_id: parent_id })
end

#create_suite(name, project_id) ⇒ Object



81
82
83
84
85
# File 'lib/testrail_export/client.rb', line 81

def create_suite(name, project_id)
  @suites.delete(project_id) # invalidate cached stuff
  puts "TestRail Exporter [INFO] Creating suite: #{name} under project: #{ self.get_project(project_id)['name'] }"
  @client.send_post("add_suite/#{project_id}", { name: name, description: AUTOMATED_DESCRIPTION })
end

#find_case(title, section, depth) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/testrail_export/client.rb', line 125

def find_case(title, section, depth)
  suite = self.get_suite(section['suite_id'])
  test_cases = @client.send_get("get_cases/#{suite['project_id']}&suite_id=#{suite['id']}")
  test_cases.find do |test_case|
    test_case['title'] == title && test_case['section_id'] == section['id']
  end
end

#find_or_create_case(title, section, depth) ⇒ Object

—————————————————-> cases <—————————————————–



121
122
123
# File 'lib/testrail_export/client.rb', line 121

def find_or_create_case(title, section, depth)
  self.find_case(title, section, depth) || self.create_case(title, section['id'])
end

#find_or_create_section(name, suite, parent, depth) ⇒ Object



105
106
107
108
# File 'lib/testrail_export/client.rb', line 105

def find_or_create_section(name, suite, parent, depth)
  parent_id = (depth == 0) ? nil : parent['id']
  self.find_section(name, suite, parent_id) || self.create_section(name, suite, parent_id)
end

#find_or_create_suite(name, project_id) ⇒ Object



87
88
89
# File 'lib/testrail_export/client.rb', line 87

def find_or_create_suite(name, project_id)
  self.find_suite_by_name(name, project_id) || self.create_suite(name, project_id)
end

#find_section(name, suite, parent_id) ⇒ Object



101
102
103
# File 'lib/testrail_export/client.rb', line 101

def find_section(name, suite, parent_id)
  get_sections(suite).select{ |s| s['parent_id'] == parent_id }.find{ |s| s['name'] == name.strip }
end

#find_suite_by_name(name, project_id) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/testrail_export/client.rb', line 73

def find_suite_by_name(name, project_id)
  suites = self.get_suites(project_id).select do |suite|
    suite['name'] == name
  end
  puts "TestRail Exporter [WARN] #{suites.size} suites found with name: #{name}. Using first one." if suites.size > 1
  suites.first
end

#get_project(project_id) ⇒ Object



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

def get_project(project_id)
  self.get_projects.find { |project| project['id'] == project_id }
end

#get_projectsObject

—————————————————> projects —————————————————-



47
48
49
# File 'lib/testrail_export/client.rb', line 47

def get_projects
  @projects ||= @client.send_get('get_projects')
end

#get_sections(suite) ⇒ Object

—————————————————> sections <—————————————————



93
94
95
# File 'lib/testrail_export/client.rb', line 93

def get_sections(suite)
  @sections[suite['id']] ||= @client.send_get("get_sections/#{suite['project_id']}&suite_id=#{suite['id']}")
end

#get_suite(suite_id) ⇒ Object

—————————————————-> suites <—————————————————-



65
66
67
# File 'lib/testrail_export/client.rb', line 65

def get_suite(suite_id)
  @suite[suite_id] ||= @client.send_get("get_suite/#{suite_id}")
end

#get_suites(project_id) ⇒ Object



69
70
71
# File 'lib/testrail_export/client.rb', line 69

def get_suites(project_id)
  @suites[project_id] ||= @client.send_get("get_suites/#{project_id}")
end

#section_ids_at_depth(suite, depth) ⇒ Object



97
98
99
# File 'lib/testrail_export/client.rb', line 97

def section_ids_at_depth(suite, depth)
  self.get_sections(suite).select{ |s| s['depth'] == depth }.map{ |s| s['id'] }
end