class OnlyofficeTestrailWrapper::Testrail2
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
Constants
- CONFIG_LOCATION
@return [String] default config location
Attributes
Attribute to write admin_pass
Attribute to write admin_user
@return [Hash] project information
Public Class Methods
# File lib/onlyoffice_testrail_wrapper/testrail.rb, line 69 def admin_pass read_keys @admin_pass end
# File lib/onlyoffice_testrail_wrapper/testrail.rb, line 64 def admin_user read_keys @admin_user end
# File lib/onlyoffice_testrail_wrapper/testrail.rb, line 74 def get_testrail_address read_keys unless testrail_url testrail_url end
Perform http get on address @param [String] request_url to perform http get @return [Hash] Json with result data in hash form
# 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
Perform http post on address @param [String] request_url to perform http get @param [Hash] data_hash headers to add to post query @return [Hash] Json with result data in hash form
# 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
# File lib/onlyoffice_testrail_wrapper/testrail.rb, line 34 def initialize @projects_names = {} end
# 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
endregion
# 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
Public Instance Methods
Check if Testrail connection is available @return [True, False] result of test connection
# File lib/onlyoffice_testrail_wrapper/testrail.rb, line 158 def available? get_projects true rescue StandardError false end
# 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 all projects on testrail @return [Array, ProjectTestrail] array of projects
# 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
# 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 all projects on testrail @return [Array, ProjectTestrail] array of projects
# 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
Initialize project by it's name @param [String] name name of project @return [TestrailProject] project with this name
# 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
region PROJECT
# 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