gurock / testrail-api

TestRail API: Examples on how to use TestRail's API from various languages
Other
116 stars 86 forks source link

com.qa.utils.APIException: TestRail API returned HTTP 400("Field :case_id is not a valid ID.") #31

Open wrathmelior opened 2 years ago

wrathmelior commented 2 years ago

Getting 'TestRail API returned HTTP 400("Field :case_id is not a valid ID.") It should be getting id="188056" set by the TestRails annotation and i'm not sure if it's just not getting that at all and where it is getting case_id from even because I only use caseId it my test.

Screenshot https://monosnap.com/file/ooAnQRTmQZw8bDA7lMT79pBXuEMqNz Im using the instructions here https://www.swtestacademy.com/integrate-test-automation-results-with-testrail-testng/

Here is the code:

  public void beforeTest(ITestContext ctx,Method method) throws NoSuchMethodException {
        Method m = OnboardingTests.class.getMethod(method.getName());
        if (m.isAnnotationPresent(TestRails.class)) {
            TestRails ta = m.getAnnotation(TestRails.class);
            ctx.setAttribute("caseId",ta.id());
        }

}

@AfterMethod public void afterTest(ITestResult result, ITestContext ctx) throws IOException, APIException { Map data = new HashMap(); if(result.isSuccess()) { data.put("status_id",1); } else{ data.put("status_id",5); data.put("comment", result.getThrowable().toString()); } String caseId = (String)ctx.getAttribute("caseId"); Long suiteId = (Long)ctx.getAttribute("suiteId"); client.sendPost("add_result_for_case/"+suiteId+"/"+caseId,data); }

@TestRails(id="188056") @Test (groups = "1") public void onBoarding() throws InterruptedException {

test steps ommited }

}

jonrgurock commented 2 years ago

Hi @wrathmelior,

When using add_result_for_case, you should be passing the test run ID and case ID as parameters in your request. Based on your code, you are submitting the suite ID instead of the run ID and this appears to be the cause of the error message.

If you continue to have issues submitting API requests, please reach out to the support team at contact@gurock.com.

The issues for this repository are primarily used for reporting issues with the TestRail API bindings, or other code inside this repository.

Regards, Jon

wrathmelior commented 2 years ago

The 'suite_id' being used here should be the test run id generated earlier in the run in my super class with this code

@BeforeSuite
public void createSuite(ITestContext ctx) throws IOException, APIException {
client = new APIClient("https://company.testrail.com/");
client.setUser("user@company.com");
client.setPassword("password");
long yourmilliseconds = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");    
Date resultdate = new Date(yourmilliseconds);
Map data = new HashMap();
data.put("suite_id", 856);
data.put("include_all", true);
data.put("name" ,"Android BVT Automation Run - " + resultdate);
JSONObject c = null;
System.out.println("data: " + data);
c = (JSONObject)client.sendPost("add_run/"+102,data);
Long suite_id = (Long)c.get("id");
ctx.setAttribute("suiteId",suite_id);
}

https://monosnap.com/file/4ySvninX8fzEmjYEZQK3MU6RvqsZrh https://i.imgur.com/RGmimaL.png

jonrgurock commented 2 years ago

Hi @wrathmelior,

Please reach out to our support team at contact@gurock.com so one of our support engineers can further assist you.

Regards, Jon