Class: Itriagetestrail::TestRailInterface

Inherits:
Object
  • Object
show all
Includes:
Milestones, Projects, Sections, Suites, TestCases, TestPlans, TestResults, TestRuns
Defined in:
lib/itriagetestrail.rb

Direct Known Subclasses

TRCucumber13, TRCucumber20, TRCucumber30, TRMiniTest

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TestResults

#read_batch_file, #send_batch_results_to_testrail, #send_results_to_testrail, #store_result, #store_results_to_batch_file, #update_test_suite

Methods included from TestRuns

#add_testrail_run, #existing_cases_from_description, #existing_cases_from_run, #extend_testrail_run

Methods included from TestPlans

#add_plan_entry, #add_testrail_plan, #append_configuration_ids, #config_id, #configuration_group, #configuration_ids, #configurations

Methods included from TestCases

#add_testrail_test_case, #app_version_label, #associate_result, #jenkins_build_label, #test_name, #testrail_ids, #testrail_test_case_id, #time_zone_label

Methods included from Sections

#add_testrail_section, #testrail_section_id, #testrail_sections

Methods included from Milestones

#fetch_milestone, #milestone_archive_name, #milestone_due_date, #milestone_period_start, #milestone_runs, #normalize_milestone, #normalize_origin, #rename_milestone, #reset_milestone

Methods included from Suites

#add_testrail_suite, #testrail_suite_id, #testrail_suites

Methods included from Projects

#project_by_id, #project_by_name, #projects, #set_project

Constructor Details

#initializeTestRailInterface

Returns a new instance of TestRailInterface.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/itriagetestrail.rb', line 68

def initialize
  @setup = false
  config

  if File.exist?(run_tempfile_name)
    @run_id = File.read(run_tempfile_name)
    @plan_id = File.read(plan_tempfile_name)
    @shared_run = true
  else
    @run_id = 0
    @plan_id = 0
  end

  @milestone_name = normalize_milestone
  @jenkins_build = @testrail_config[:jenkinsBuild]
  @app_version = @testrail_config[:appVersion]
  @suite_name = @testrail_config[:suiteName]

  _time_zone

  _execute

  clear_results
  @submitted = { results: [] }

  @external_results = { results: [] }
  @batch_size = @testrail_config[:batch_size] || 0
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



36
37
38
# File 'lib/itriagetestrail.rb', line 36

def batch_size
  @batch_size
end

#clientObject

Returns the value of attribute client.



29
30
31
# File 'lib/itriagetestrail.rb', line 29

def client
  @client
end

#executeObject

Returns the value of attribute execute.



34
35
36
# File 'lib/itriagetestrail.rb', line 34

def execute
  @execute
end

#external_resultsObject (readonly)

Returns the value of attribute external_results.



38
39
40
# File 'lib/itriagetestrail.rb', line 38

def external_results
  @external_results
end

#poolObject

Returns the value of attribute pool.



35
36
37
# File 'lib/itriagetestrail.rb', line 35

def pool
  @pool
end

#resultsObject (readonly)

Returns the value of attribute results.



37
38
39
# File 'lib/itriagetestrail.rb', line 37

def results
  @results
end

#run_idObject

Returns the value of attribute run_id.



33
34
35
# File 'lib/itriagetestrail.rb', line 33

def run_id
  @run_id
end

#sectionsObject

Returns the value of attribute sections.



30
31
32
# File 'lib/itriagetestrail.rb', line 30

def sections
  @sections
end

#test_case_idsObject

Returns the value of attribute test_case_ids.



32
33
34
# File 'lib/itriagetestrail.rb', line 32

def test_case_ids
  @test_case_ids
end

#test_casesObject

Returns the value of attribute test_cases.



31
32
33
# File 'lib/itriagetestrail.rb', line 31

def test_cases
  @test_cases
end

Instance Method Details

#_executeObject



60
61
62
63
64
65
66
# File 'lib/itriagetestrail.rb', line 60

def _execute
  @execute = if @testrail_config[:user].nil? || @testrail_config[:user].empty?
               false
             else
               testrail_online
             end
end

#_time_zoneObject



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

def _time_zone
  @time_zone = if @testrail_config[:tzinfoTimeZone].nil? || @testrail_config[:tzinfoTimeZone].empty?
                 TZInfo::Timezone.get('UTC')
               else
                 TZInfo::Timezone.get(@testrail_config[:tzinfoTimeZone])
               end
end

#clear_resultsObject



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

def clear_results
  @results = { results: [] }
end

#close_run(run_id = @run_id, message = '') ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/itriagetestrail.rb', line 234

def close_run(run_id = @run_id, message = '')
  # A delay is recommended to prevent an HTTP 400 race condition where final results are still
  # being populated in the TESTRAIL DB while a close_run has been requested.

  close_run_delay = @testrail_config[:close_run_delay] || 5
  sleep close_run_delay.to_i
  @client.send_post("update_run/#{run_id}",
                    include_all: false, case_ids: existing_cases_from_run(run_id),
                    description: 'Timestamp: ' + @time_zone.now.strftime('%m/%d/%Y %I:%M %p') +
                        "\nBranch: #{@testrail_config[:origin]}" + "\n#{message}")

  @client.send_post("close_run/#{run_id}", {})
end

#close_run?Boolean

Returns:

  • (Boolean)


210
211
212
213
214
215
216
217
# File 'lib/itriagetestrail.rb', line 210

