Class: TestRail::APIClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ APIClient

Returns a new instance of APIClient.



26
27
28
29
30
31
# File 'lib/testrail.rb', line 26

def initialize(base_url)
  if !base_url.match(/\/$/)
    base_url += '/'
  end
  @url = base_url + 'index.php?/api/v2/'
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



24
25
26
# File 'lib/testrail.rb', line 24

def password
  @password
end

#userObject

Returns the value of attribute user.



23
24
25
# File 'lib/testrail.rb', line 23

def user
  @user
end

Instance Method Details

#add_milestone(project_id, ms_name) ⇒ Object



166
167
168
169
170
# File 'lib/testrail.rb', line 166

def add_milestone(project_id, ms_name)
  data = {"name"=> ms_name + Time.now.strftime("_%H/%M/%S_%d/%m/%Y")}
  response = send_post("add_milestone/#{project_id}", data)
  response['id']
end

#add_result_for_case(run_id, case_id, data) ⇒ Object



151
152
153
# File 'lib/testrail.rb', line 151

def add_result_for_case(run_id, case_id, data)
  send_post("add_result_for_case/#{run_id}/#{case_id}", data)
end

#add_results_for_cases(run_id, test_rail_results) ⇒ Object



76
77
78
79
80
# File 'lib/testrail.rb', line 76

def add_results_for_cases(run_id, test_rail_results)
  data = {}
  data[:results] = test_rail_results
  send_post("add_results_for_cases/#{run_id}", data)
end

#add_run(project_id, suite_id, test_run_name, section_id = nil) ⇒ Object



33
34
35
36
37
# File 'lib/testrail.rb', line 33

def add_run(project_id, suite_id, test_run_name, section_id=nil)
  cases_ids = section_id ? get_all_cases(project_id, suite_id, section_id) : get_cases_for_suite(project_id, suite_id).map {|item| item['id']}
  data = {'suite_id'=> suite_id, 'name' => test_run_name,'include_all'=>false, 'case_ids' => cases_ids }
  send_post("add_run/#{project_id}", data)
end

#build_hash(node, array) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/testrail.rb', line 120

def build_hash(node, array)
  nodes_to_proceed = array.select do |section|
    section['parent_id'] == node['id']
  end

  nodes_to_proceed.each do |n|
    node['entries'] << n
    array.delete n
    build_hash(n, array)
  end
end

#close_milestone(ms_id) ⇒ Object



172
173
174
175
# File 'lib/testrail.rb', line 172

def close_milestone(ms_id)
  data = {'is_completed' => true}
  send_post("update_milestone/#{ms_id}", data)
end

#close_run(run_id, data) ⇒ Object



155
156
157
# File 'lib/testrail.rb', line 155

def close_run(run_id, data)
  send_post("close_run/#{run_id}", data)
end

#full_title(case_hash, sections) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/testrail.rb', line 82

def full_title(case_hash, sections)
  parent_section_id = case_hash['section_id']
  results = []
  section = sections.find do |sec|
    sec['id'] == parent_section_id
  end

  get_full_sections_title_for(section, sections, results)
  results.push(case_hash['title']).join(' ')
end

#get_all_cases(project_id, suite_id, section_id) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/testrail.rb', line 39

def get_all_cases(project_id, suite_id, section_id)
  all_sections = get_all_inherited_sections(project_id, suite_id, section_id)
  cases_ids = []
  all_sections.uniq!
  all_sections.each do |section|
    cases_ids += get_cases_for_section(project_id, suite_id, section)
  end
  cases_ids
end

#get_all_inherited_sections(project_id, suite_id, section_id) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/testrail.rb', line 57

def get_all_inherited_sections(project_id, suite_id, section_id)
  hash = sections_hash(project_id, suite_id)
  to_preceed = hash['entries'].find{|el| el['id'] == section_id}
  results = []
  get_entries_array to_preceed, results
  results.map { |el| el['id']}
end

#get_cases_for_section(project_id, suite_id, section_id) ⇒ Object



53
54
55
# File 'lib/testrail.rb', line 53

def get_cases_for_section(project_id, suite_id, section_id)
  send_get("get_cases/#{project_id}&suite_id=#{suite_id}&section_id=#{section_id}").map {|item| item['id']}
