Class: ApiTestRail

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

Overview

TODO: CLEAN UP or DRY testruns and testplans methods since can be combined.

Refactor attr_accessor variables to be class variables
Implement other API calls

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ ApiTestRail

Returns a new instance of ApiTestRail.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/trail_marker/api_testrail.rb', line 8

def initialize(client)
  @client = client
  @projects = get_projects()
  @project = nil
  @suites = nil
  @suite = nil
  @plans = nil
  @plan = nil
  @runs = nil
  @run = nil
  @test_cases = nil
  @test_case = nil
  @milestones = nil
  @milestone = nil
  @statuses = get_statuses()
end

Instance Attribute Details

#project_idObject

Returns the value of attribute project_id.



6
7
8
# File 'lib/trail_marker/api_testrail.rb', line 6

def project_id
  @project_id
end

#run_idObject

Returns the value of attribute run_id.



6
7
8
# File 'lib/trail_marker/api_testrail.rb', line 6

def run_id
  @run_id
end

#section_idObject

Returns the value of attribute section_id.



6
7
8
# File 'lib/trail_marker/api_testrail.rb', line 6

def section_id
  @section_id
end

#suite_idObject

Returns the value of attribute suite_id.



6
7
8
# File 'lib/trail_marker/api_testrail.rb', line 6

def suite_id
  @suite_id
end

#test_case_idObject

Returns the value of attribute test_case_id.



6
7
8
# File 'lib/trail_marker/api_testrail.rb', line 6

def test_case_id
  @test_case_id
end

Instance Method Details

#create_milestone(proj_name, msname) ⇒ Object

Makes a post call to create a new milestone



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/trail_marker/api_testrail.rb', line 243

def create_milestone(proj_name, msname)
  proj_id = get_project_id(proj_name)
  if ! milestone_exists?(proj_id, msname)
    puts "Create Milestones #{msname} in Project #{proj_name} "
    unix_timestamp = Time.now.to_i 
    req_field = {:name => msname, :due_on => unix_timestamp}
    create_milestone_req = "add_milestone/" + proj_id.to_s
    request_post(create_milestone_req, req_field)
    sleep(2)
    get_milestones(proj_id)
  end
end

#create_testrun(proj_name, msname, trname, tsname) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/trail_marker/api_testrail.rb', line 264

def create_testrun(proj_name, msname, trname, tsname)
  proj_id = get_project_id(proj_name)
  if ! run_exists?(proj_id, trname)
    if suite_exists?(proj_id, tsname)
      ms_id = nil
      if ! msname.nil? && msname != ""
        ms_id = get_milestone_id(proj_id, msname)
      end
      puts "MSID: #{msname} #{ms_id}"
      ts_id = get_suite_id(proj_id, tsname)
      req_field = {:name => trname, :suite_id => ts_id, :description => "AutoCreated"}
      if ! ms_id.nil? && ms_id != ""
        req_field[:milestone_id] = ms_id
      end
      add_run_req = "add_run/" + proj_id.to_s
      request_post(add_run_req, req_field)
      puts "CREATING RUN under #{proj_name} #{req_field}"
      sleep(2)
      get_runs(proj_id)
    else
      puts "CANNOT Create New TestRun, (-s) test suite name needed."
    end
  else
    puts "CANNOT Create RUN #{trname}, already exists."
  end
end

#delete_milestone(proj_name, msname) ⇒ Object



256
257
258
259
260
261
262
# File 'lib/trail_marker/api_testrail.rb', line 256

def delete_milestone(proj_name, msname)
  proj_id = get_project_id(proj_name)
  ms_id = get_milestone_id(proj_id, msname)
  del_ms_req = "delete_milestone/" + ms_id.to_s
  request_post(del_ms_req, nil)
  exit
end

#get_milestone_id(project_id, milestone_name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/trail_marker/api_testrail.rb', line 41

def get_milestone_id(project_id, milestone_name)
  ms_id = nil
  if ! milestone_exists?(project_id, milestone_name)
    puts "\nMilestone '#{milestone_name}' WAS NOT FOUND, select from available milestones."
    resp_ms = Response.new(@milestones)
    milestone_name = resp_ms.picker("name")
    #ms_id = search_array_kv(@milestones, 'name', milestone_name, 'id')
  end
  ms_id = search_array_kv(@milestones, 'name', milestone_name, 'id')
  return ms_id
