Search code examples
c#json.nettestrail

Invalid cast from JArray to JObject


I've already implemented integration with Selenium -- Testrail. Everything was correct until today when I'm keep receiving:

OneTimeSetUp: System.InvalidCastException : Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'.

Here is the line of code where it fails:

JArray getListOfAllActiveTestRun = (JArray)client.SendGet($"get_runs/{ProjectId}");

As I'm assuming I need to have a JArray to use it later inside foreach loop (Full code view)

if (RunId == null)
{
    JArray getListOfAllActiveTestRun = (JArray)client.SendGet($"get_runs/{ProjectId}");

    foreach (JObject testRun in getListOfAllActiveTestRun)
    {
        bool isCompleted = Convert.ToBoolean(testRun["is_completed"]);
        string lastTestRunId = testRun["id"].ToString();
        string lastSuiteId = testRun["suite_id"].ToString();
        int failedTestCount = testRun["failed_count"].Value<int>();
        int untestedTestCount = testRun["untested_count"].Value<int>();


        if (!isCompleted && lastSuiteId.Equals(SuitId) && failedTestCount > 0 || !isCompleted && lastSuiteId.Equals(SuitId) && untestedTestCount > 0) // we are checking that there is not finished testRun with suitId equal to this and failed tests and untested tests
        {
            RunId = lastTestRunId;
            break;
        }
    }
}

Checked the solutions and most of the time I was facing option with JsonConvert.DeserializeObject but I'm not sure if this is correct hint in my case.

Edit (JSON)

"runs":

      {
         "id":2874,
         "suite_id":878,
         "name":"[ENV: TEST] [BACKOFFICE] Automation Test Run - [20:02:55 PM]",
         "description":null,
         "milestone_id":null,
         "assignedto_id":null,
         "include_all":true,
         "is_completed":false,
         "completed_on":null,
         "config":null,
         "config_ids":[
            
         ],
         "passed_count":171,
         "blocked_count":0,
         "untested_count":1,
         "retest_count":0,
         "failed_count":3,
         "custom_status1_count":0,
         "custom_status2_count":0,
         "custom_status3_count":0,
         "custom_status4_count":0,
         "custom_status5_count":0,
         "custom_status6_count":0,
         "custom_status7_count":0,
         "project_id":19,
         "plan_id":null,
         "created_on":1631901776,
         "updated_on":1631901776,
         "refs":null,
         "created_by":124,
         "url":"ssss"
      }

Solution

  • I've been encountering the same problem starting from today as well -- it seems like there's been a change in TestRail's API.

    From TestRail's API reference: "...These bulk endpoints will no longer return an array of all entities, but will instead return an object with additional pagination fields and an array of up to 250 entities."

    Source: https://www.gurock.com/testrail/docs/api/reference/runs#getruns