end

#get_cases_for_suite(project_id, suite_id) ⇒ Object



49
50
51
# File 'lib/testrail.rb', line 49

def get_cases_for_suite(project_id, suite_id)
  send_get("get_cases/#{project_id}&suite_id=#{suite_id}")
end

#get_cases_titles(cases, project_id, suite_id) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/testrail.rb', line 65

def get_cases_titles(cases, project_id, suite_id)
  sections = get_sections(project_id, suite_id)
  full_cases = []
  cases.each do |c|
    c['title'] = full_title(c, sections)
    full_cases << c
  end

  full_cases
end

#get_entries_array(node, results) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/testrail.rb', line 106

def get_entries_array(node, results)
  results << node
  node['entries'].each do |entry|
    results << entry
    unless entry['entries'].empty?
      get_entries_array(entry, results)
    end
  end
end

#get_full_sections_title_for(section, sections, results) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/testrail.rb', line 97

def get_full_sections_title_for(section, sections, results)
  results.unshift(section['name'])
  unless section['depth'] == 0
    parent_section = parent_section(section, sections)
    get_full_sections_title_for(parent_section, sections, results)
  end
end

#get_section(section_id) ⇒ Object



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

def get_section(section_id)
  send_get("get_section/#{section_id}")
end

#get_section_id(project_id, suite_id, section_title) ⇒ Object



142
143
144
145
# File 'lib/testrail.rb', line 142

def get_section_id(project_id, suite_id, section_title)
  all_sections = get_sections(project_id, suite_id)
  all_sections.find{|section| section['name'] == section_title}['id']
end

#get_sections(project_id, suite_id) ⇒ Object



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

def get_sections(project_id, suite_id)
  send_get("get_sections/#{project_id}&suite_id=#{suite_id}")
end

#get_untested_tests(run_id) ⇒ Object



186
187
188
# File 'lib/testrail.rb', line 186

def get_untested_tests(run_id)
  send_get("get_tests/#{run_id}&status_id=3")
end

#mark_failed_tests_for_run(run_id, tests_ids) ⇒ Object



177
178
179
180
181
182
183
184
# File 'lib/testrail.rb', line 177

def mark_failed_tests_for_run(run_id, tests_ids)
  data = {}
  data['results'] = []
  tests_ids.each do |id|
    data['results'] << {'test_id'=>id, 'status_id'=>5}
  end
  send_post("add_results/#{run_id}", data)
end

#mark_untested_tests_failed(run_id) ⇒ Object



159
160
161
162
163
164
# File 'lib/testrail.rb', line 159

def mark_untested_tests_failed(run_id)
  tests = get_untested_tests(run_id)
  return if tests.empty?
  tests_ids = tests.map {|t| t['id']}
  mark_failed_tests_for_run(run_id, tests_ids)
end

#parent_section(section, sections) ⇒ Object



116
117
118
# File 'lib/testrail.rb', line 116

def parent_section(section, sections)
  sections.find { |sec| sec['id'] == section['parent_id'] }
end

#sections_hash(project_id, suite_id) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/testrail.rb', line 133

def sections_hash(project_id, suite_id)
  all_sections = get_sections(project_id, suite_id)
  all_sections.map! {|el| el['entries'] =[]; el}
  main = all_sections.find {|el| el['depth'] == 0}
  all_sections.delete(main)
  build_hash(main, all_sections)
  main
end

#send_get(uri) ⇒ Object

Send Get

Issues a GET request (read) against the API and returns the result (as Ruby hash).

Arguments:

uri The API method to call including parameters

(e.g. get_case/1)


202
203
204
# File 'lib/testrail.rb', line 202

def send_get(uri)
  _send_request('GET', uri, nil)
end

#send_post(uri, data) ⇒ Object

Send POST

Issues a POST request (write) against the API and returns the result (as Ruby hash).

Arguments:

uri The API method to call including parameters

(e.g. add_case/1)

data The data to submit as part of the request (as

Ruby hash, strings must be UTF-8 encoded)


219
220
221
# File 'lib/testrail.rb', line 219

def send_post(uri, data)
  _send_request('POST', uri, data)
end