using System; using System.Collections.Specialized; using System.Net.Http; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text; using System.Text.Json; using System.Web; // (c) 2016 Real-Time Technology Solutions, Inc. All Rights Reserved. namespace QuerySurgeConnector { class QuerySurgeConnection { //Define Global Variables for your QuerySurge instance string hostName = "127.0.0.1"; string port = "80"; string baseUrl = ""; string sessionId = ""; public QuerySurgeConnection(){ baseUrl = string.Format("http://{0}:{1}/QuerySurge/api", hostName, port) ; } public async Task ExecuteScenarioBySuiteId(string projectId, string scenarioName, string suiteIdList, string outputpath){ //Variable Declarations int timeout = 180; int retryCount = 0; string executionStatus = "Not Run"; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = null; try { client.DefaultRequestHeaders.Add("X-QS-AUTH", sessionId); string url = string.Format(baseUrl + "/project/{0}/execute/suite", projectId); UriBuilder uriBuilder = new UriBuilder(url); NameValueCollection query = HttpUtility.ParseQueryString(string.Empty); List suiteIds = new List(suiteIdList.Split(',')); foreach(string suiteId in suiteIds){ query.Add("id",suiteId); } query.Add("name", scenarioName); uriBuilder.Query = query.ToString(); //Execute a Scenario response = await client.PostAsync(uriBuilder.ToString(), null); response.EnsureSuccessStatusCode(); var responseJson = await response.Content.ReadFromJsonAsync(); string scenarioId = responseJson.guid; //Get scenario outcome using the scenario id obtained above //Loop until scenrio execution has completed or time-out expires url = string.Format(baseUrl + "/project/{0}/status/scenario/{1}", projectId, scenarioId); while((executionStatus == "Not Run" || executionStatus == "Running") && retryCount < timeout){ response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); responseJson = await response.Content.ReadFromJsonAsync(); executionStatus = responseJson.message; Thread.Sleep(1000); retryCount++; } if(executionStatus == "Not Run" || executionStatus == "Running"){ Console.WriteLine("Scenario Execution Timed-Out"); } else{ //Output the execution result status to the console Console.WriteLine("----------------------"); Console.WriteLine("Execution Result: " + executionStatus); Console.WriteLine("----------------------"); //Send REST API request to get the scenario results as html client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html")); url = string.Format(baseUrl + "/project/{0}/results/testmanagement/summary/{1}", projectId, scenarioId); response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); string responseString = await response.Content.ReadAsStringAsync(); string htmlOutput = responseString; //Write the html output to file WriteHTMLOutputResultsToFile(htmlOutput,outputpath); } } catch (HttpRequestException e) { if(response != null){ Console.WriteLine($"Execution error: {await response.Content.ReadAsStringAsync()}"); }else{ Console.WriteLine($"Execution error: {e.Message}"); } } } return executionStatus; } public void WriteHTMLOutputResultsToFile(string htmlOutput, string outputPath) { try { StreamWriter SW = new StreamWriter(outputPath); SW.Write(htmlOutput); SW.Close(); } catch (IOException e) { Console.WriteLine(e.Message); } } public async Task Login(string username, string password) { String url = baseUrl + "/auth/login"; using (HttpClient client = new HttpClient()) { try { var requestBody = new {username = username, password = password}; string json = JsonSerializer.Serialize(requestBody); StringContent content = new StringContent(json, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(url, content); response.EnsureSuccessStatusCode(); // Throws an exception if the status code is not successful string responseString = await response.Content.ReadAsStringAsync(); var responseJson = JsonSerializer.Deserialize(responseString); sessionId = responseJson.sessionId; } catch (HttpRequestException e) { Console.WriteLine($"Login error: {e.Message}"); } } } public async Task Logout() { String url = baseUrl + "/auth/logout"; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = null; try { client.DefaultRequestHeaders.Add("X-QS-AUTH", sessionId); response = await client.PostAsync(url,null); response.EnsureSuccessStatusCode(); // Throws an exception if the status code is not successful string responseBody = await response.Content.ReadAsStringAsync(); } catch (HttpRequestException e) { if(response != null){ Console.WriteLine($"Logout error: {await response.Content.ReadAsStringAsync()}"); }else{ Console.WriteLine($"Logout error: {e.Message}"); } } } } public class GenericDTO{ public string username {get;set;} public string sessionId {get; set;} public string guid {get; set;} public string message {get; set;} } } }