require 'savon' require 'json' module RLib class SpiraAgent @cookies @client @test_name_table @project_id # Initializes Spira Agent, set up savon client and makes connection to the Service point def initialize(user_name, password, spira_wsdl, project_id = nil, folder_id = nil) @client = Savon.client(wsdl: spira_wsdl) connection = @client.call(:connection_authenticate, message: {user_name: user_name, password: password}) @cookies = connection.http.cookies #Retrieves the table which maps test names to the unique test IDs automatically generated by spira (can be found in test URL) if project_id != nil and folder_id != nil set_test_name_table(project_id, folder_id) @project_id = project_id end end # Returns the table with test names mapped to test IDs def get_test_table_from_spira_by_folder(project_id, folder_id) #Connects to project Id @client.call(:connection_connect_to_project, message: {project_id: project_id}, cookies: @cookies) resp = @client.call(:test_case_retrieve_by_folder, message: {test_casefolder_id: folder_id}, cookies: @cookies) # gets the response body and retrieves an array with the test cases test_cases = resp.body[:test_case_retrieve_by_folder_response][:test_case_retrieve_by_folder_result][:remote_test_case] test_table = {} # for each test case slice the name and the ID of the test case and save it in the table test_cases.each do |test| test_table[test[:name]] = test[:test_case_id] end return test_table end # Returns Test information from a specific Test Case def get_test_case_from_spira_by_id(project_id, test_case_id) test_case = {} @client.call(:connection_connect_to_project, message: {project_id: project_id}, cookies: @cookies) resp = @client.call(:test_case_retrieve_by_id, message: {test_case_id: test_case_id}, cookies: @cookies) # getting the response body response_body = resp.body[:test_case_retrieve_by_id_response][:test_case_retrieve_by_id_result] test_case['tcDescription'] = response_body[:description].gsub( %r{]+?>}, '' ) test_case['tcName'] = response_body[:name] steps_data = response_body[:test_steps][:remote_test_step] steps = [] # Parses array and returns result if steps_data # if multiple steps if steps_data.kind_of?(Array) steps_data.each_with_index{|step| temp_step = {} temp_step['tsExpectedResult'] = step[:expected_result] temp_step['tsDescription'] = step[:description].gsub('
',"\n").gsub(/<[^>]*>/,'').strip steps.push temp_step } else temp_step = {} temp_step['tsExpectedResult'] = steps_data[:expected_result] temp_step['tsDescription'] = steps_data[:description].gsub('
',"\n").gsub(/<[^>]*>/,'').strip steps.push temp_step end end test_case['tcSteps'] = steps return test_case end # Returns the expected result in array for a given project and test case in JSON format def get_expected_result_by_id(project_id, test_case_id) test_case = self.get_test_case_from_spira_by_id(project_id, test_case_id) steps = test_case['tcSteps'] expected = [] expected_hash = {} steps.each{|le_step| # expected.push JSON.parse(le_step['tsExpectedResult']) le_step['tsExpectedResult'].split('
').each { |item| expected.push(item.gsub( %r{]+?>}, '' )) } } expected.each { |item| items = item.split(':'); expected_hash.merge!({items[0] => items[1] })} { :expected_results => expected_hash, :test_case_name => test_case['tcName'], :test_case_description => test_case['tcDescription'] } end # Retrieves the expected result for a specific test case # To get the expected results first initialize test names by calling set_test_name_table(projectId, folderId) method # and provide the ID of the project and the ID of the folder that contains the test cases. Once the test tables are # initialized get the expected result by simply providing the name of the desired test case. def get_expected_result_by_name(test_case_name) if @test_name_table == nil raise 'ERROR: test_name_table is not set, call set_test_name_table(project_id, folder_id) method to initialize the '+ 'test_name_table and project ID. Alternatively you can call get_expected_result_by_id(project_id, test_case_id) or' + 'set_test_name_table(project_id, folder_id) to set test_name_table.' end test_id = @test_name_table[test_case_name] if test_id == nil raise "ERROR: Program returned empty ID for the test name \"#{test_case_name}\"" end get_expected_result_by_id(@project_id, test_id) end # Sets the test_name_table variable to the map that contains mapping of test names to the test IDs def set_test_name_table(project_id, folder_id) @project_id = project_id @test_name_table = self.get_test_table_from_spira_by_folder(project_id, folder_id) end private :set_test_name_table end end