Esempio n. 1
0
        //Report test results to TestRails.
        private static void SendResultForAllCasesToTestRails(string status, int testRunId, List <int> testCaseIds)
        {
            StringBuilder json = new StringBuilder();

            json.Append("{\"results\": [");
            int idStatus = TEST_RAILS_STATUSES[status];

            for (int x = 0; x < testCaseIds.Count; x++)
            {
                string comment       = string.Empty;
                string deviceDetails = AutomationMaster.GameMaster.GetDeviceDetails();
                if (status == FAILED_NAME)
                {
                    comment = string.Format("FROM TEST METHOD - {0} - FAILED! Details: {1} [{2}]", AutomationMaster.CurrentTestContext.TestName, AutomationMaster.CurrentTestContext.ErrorDetails, deviceDetails);
                }
                else
                {
                    comment = string.Format("FROM TEST METHOD - {0} - {1} [{2}]", AutomationMaster.CurrentTestContext.TestName, status.ToUpper(), deviceDetails);
                }
                json.AppendLine("{");
                json.Append(string.Format("\"test_id\":{0},", testCaseIds[x]));
                json.Append(string.Format("\"status_id\":{0},", idStatus));
                json.Append(string.Format("\"comment\":\"{0}\"", comment));
                json.Append("}");
                if (x + 1 < testCaseIds.Count)
                {
                    json.Append(",");
                }
            }
            json.Append("]");
            json.Append("}");

            TestRailsAPIClient client = new TestRailsAPIClient(GameMaster.BASE_TEST_RAILS_URL);

            AutoConsole.PostMessage(client.SendPost(string.Format("add_results/{0}", testRunId), json.ToString()));

            for (int t = 0; t < testCaseIds.Count; t++)
            {
                string jsonResult = client.GetTestName(testCaseIds[t]);
                //Only report as failure if assertion has not already failed, leading to these test cases being marked as fails.
                if (!Q.assert.IsFailing)
                {
                    AutomationMaster.CurrentTestContext.AddAssertion(string.Format("<a href='{0}{1}'>{2}</a>", TestRailsAPIClient.GetUrl(), testCaseIds[t], jsonResult));
                }
                else
                {
                    AutomationMaster.CurrentTestContext.AddTestCaseAssertionOnFailure(string.Format("**TRY_FAIL**<a href='{0}{1}'>{2}</a>", TestRailsAPIClient.GetUrl(), testCaseIds[t], jsonResult));
                }
            }
        }
        static string ResetTestRailsTestStatuses(List <string> args)
        {
            if (args.First().ToInt() > 0)
            {
                TestRailsAPIClient client = new TestRailsAPIClient(GameMaster.BASE_TEST_RAILS_URL);
                if (client == null)
                {
                    string message = "COULD NOT RESET RAILS! New API Client could not be instantiated.";
                    AutoConsole.PostMessage(message, MessageLevel.Abridged);
                    return(message);
                }
                string               json        = client.SendGet(string.Format("get_tests/{0}", args.First()));
                System.Object        jsonObj     = Json.Deserialize(json);
                List <System.Object> list        = (List <System.Object>)jsonObj;
                StringBuilder        jsonUpdates = new StringBuilder();
                jsonUpdates.Append("{\"results\": [");

                for (int i = 0; i < list.Count; i++)
                {
                    Dictionary <string, object> item = (Dictionary <string, object>)list[i];
                    int id       = item["id"].ToString().ToInt();
                    int statusId = item["status_id"].ToString().ToInt();

                    //If test is not marked as Blocked.
                    if (statusId != 2)
                    {
                        jsonUpdates.AppendLine("{");
                        jsonUpdates.Append(string.Format("\"test_id\":{0},", id));
                        jsonUpdates.Append("\"status_id\":4,");
                        jsonUpdates.Append("\"comment\":\"Resetting test for next run.\"");
                        jsonUpdates.Append("}");
                        if (i + 1 < list.Count)
                        {
                            jsonUpdates.Append(",");
                        }
                    }
                }

                jsonUpdates.Append("]");
                jsonUpdates.Append("}");
                string jsonFinal = jsonUpdates.ToString().Replace("},]", "}]");
                client.SendPost(string.Format("add_results/{0}", args.First()), jsonFinal);

                return("Test statuses set to Untested. Please check the test run in your browser to confirm.");
            }
            else
            {
                return("The provided TestRails TestRunID must be a valid integer.");
            }
        }