//RB: This function below gets the tests run id public static JArray GetTestsRunId(JObject tr) { var st = tr.First; string[] id = (st.ToString()).Split(':'); JArray getRun = TestExporter.Get1("get_tests/" + id[1].TrimStart()); return(getRun); }
//RB: This function gets the test cases and add it to the specific run public static JObject GetCasesandAddtoRun(Dictionary <string, Tuple <int, string> > dict, Dictionary <string, string> dictNameSuite, Dictionary <string, string> dictNameSections, string testTypes, object project, object suiteName, object sectionName) { RArray = TestExporter.Get1("get_cases/" + AppSettings[project.ToString()] + "&suite_id=" + dictNameSuite[suiteName.ToString()] + "§ion_id=" + dictNameSections[sectionName.ToString()]); SuiteId = int.Parse(dictNameSuite[suiteName.ToString()]); RunName = suiteName + " " + System.DateTime.Now; Tr = TestExporter.CreateTestRunAddAllCases("add_run/" + AppSettings[project.ToString()], SuiteId, RunName); return(Tr); }
//RB: This function gets the sections name and ID for a specific suite public static Dictionary <string, string> GetSections(object project, Dictionary <string, string> dictNameSuite, string suiteName) { RArray = TestExporter.Get1("get_sections/" + AppSettings[project.ToString()] + "&suite_id=" + dictNameSuite[suiteName]); Dictionary <string, string> dictNameSections = new Dictionary <string, string>(); foreach (var item in RArray.Children()) { var obj = JObject.Parse(item.ToString()); dictNameSections.Add((string)obj.SelectToken("name"), (string)obj.SelectToken("id")); } return(dictNameSections); }
//RB: This function allows to add cases for a dedicated project, suite, section public static void AddCases(Dictionary <string, Tuple <int, string> > dict, string path, string testType, object project, object suiteName, object sectionName, string sectionId) { var dictNameSuite = GetSuites(project); var dictNameSections = GetSections(project, dictNameSuite, suiteName.ToString()); List <string> TestRailLists = new List <string>(); RArray = TestExporter.Get1("get_cases/" + AppSettings[project.ToString()] + "&suite_id=" + dictNameSuite[suiteName.ToString()] + "§ion_id=" + dictNameSections[sectionName.ToString()]); if (RArray.Count == 0) { foreach (string key in dict.Keys) { JObject r = TestExporter.CreateTestName("add_case/" + sectionId, key); JToken s = r.First; string[] t = (s.ToString()).Split(':'); } } else { if (RArray.Count() != dict.Count()) { //In this step, add any tests not present in TestRail in case of 429 exception, you can keep adding tests foreach (var item in RArray.Children()) { var obj = JObject.Parse(item.ToString()); var title = (string)obj.SelectToken("title"); TestRailLists.Add(title.Trim()); } foreach (var elem in dict.Keys.ToList()) { if (!TestRailLists.Contains(elem.Trim())) { JObject r = TestExporter.CreateTestName("add_case/" + sectionId, elem); } } } else { Console.WriteLine("Test Cases have been already added"); } } }
//RB: This function gets the suites name and ID for a specific project public static Dictionary <string, string> GetSuites(object project) { // Load AppSettings from embedded resource (saves distributing the config) using (var stream = typeof(RunandResults).Assembly.GetManifestResourceStream("ResultsParser.appSettings.config")) { var doc = XDocument.Load(stream); foreach (var a in doc.Element("appSettings").Elements("add")) { AppSettings[a.Attribute("key").Value] = a.Attribute("value").Value; } } RArray = TestExporter.Get1("get_suites/" + AppSettings[project.ToString()]); Dictionary <string, string> dictNameSuite = new Dictionary <string, string>(); foreach (var item in RArray.Children()) { var obj = JObject.Parse(item.ToString()); dictNameSuite.Add((string)obj.SelectToken("name"), (string)obj.SelectToken("id")); } return(dictNameSuite); }
//RB: This function is a workaround to delete several tests in a row public static void DeleteTestsWorkAround(Dictionary <string, Tuple <int, string> > testsFromFile, string path, object project, Dictionary <string, string> dictNameSuite, Dictionary <string, string> dictNameSections, object suiteName, object sectionName) { var getCases = TestExporter.Get1("get_cases/" + AppSettings[project.ToString()] + "&suite_id=" + dictNameSuite[suiteName.ToString()] + "§ion_id=" + dictNameSections[sectionName.ToString()]); List <string> ListCasesToDelete = new List <string>(); if (getCases.Count != 0) { // List the tests that are not in the file foreach (var item in getCases.Children()) { var obj = JObject.Parse(item.ToString()); var title = (string)obj.SelectToken("title"); if (!testsFromFile.Keys.Contains(title)) { ListCasesToDelete.Add(title); } } // Create a section TestExporter.CreateSection("add_section/" + AppSettings[project.ToString()], "TestsToBeDeleted"); // Get the section and section Id dictNameSections = GetSections(project, dictNameSuite, suiteName.ToString()); string sectionId = dictNameSections["TestsToBeDeleted"]; // Add the unnecessary tests to the section foreach (var elem in ListCasesToDelete) { JObject r = TestExporter.CreateTestName("add_case/" + sectionId, elem); } // Delete the section TestExporter.Post("delete_section/" + sectionId); } }
//RB: This function below compares the current running tests with the tests currently present in TestRail: // - if the test is not present in TestRail, it will add it, // - if the test is not present in the file, it will delete it from TestRail, // - finally, it will return the correct list of tests cases public static void CompareAndUpdate(Dictionary <string, Tuple <int, string> > testsFromFile, string path, string testType, object project, Dictionary <string, string> dictNameSuite, Dictionary <string, string> dictNameSections, object suiteName, object sectionName) { List <Tuple <string, string> > testRailTestsList = new List <Tuple <string, string> >(); List <Tuple <string, string> > TestcasesWithDuplicates = new List <Tuple <string, string> >(); List <string> fileTestsList = new List <string>(); Dictionary <string, string> difference = new Dictionary <string, string>(); var testsFromTestRails = TestExporter.Get1("get_cases/" + AppSettings[project.ToString()] + "&suite_id=" + dictNameSuite[suiteName.ToString()] + "§ion_id=" + dictNameSections[sectionName.ToString()]); fileTestsList = testsFromFile.Keys.ToList(); //First step, clean the tests that are not in the file foreach (var item in testsFromTestRails.Children()) { var obj = JObject.Parse(item.ToString()); var title = (string)obj.SelectToken("title"); string caseId = (string)obj.SelectToken("id"); testRailTestsList.Add(Tuple.Create(title, caseId)); if (!fileTestsList.Contains(title)) { JObject r = TestExporter.DeleteTestCase("delete_case/" + caseId, caseId); } } //Second step, clean any duplicates in the TestRail section TestcasesWithDuplicates = testRailTestsList.GroupBy(s => s.Item1) .SelectMany(grp => grp.Skip(1)) .ToList(); if (TestcasesWithDuplicates.Count() > 0) { foreach (var dupli in TestcasesWithDuplicates) { JObject r = TestExporter.DeleteTestCase("delete_case/" + dupli.Item2, dupli.Item2); } } testRailTestsList = testRailTestsList.Distinct().ToList(); //RB: This line is currently added as the API tests will only show the failed one if (testType != "API") { //Third step, add any tests not present in TestRail foreach (var elem in fileTestsList) { bool tupleHadProduct = testRailTestsList.Any(m => m.Item1 == elem); if (!tupleHadProduct) { string sectionId = dictNameSections[sectionName.ToString()]; JObject r = TestExporter.CreateTestName("add_case/" + sectionId, elem); } } } else { foreach (var elem in testsFromFile) { bool tupleHadProduct = testRailTestsList.Any(m => m.Item1 == elem.Key); if (elem.Value.Item1 == 5 && !tupleHadProduct) { var testCase = (elem.Key.Length > 250) ? elem.Key.Remove(240) : elem.Key; string sectionId = dictNameSections[sectionName.ToString()]; JObject r = TestExporter.CreateTestName("add_case/" + sectionId, testCase); } } } }