end

#get_milestones(proj_id) ⇒ Object



94
95
96
97
# File 'lib/trail_marker/api_testrail.rb', line 94

def get_milestones(proj_id)
  ms_req = "get_milestones/" + proj_id.to_s
  @milestones = request_get(ms_req)
end

#get_plan(plan_id) ⇒ Object



109
110
111
112
# File 'lib/trail_marker/api_testrail.rb', line 109

def get_plan(plan_id)
  plan_req = "get_plan/" + plan_id.to_s
  @plan = request_get(plan_req)
end

#get_plans(proj_id) ⇒ Object



104
105
106
107
# File 'lib/trail_marker/api_testrail.rb', line 104

def get_plans(proj_id)
  plans_req = "get_plans/" + proj_id.to_s
  @plans = request_get(plans_req)
end

#get_project_id(project_name) ⇒ Object

Returns the project ID of the project name passed.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/trail_marker/api_testrail.rb', line 27

def get_project_id(project_name)
  if @projects.nil?
    get_projects()
  end
  project_id = search_array_kv(@projects, 'name', project_name, 'id')
  if project_id.nil?
    puts "\nProject '#{project_name}' WAS NOT FOUND, select from available projects."
    resp_projects = Response.new(@projects)
    project_name = resp_projects.picker("name")
    project_id = search_array_kv(@projects, 'name', project_name, 'id')
  end
  return project_id
end

#get_projectsObject



89
90
91
92
# File 'lib/trail_marker/api_testrail.rb', line 89

def get_projects()
  @projects = request_get('get_projects')
  return @projects
end

#get_runs(proj_id) ⇒ Object



99
100
101
102
# File 'lib/trail_marker/api_testrail.rb', line 99

def get_runs(proj_id)
  runs_req = "get_runs/" + proj_id.to_s
  @runs = request_get(runs_req)
end

#get_statusesObject

Makes an API call to get all possible results status. Currently has 5 passed, blocked, untested, retest, failed. Puts the name and status in @statuses array.



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/trail_marker/api_testrail.rb', line 164

def get_statuses()
  @statuses = []
  status_req = request_get('get_statuses')
  status_req.each do |status|
    status_hash  ={}
    status_hash['id'] = status['id']
    status_hash['name'] = status['name']
    @statuses.push(status_hash)
  end
  return @statuses
end

#get_suite_id(proj_id, suite_name) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/trail_marker/api_testrail.rb', line 81

def get_suite_id(proj_id, suite_name)
  suite_id = nil
  if suite_exists?(proj_id, suite_name)
    suite_id = search_array_kv(@suites, 'name', suite_name, 'id')
  end
  return suite_id
end

#get_suites(proj_id) ⇒ Object



114
115
116
117
# File 'lib/trail_marker/api_testrail.rb', line 114

def get_suites(proj_id)
  suite_req = "get_suites/" + proj_id.to_s
  @suites = request_get(suite_req)
end

#get_test_runplan_id(project_id, runplan_name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/trail_marker/api_testrail.rb', line 53

def get_test_runplan_id(project_id, runplan_name)
  if @runs.nil?
    get_runs(project_id)
  end
  run_id = search_array_kv(@runs, 'name', runplan_name, 'id')
  if run_id.nil?
    puts "\nTest Run WAS NOT FOUND, select from available."
    resp_runs = Response.new(@runs)
    runplan_name = resp_runs.picker("name")
    run_id = search_array_kv(@runs, 'name', runplan_name, 'id')
  end
  return run_id
end

#get_testid(run_id, case_id) ⇒ Object



217
218
219
220
221
222
223
224
# File 'lib/trail_marker/api_testrail.rb', line 217

def get_testid(run_id, case_id)
  puts "SEARCH FOR TESTID: #{run_id}  :: #{case_id}"
  tests_req = 'get_tests/' + run_id.to_s
  tests_resp = request_get(tests_req)
  puts "GET_TESTS: #{tests_resp}"
  id_equiv = search_array_kv(tests_resp, 'case_id', case_id.to_i, 'id')
  return id_equiv
end

#get_testplan_id(project_id, testplan_name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/trail_marker/api_testrail.rb', line 67

