Based on the description of your problem, it can be divided into two distinct tasks:
In your scenario, you have a test suite, and you want to identify particular test cases within this suite that have been marked with the customized filed "can_be_automated = true"
Once you've identified these specific test cases, the second part of your problem involves adding them to an already existing test run.
For the first part, to filter and retrieve the specific test cases from your test suite, you can utilize the 'get_cases' API endpoint. The corresponding API query would appear as follows:
GET index.php?/api/v2/get_cases/{project_id}&suite_id={suite_id}
Upon executing this query, the response you receive will be in JSON format. Within this JSON response, you can find the custom field named "custom_can_be_automated".
"cases":[
{
"id":22478,
"title":"Test Case Steps (Text)",
"section_id":2347,
"template_id":1,
"type_id":5,
"priority_id":4,
"milestone_id":null,
"refs":null,
"created_by":36,
"created_on":1691415817,
"updated_by":36,
"updated_on":1692281184,
"estimate":null,
"estimate_forecast":null,
"suite_id":196,
"display_order":4,
"is_deleted":0,
"case_assignedto_id":null,
"custom_automation_type":6,
"custom_can_be_automated":true,
"custom_preconds":"Test Case Step *TEXT* Precondition",
"custom_steps":"%Var3 \r\n%Var4\r\n",
"custom_testrail_bdd_scenario":"",
"custom_expected":"  ",
"custom_steps_separated":null,
"custom_mission":null,
"custom_goals":null,
"comments":[
]
},
{
"id":22494,
"title":"Automated Checkout",
"section_id":2347,
"template_id":9,
"type_id":3,
"priority_id":3,
"milestone_id":null,
"refs":null,
"created_by":36,
"created_on":1691679382,
"updated_by":32,
"updated_on":1694640912,
"estimate":null,
"estimate_forecast":null,
"suite_id":196,
"display_order":5,
"is_deleted":0,
"case_assignedto_id":null,
"custom_automation_type":0,
"custom_can_be_automated":false,
"custom_preconds":null,
"custom_steps":null,
"custom_testrail_bdd_scenario":"",
"custom_expected":null,
"custom_steps_separated":null,
"custom_mission":null,
"custom_goals":null,
"comments":[
]
},
Based on the provided response, you can employ a filter to extract the case IDs based on the 'custom_can_be_automated' attribute.To exemplify the API calls, I'll use the well-known "curl" utility ahead; then you can adapt these to your Javascript environment, where you may be using specific libraries for performing the HTTP request. You can achieve this by using the following curl with the jq command.
curl -H "Content-Type: application/json" -u "$TESTRAIL_EMAIL:$TESTRAIL_PASS" "$TESTRAIL_URL/index.php?/api/v2/get_cases/{project_id}&suite_id={suite_id}" | jq '[.[] | select(.custom_can_be_automated == true)]'
This curl command will fetch the relevant cases based on the 'custom_can_be_automated' attribute.
After obtaining all of these case IDs, you can proceed to add them to the test run using the 'update_run' endpoint. The request for this operation appears as follows:
POST index.php?/api/v2/update_run/{run_id}
The request body should be structured as shown below:
{ "include_all": true, "case_ids": [1, 2, 3, 5, 8] }
The POST request with “curl” utility looks like below:
curl -X POST "https://$TESTRAIL_URL/index.php?/api/v2/update_run/{run_id}"
-H "Content-Type: application/json"
-u "$TESTRAIL_EMAIL:$TESTRAIL_PASS"
-d '{"include_all": true, "case_ids": [1, 2, 3, 5, 8]}'
Once the cases have been successfully added, you will receive a response code of 200 to confirm the operation's success.
Links to the endpoints
https://support.testrail.com/hc/en-us/articles/7077874763156-Runs#updaterun
https://support.testrail.com/hc/en-us/articles/7077292642580-Cases#getcases