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

Getting started with Testrail API


#1

Hi,
i have just started to work on integration of testrail with selenium using java.I have TEST ID :T1 RUN id: R1 Project id: P1 in testrail.Please let me know how to print title of this testcase as I am using following code:

APIClient client = new APIClient(“http://seleniumautomation.testrail.net”);
client.setUser(“valid_id”);
client.setPassword(“valid_pwd”);
JSONObject c = (JSONObject) client.sendGet(“get_Case/T1”); //line no. 17
System.out.println(c.get(“title”));

i am getting following exception:

Exception in thread “main” com.gurock.testrail.APIException: TestRail API return HTTP 301 (No additional error message received)
at com.gurock.testrail.APIClient.sendRequest(APIClient.java:159)
at com.gurock.testrail.APIClient.sendGet(APIClient.java:91)
at com.gurock.testrail.testrail.main(testrail.java:17)


#2

The 301 response code is used for redirection, so my guess is that the URL in your first line isn’t the root URL of the testrail install. For most installations it’s in the testrail directory, i.e. http://seleniumautomation.testrail.net/testrail


#3

@donalaya good guess!

The problem is actually that you tried to use http:// instead of https://, resulting in a redirect. Make sure you use the correct HTTPS address of: https://seleniumautomation.testrail.net/


#4

Thanxx for your reply…
Now i tried using https and it is able to access the link :smile:

        APIClient client = new APIClient("https://seleniumautomation.testrail.net");
	client.setUser("valid_uname");
	client.setPassword("valid_pwd");
	JSONObject c = (JSONObject) client.sendGet("get_Case/T1");
	System.out.println(c.get("title"));

and getting exception:
Exception in thread “main” com.gurock.testrail.APIException: TestRail API returned HTTP 400(“Field :case_id is not a valid ID.”)
at com.gurock.testrail.APIClient.sendRequest(APIClient.java:216)
at com.gurock.testrail.APIClient.sendGet(APIClient.java:91)
at com.gurock.testrail.testrail.main(testrail.java:17)

Can you please tel me how we give the test case ID as in testrail i have created a Project P1 a Testsuit S1 and testcase having id C1
Then created test run R1 for this test suite And under this test run Testcase id is T1

JSONObject c = (JSONObject) client.sendGet(“get_Case/T1”);
what is the correct id for test case ???


#5

The T, C and Rs for Test, Case, and Runs are not included on the IDs - just the numbers are the ID in the table.


#6

The get_case API method would also expect case IDs (those that start with C, but only the numeric part). Cases can be found on the Test Cases / Test Suites & Cases tab. Test IDs, on the other hand, start with T and the test runs/tests/results can be found on the Test Runs & Results tab.

I hope this helps!

Regards,
Tobias


#7

Thanxx … :smile: …able to update results now… :smile:

I am now trying to update test results using method add_results_for_cases. This method expects an array of test results (via the ‘results’ field).

Can you Please tell how we declare this results field and created in java??is this a json array??

{
“results”: [
{
“case_id”: 1,
“status_id”: 1,

	},
	{
		"case_id": 2,
		"status_id": 1,
		
	}]

}

&

Is this correct t o pass that json results array field as follows:
client.sendPost(“add_result_for_case/1/2”,results);


#8

Hi!

Sure, I’m happy to help. For Java, you would need to use a combination of Map and List objects and then pass this to the sendPost API method. This looks as follows, for example:

http://docs.gurock.com/testrail-api2/bindings-java#examplepost_request

It’s a bit verbose in Java compared to the other bindings/languages. The idea for add_result_for_case is to build a Map object with the results entry pointing to a List (which in turn consists of Map objects with case_id and status_id keys).

Please let me know in case anything is unclear or if you have any further questions.

Regards,
Tobias


How to use add_results to a test run with sendPost() for all test cases?
#9

Hi i am using add_results method to update complete test run,My test run id is 1

JSONArray Results =t.createJsonArrayFromList();
client.sendPost(“add_results/1”,Results) ;

where Results is a json array as follows:

[{“status_id”:“PASS”,“test_id”:“1”},{“status_id”:“PASS”,“test_id”:“2”}]

but not able to update the testcases in testrail and getting exception:

Exception in thread “main” com.Charter.TestRail.APIException: TestRail API returned HTTP 400(“Field :results is a required field.”)


#10

Here are some issues with the call:

Results should be something like:

{results: [{“status_id”:“PASS”,“test_id”:“1”},{“status_id”:“PASS”,“test_id”:“2”}]}

Also, status_id is expecting an int, so you’ll need to use the ID for “PASS”. You can use get_statuses to get the ID for any given status, but for this example, the status_id for Passing is 1. So here’s the corrected result:

{results: [{“status_id”:1,“test_id”:1},{“status_id”:1,“test_id”:2}]}


#11

thnxx … :smile: able to do it now… :slight_smile:


#12

Hi,
Till now i was using my client network to update resuilts in my testrail but When i am trying to connect testrail without using client network i.e on local Network,i am not able to update results.However directly hit URL in browser testrail opens.

APIClient client = new APIClient(“https://seleniumautomation.testrail.net”);

i am getting following exception:
Exception in thread “main” java.net.UnknownHostException: seleniumautomation.testrail.net
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:585)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:641)
at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:275)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1044)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:941)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1223)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1198)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at com.Charter.TestRail.APIClient.sendRequest(APIClient.java:142)
at com.Charter.TestRail.APIClient.sendPost(APIClient.java:115)
at com.Charter.TestRail.testrail.main(testrail.java:62)