def close_run?
  # do not close a run if a run id was supplied.  other test suites related to the job will need it.
  close_run = false
  if @testrail_config[:close_run] == 'true' && (@testrail_config[:run_id].nil? || @testrail_config[:run_id].empty?)
    close_run = true
  end
  close_run
end

#configObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/itriagetestrail.rb', line 101

def config
  time_zone = if ENV['TZINFO_TIME_ZONE'].nil? || ENV['TZINFO_TIME_ZONE'].empty?
                'America/Denver'
              else
                ENV['TZINFO_TIME_ZONE']
              end

  @testrail_config = {
    site: ENV['TESTRAIL_SITE'],
    user: ENV['TESTRAIL_USER'],
    password: ENV['TESTRAIL_PASSWORD'],
    tzinfoTimeZone: time_zone,
    projectId: ENV['TESTRAIL_PROJECT_ID'],
    milestone: ENV['TESTRAIL_MILESTONE'],
    projectName: ENV['APP_NAME'],
    suiteName: ENV['TESTRAIL_SUITE_NAME'],
    run_id: ENV['TESTRAIL_RUN_ID'],
    report_skips: ENV['TESTRAIL_REPORT_SKIPS'],
    close_run: ENV['TESTRAIL_CLOSE_RUN'],
    close_run_delay: ENV['TESTRAIL_CLOSE_RUN_DELAY'],
    origin: ENV['GIT_BRANCH'],
    jenkinsBuild: ENV['BUILD_NUMBER'],
    appVersion: nil,
    config: { ios: ENV['IOS_VERSION'],
              android: ENV['ANDROID_VERSION'],
              androidDevice: ENV['DEVICE_NAME'],
              browser: ENV['BROWSER_NAME'], # Jenkins: SELENIUM_BROWSER
              platform: ENV['PLATFORM'], # Jenkins: SELENIUM_PLATFORM
              browserVersion: ENV['BROWSER_VERSION'] } # Jenkins: SELENIUM_VERSION
  }
end

#create_run_idObject



177
178
179
180
# File 'lib/itriagetestrail.rb', line 177

def create_run_id 
  # TODO: Look into configuration_ids to add a plan
  add_testrail_run if @run_id.to_i.zero?
end

#delete_temp_filesObject

This method is only used publicly



220
221
222
223
# File 'lib/itriagetestrail.rb', line 220

def delete_temp_files
  File.delete(run_tempfile_name) if File.exist?(run_tempfile_name)
  File.delete(plan_tempfile_name) if File.exist?(plan_tempfile_name)
end

#initialize_temp_filesObject

This method is only used publicly



226
227
228
229
230
231
232
# File 'lib/itriagetestrail.rb', line 226

def initialize_temp_files
  Dir.mkdir('./tmp') unless File.exist?('./tmp')
  File.write(run_tempfile_name, @run_id)
  File.write(plan_tempfile_name, @plan_id)

  @shared_run = true
end

#initialize_variablesObject



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

def initialize_variables
  make_connection
  # Set the project id
  set_project
  # Get the test rail suites
  testrail_suites
  # Get the test rail sections
  testrail_sections
  # Get the test rail ids
  testrail_ids true
  # Populate configurations
  configurations
end

#make_connectionObject



204
205
206
207
208
# File 'lib/itriagetestrail.rb', line 204

def make_connection
  @client = TestRail::APIClient.new(@testrail_config[:site])
  @client.user = @testrail_config[:user]
  @client.password = @testrail_config[:password]
end

#plan_tempfile_nameObject



200
201
202
# File 'lib/itriagetestrail.rb', line 200

def plan_tempfile_name
  "./tmp/#{@testrail_config[:jenkinsBuild]}_testrail_plan_id"
end

#run_tempfile_nameObject



196
197
198
# File 'lib/itriagetestrail.rb', line 196

def run_tempfile_name
  "./tmp/#{@testrail_config[:jenkinsBuild]}_testrail_run_id"
end

#setupObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/itriagetestrail.rb', line 155

def setup
  return false if setup?
  return false unless execute

  @results = { results: [] }
  @submitted = { results: [] }

  @external_results = { results: [] }
  @batch_size = @testrail_config[:batch_size] || 0

  initialize_variables

  @milestone_id = fetch_milestone(@milestone_name)

  reset_milestone(@milestone_name)

  #Supports tagging
  @testrail_case_fields = @client.send_get('get_case_fields')
  @pool = Pool.new(1)
  @setup = true
end

#setup?Boolean

Returns:

  • (Boolean)


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

def setup?
  @setup
end

#shutdown(message = '') ⇒ Object



248
249
250
251
252
# File 'lib/itriagetestrail.rb', line 248

def shutdown(message = '')
  @pool.shutdown

  close_run(message) if close_run?
end

#tag_ids(tag_names = []) ⇒ Object

These are generic, hardcoded tag translations that map to test ids in TestRail



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/itriagetestrail.rb', line 134

def tag_ids(tag_names = [])
  tag_ids = []
  tag_names.each do |tag_name|
    case tag_name
    when '@wip'
      tag_ids << 1
    when '@flaky'
      tag_ids << 2
    when '@broken'
      tag_ids << 3
    when '@missingTheSauce'
      tag_ids << 4
    end
  end
  tag_ids
end

#testrail_onlineObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/itriagetestrail.rb', line 48

def testrail_online
  # This is the very first call to TestRail
  make_connection
  projects
  if @client.response_code == '200' || @client.response_code.nil?
    true
  else
    puts "**** TESTRAIL IS OFFLINE for maintenance or other reason with status code #{@client.response_code}"
    false
  end
end