def get_testplan_id(project_id, testplan_name)
  if @plans.nil?
    get_plans(project_id)
  end
  plan_id = search_array_kv(@plans, 'name', testplan_name, 'id')
  if plan_id.nil?
    puts "\nTestPlan #{testplan_name} WAS NOT FOUND, select from available."
    resp_plans = Response.new(@plans)
    plan_name = resp_plans.picker("name")
    plan_id = search_array_kv(@plans, 'name', plan_name, 'id')
  end
  return plan_id
end

#get_testplan_run_ids(project_id, testplan_name) ⇒ Object

Returns array of id for testruns inside a testplan



196
197
198
199
# File 'lib/trail_marker/api_testrail.rb', line 196

def get_testplan_run_ids(project_id, testplan_name)
  testplan_id = get_testplan_id(project_id, testplan_name)
  get_testplan_testruns(testplan_id)
end

#get_testplan_testruns(testplan_id) ⇒ Object

Returns an array of testrun IDs that is inside a testplan.



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/trail_marker/api_testrail.rb', line 182

def get_testplan_testruns(testplan_id)
  run_ids = []
  plan_resp = get_plan(testplan_id)
  tp_entries = plan_resp['entries']
  tp_entries.each do |entry|
    entry_runs = entry['runs']
    entry_runs.each do |erun|
      run_ids.push(erun['id'])
    end
  end
  return run_ids
end

#markoff_test(case_id, run_id, pass_status, comment_txt) ⇒ Object

TODO: Parse XML for failed reason and add to comment(?)



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/trail_marker/api_testrail.rb', line 230

def markoff_test(case_id, run_id, pass_status, comment_txt)
  status_id = search_array_kv(@statuses, 'name', 'failed', 'id')
  defect_txt = ""
  if pass_status
    status_id = search_array_kv(@statuses, 'name', 'passed', 'id')
    defect_txt = ""
  end
  equiv_json = {:status_id => status_id, :comment => comment_txt, :defects => defect_txt}
  add_result_req = "add_result_for_case/" + run_id.to_s + "/" + case_id.to_s
  request_post(add_result_req, equiv_json)
end

#markoff_testplan(case_id, testplan_id, pass_status) ⇒ Object

NOTE - NOT USED RIGHT NOW. Marks a test case inside a testplan. Testplans adds extra API calls because testruns are inside the testplans and are not named. See note below MARK_TESTPLAN



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/trail_marker/api_testrail.rb', line 205

def markoff_testplan(case_id, testplan_id, pass_status)
  status_id = search_array_kv(@statuses, 'name', 'failed', 'id')
  defect_txt = ""
  if pass_status
    status_id = search_array_kv(@statuses, 'name', 'passed', 'id')
    defect_txt = ""
  end
  equiv_json = {:status_id => status_id, :comment => 'Auto marker.', :defects => defect_txt}
  add_result_req = "add_result_for_case/" + run_id.to_s + "/" + case_id.to_s
  request_post(add_result_req, equiv_json)
end

#milestone_exists?(project_id, milestone_name) ⇒ Boolean

VERIFY

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/trail_marker/api_testrail.rb', line 121

def milestone_exists?(project_id, milestone_name)
  it_exists = false
  if @milestones.nil?
    get_milestones(project_id)
  end
  puts "ALL MILES: #{@milestones}"
  ms_id = search_array_kv(@milestones, 'name', milestone_name, 'id')
  if ! ms_id.nil?
    it_exists = true
  end
  return it_exists
end

#run_exists?(proj_id, run_name) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
137
138
139
140
141
142
143
144
# File 'lib/trail_marker/api_testrail.rb', line 134

def run_exists?(proj_id, run_name)
  it_exists = false
  if @runs.nil? 
    @runs = get_runs(proj_id)
  end
  run_id = search_array_kv(@runs, 'name', run_name, 'id')
  if ! run_id.nil?
    it_exists = true
  end
  return it_exists
end

#suite_exists?(proj_id, suite_name) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
154
155
156
# File 'lib/trail_marker/api_testrail.rb', line 146

def suite_exists?(proj_id, suite_name)
  it_exists = false
  if @suites.nil? 
    @suites = get_suites(proj_id)
  end
  suite_id = search_array_kv(@suites, 'name', suite_name, 'id')
  if ! suite_id.nil?
    it_exists = true
  end
  return it_exists
end