can u tell solution for this…??

As for http client i have to use following PROXY DEATILS in eclipse.I think same needed for API CLIENT also.
HttpClient client1 = new HttpClient();
HttpMethod method = new GetMethod(“http://www.google.com”);
HostConfiguration config = client1.getHostConfiguration();

	String PROXY_HOST="10.152.110.10";
	int PROXY_PORT=80;
	config.setProxy(PROXY_HOST, PROXY_PORT);
	String username = "my_uname_local_network"; String password = "my_pwd_local_network";
	Credentials credentials = new UsernamePasswordCredentials(username, password);
	AuthScope authScope = new AuthScope(PROXY_HOST, PROXY_PORT);
	client1.getState().setProxyCredentials(authScope, credentials);

#13

Hi!

This looks like a network/connection related issue. Please make sure that you can reach TestRail from the machine you are testing this with and that the proxy connection you’ve configured works as expected. If you can only access TestRail via your proxy, you might need to add proxy support to TestRail’s APIClient class as well (if you cannot set the proxy globally in Java).

I hope this helps!

Regards,
Tobias


#14

I would like to use Testrail for collecting test results from automated tests. I have a question about the status id. Is there a way to get it automatically during/after the test run or one has to add it manually to each test?


#15

Hello,

Thanks for your posting! The status ID refers to the status you want to submit to TestRail (passed/failed/etc.) and you would need to submit this as part of the API methods to add results:

http://docs.gurock.com/testrail-api2/reference-results

The full list of statuses in TestRail can be queried via get_statuses:

http://docs.gurock.com/testrail-api2/reference-statuses

The standard list of status IDs is 1 for Passed up to 5 for Failed (the list can also be found on the previous website).

I hope this helps!

Regards,
Tobias


#16

Hi,

I have following string and want to add plan for this

String addPlanString = “{“name”: “+projectName+” Test Plan,“entries”: [{“suite_id”: 1234,“include_all”: true,“config_ids”: [“1,2”],“runs”: [”{“include_all”: true,“config_ids”: [1]},{“include_all”: true,“config_ids”: [2]}"]}]}";

I want to add this using java but i dont understand how do i do this.

i tried to understand it by your given example but this is not got to me.
Here in your example what is t ?

JSONArray Results =t.createJsonArrayFromList();
client.sendPost(“add_results/1”,Results) ;

where Results is a json array as follows:
[{“status_id”:“PASS”,“test_id”:“1”},{“status_id”:“PASS”,“test_id”:“2”}]


#17

Hello Chetan,

Thanks for your posting. I would recommend passing a structured object/array to sendPost as documented here:

http://docs.gurock.com/testrail-api2/bindings-java

I haven’t used createJsonArrayFromList before so I’m not sure how the resulting objects look like and I recommend formatting the sendPost parameter manually as described in our documentation.

I hope this helps!

Cheers,
Tobias


#18

hi @tgurock

I followed the guideline but still getting error
TestRail API returned HTTP 400(“Field :case_id is not a valid test case.”)

Map data = new HashMap();
data.put(“status_id”, new Integer(1));
data.put(“comment”, “This test worked fine from!”);
JSONObject r = (JSONObject) client.sendPost(“add_result_for_case/589438/433431159”, data);