Class: OnlyofficeTestrailWrapper::Testrail2

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

Overview

Main class for working with testrail dvd_copy = Project.init_project_by_name(‘AVS Disc Creator’) compete_test_suit= dvd_copy.init_suite_by_name(‘Complete Test Suite’) test_run_from_api = compete_test_suit.start_test_run(‘TestRunName’, “Simple description”) incompleted_test = test_run_from_api.get_incomplete_tests() while(incomplete_test.length > 0)

current_test = incomplete_test.sample
p current_test.title
current_test.add_result(Testrail2::TEST_RESULT_OK, 'description','version')
incomplete_test = test_run_from_api.get_incomplete_tests()

end1

Constant Summary collapse

CONFIG_LOCATION =

Returns default config location.

Returns:

  • (String)

    default config location

"#{Dir.home}/.gem-onlyoffice_testrail_wrapper/config.yml"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTestrail2

Returns a new instance of Testrail2.



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

def initialize
  @projects_names = {}
end

Class Attribute Details

.admin_passObject



69
70
71
72
# File 'lib/onlyoffice_testrail_wrapper/testrail.rb', line 69

def admin_pass
  read_keys
  @admin_pass
end

.admin_userObject



64
65
66
67
# File 'lib/onlyoffice_testrail_wrapper/testrail.rb', line 64

def admin_user
  read_keys
  @admin_user
end

.testrail_urlObject

Returns the value of attribute testrail_url.



39
40
41
# File 'lib/onlyoffice_testrail_wrapper/testrail.rb', line 39

def testrail_url
  @testrail_url
end

Instance Attribute Details

#projects_namesHash

Returns project information.

Returns:

  • (Hash)

    project information



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

def projects_names
  @projects_names
end

Class Method Details

.get_testrail_addressObject



74
75
76
77
# File 'lib/onlyoffice_testrail_wrapper/testrail.rb', line 74

def get_testrail_address
  read_keys unless testrail_url
  testrail_url
end

.http_get(request_url) ⇒ Hash

Perform http get on address

Parameters:

  • request_url (String)

    to perform http get

Returns:

  • (Hash)

    Json with result data in hash form



82
83
84
85
86
87
# File 'lib/onlyoffice_testrail_wrapper/testrail.rb', line 82

def http_get(request_url)
  uri = URI get_testrail_address + request_url
  request = Net::HTTP::Get.new uri.request_uri
  response = send_request(uri, request)
  JSON.parse response.body
end

.http_post(request_url, data_hash = {}) ⇒ Hash

Perform http post on address

Parameters:

  • request_url (String)

    to perform http get

  • data_hash (Hash) (defaults to: {})

    headers to add to post query

Returns:

  • (Hash)

    Json with result data in hash form



93
94
95
96
97
98
99
100
101
# File 'lib/onlyoffice_testrail_wrapper/testrail.rb', line 93

def http_post(request_url, data_hash = {})
  uri = URI get_testrail_address + request_url
  request = Net::HTTP::Post.new uri.request_uri
  request.body = data_hash.to_json
  response = send_request(uri, request)
  return if response.body == ''

  JSON.parse response.body
end

.read_keysObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/onlyoffice_testrail_wrapper/testrail.rb', line 48

def read_keys
  @testrail_url = ENV['TESTRAIL_URL']
  @admin_user = ENV['TESTRAIL_USER']
  @admin_pass = ENV['TESTRAIL_PASSWORD']
  return unless @admin_user.nil? && @admin_pass.nil?

  begin
    yaml = YAML.load_file(CONFIG_LOCATION)
    @testrail_url = yaml['url']
    @admin_user = yaml['user']
    @admin_pass = yaml['password']
  rescue Errno::ENOENT
    raise Errno::ENOENT, "No user of passwords found in #{CONFIG_LOCATION}. Please create correct config"
  end
end

.send_request(uri, request) ⇒ Object

endregion



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/onlyoffice_testrail_wrapper/testrail.rb', line 167

def self.send_request(uri, request)
  request.basic_auth admin_user, admin_pass
  request.delete 'content-type'
  request.add_field 'content-type', 'application/json'
  is_ssl = (uri.scheme == 'https')
  Net::HTTP.start(uri.host, uri.port, use_ssl: is_ssl) do |http|
    attempts = 0
    begin
      response = http.request(request)
    rescue Timeout::Error
      attempts += 1
      retry if attempts < 3
      raise 'Timeout error after 3 attempts'
    rescue StandardError => e
      raise e
    end
    return response
  end
end

Instance Method Details

#available?True, False

Check if Testrail connection is available

Returns:

  • (True, False)

    result of test connection



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

def available?
  get_projects
  true
rescue StandardError
  false
end

#create_new_project(name, announcement = '', show_announcement = true) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/onlyoffice_testrail_wrapper/testrail.rb', line 125

def create_new_project(name, announcement = '', show_announcement = true)
  new_project = HashHelper.parse_to_class_variable(Testrail2.http_post('index.php?/api/v2/add_project', name: StringHelper.warnstrip!(name.to_s), announcement: announcement,
                                                                                                        show_announcement: show_announcement), TestrailProject)
  OnlyofficeLoggerHelper.log "Created new project: #{new_project.name}"
  new_project.instance_variable_set('@testrail', self)
  @projects_names[new_project.name] = new_project.id
  new_project
end

#get_project_by_id(id) ⇒ Array, ProjectTestrail

Get all projects on testrail

Returns:

  • (Array, ProjectTestrail)

    array of projects



144
145
146
147
148
149
# File 'lib/onlyoffice_testrail_wrapper/testrail.rb', line 144

def get_project_by_id(id)
  project = HashHelper.parse_to_class_variable(Testrail2.http_get("index.php?/api/v2/get_project/#{id}"), TestrailProject)
  OnlyofficeLoggerHelper.log("Initialized project: #{project.name}")
  project.instance_variable_set('@testrail', self)
  project
end

#get_project_by_name(name) ⇒ Object



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

def get_project_by_name(name)
  get_projects if @projects_names.empty?
  @projects_names[StringHelper.warnstrip!(name.to_s)].nil? ? nil : get_project_by_id(@projects_names[StringHelper.warnstrip!(name.to_s)])
end

#get_projectsArray, ProjectTestrail

Get all projects on testrail

Returns:

  • (Array, ProjectTestrail)

    array of projects



119
120
121
122
123
# File 'lib/onlyoffice_testrail_wrapper/testrail.rb', line 119

def get_projects
  projects = Testrail2.http_get 'index.php?/api/v2/get_projects'
  @projects_names = HashHelper.get_hash_from_array_with_two_parameters(projects, 'name', 'id') if @projects_names.empty?
  projects
end

#init_project_by_name(name) ⇒ TestrailProject

Initialize project by it’s name

Parameters:

  • name (String)

    name of project

Returns:



137
138
139
140
# File 'lib/onlyoffice_testrail_wrapper/testrail.rb', line 137

def init_project_by_name(name)
  found_project = get_project_by_name name
  found_project.nil? ? create_new_project(name) : found_project
end

#project(name_or_id) ⇒ Object

region PROJECT



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

def project(name_or_id)
  case name_or_id.class.to_s
  when 'Fixnum'
    get_project_by_id name_or_id
  when 'String'
    init_project_by_name name_or_id
  else
    raise 'Wrong argument. Must be name [String] or id [Integer]'
  end
end