Subscribe Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security. Subscribe

How to get status_id from the get_results_

Hi

I am using “get_results_for_case/runId/caseId”
to get the result of the testcase,

I am using Java binding and the result is the below object,
how can I use it same as a Jsonobject and get only the "status_id " value?
any help would be appreciated, thanks in advance

[{“elapsed”:null,“status_id”:1,“created_on”:1583861777,“defects”:null,“assignedto_id”:null,“comment”:null,“id”:198915,“custom_config”:null,“created_by”:254,“version”:null,“custom_step_results”:null,“test_id”:25092830}]

Hi @qatester,
you have to get the label (or further info) for the Status via get_statuses.

[
	{
		"color_bright": 12709313,
		"color_dark": 6667107,
		"color_medium": 9820525,
		"id": 1,
		"is_final": true,
		"is_system": true,
		"is_untested": false,
		"label": "Passed",
		"name": "passed"
	},
	..

Hi @kwirth

Thanks for your answer, but maybe my question is not clear.
I want to parse from the response only the status_id like any jsonobject

some thing like this:

JSONObject jsonObject =client.sendGet(“get_results_for_case/8677/1386325”);
String testStatus = jsonObject.getString(“status_id”);

Hi @qatester,
sorry I got you wrong…
Which error do you retrieve?

int testStatus = jsonObject.getInt(“status_id”);

Might be better, because the id is an integer, not a string.

Hi

I am using exactly the below code

JSONObject jsonObject =(JSONObject) client.sendGet(“get_results_for_case/8677/1386325”);
String testStatus = jsonObject.get(“status_id”).toString();

and I get this error

java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to com.github.openjson.JSONObject
at com.ale.rainbow.mobile_qa.tests.AppiumBaseTestListener.getResultForTestCase(AppiumBaseTestListener.java:104)
at com.ale.rainbow.mobile_qa.tests.AppiumBaseTestListener.onTestSuccess(AppiumBaseTestListener.java:116)
at org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:70)
at org.testng.internal.Invoker.runTestListeners(Invoker.java:1389)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:636)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

Hi,
sorry I missed again the exact call. What you receive is a JSONArray, not a single object where you can get the status_id from. A test can have multiple results in a run, therefore sendGet can return a JSONObject or an JSONArray.

JSONArray jsonArray = (JSONArray ) client.sendGet(“get_results_for_case/8677/1386325”);
JSONObject jsonObject = jsonArray.get(0); // just an example!!!
int testStatus = jsonObject.getInt(“status_id”);

Hi @kwirth

I have tried your solution but not working again :

JSONArray jsonArray = (JSONArray ) client.sendGet(“get_results_for_case/8677/1386325”);
JSONObject jsonObject = (JSONObject) jsonArray.get(0); // just an example!!!
int testStatus = jsonObject.getInt(“status_id”);

the issue is that the SendGet function returns an object and I am not an expert in java , I am not sure how to deal with this object , please help am stuck here???

Again I am having same error:

java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to com.github.openjson.JSONArray
at com.ale.rainbow.mobile_qa.tests.AppiumBaseTestListener.getResultForTestCase(AppiumBaseTestListener.java:104)
at com.ale.rainbow.mobile_qa.tests.AppiumBaseTestListener.onTestSuccess(AppiumBaseTestListener.java:117)

Well, that looks easy now. You simply don’t use the same JSON implementation like Gurock has used for their APIClient. They are using JSON-simple from Google I think.

If you’re using Maven, try this instead of the openjson via github:

 <dependency>
 		<groupId>com.googlecode.json-simple</groupId>
 		<artifactId>json-simple</artifactId>
 		<version>1.1.1</version>
 </dependency>

I haven’t tested it online, but I hope it’s correct.

Hi ,

Thanks @kwirth for you help

Gurock are using this JSON implementation : “org.json.simple”

I did implement a solution as below and now it works , the function will only retrieve the statusId, that am using for some if(statements) while updating test results

public int getResultForTestCase(String testRunId, String testCaseId) {

JSONArray resultsArray = new JSONArray();

try {

resultsArray = (JSONArray) client.sendGet(“get_results_for_case/” + testRunId + “/” + testCaseId);

} catch (IOException | APIException e) {

e.printStackTrace();

}
if (resultsArray.size() != 0) {

JSONObject resultObject = (JSONObject) resultsArray.get(0);

return Integer. parseInt (resultObject.get(“status_id”).toString());

} else{

return 0;

}

}

Thanks again for your help, If you have any comment on my implementation , I could use a good advice

Hi,
damn, my maven reference was correct I think, but my code was using a different implementation. Sorry for the confusion, I usually use my own implementation with fasterxml and not the Gurock implementation…

Regarding your code I have a funtional question. I guess you want to get the status for a test in a run. In detail I think you want to get the latest status as int, like shown in the UI on a run, correct?

Why do I ask? I’m not sure about the order in wich the results will be listed, you can’t be sure there is only one. Probably you will return a different, maybe the oldest result for a test in the run and not the latest. You can simply sort by “created_on”, which is a unix timestamp as integer. The highest value is the newest.

HI @kwirth,

Actually I did not check what if the “status_id” is the latest one , because there is only one status entry in my test for now,
But sure its better to filter it as you suggested with “created_on”.

Thanks
Amel