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

Troubleshooting API 400 error


#1

I`m getting 400 error message, but my test case id is defiantly right.

com.gurock.testrail.APIException: TestRail API returned HTTP 400(“Field :case_id is not a valid test case.”)

at com.gurock.testrail.APIClient.sendRequest(APIClient.java:216)
at com.gurock.testrail.APIClient.sendPost(APIClient.java:115)
at config.TestRailSetup.sendResultsToTestrail(TestRailSetup.java:39)
at foundation.LoginTestTest.toTestrail(LoginTestTest.java:98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:707)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

I have test automation framework with page object model. ( java + testng+ appium )
My Test:

package foundation;

import com.gurock.testrail.APIClient;
import com.gurock.testrail.APIException;
import com.moo.webcalls.AOWebClient;
import com.pageobjects.HamburgerMenuList;
import com.pageobjects.HeaderMenu;
import config.LogIn;
import config.LogOut;
import config.DesiredCapabilitiesSetup;
import config.TestRailSetup;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.support.PageFactory;
import org.testng.ITestResult;
import org.testng.annotations.*;
import org.testng.asserts.SoftAssert;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;

public class LoginTestTest {
public AndroidDriver driver;
public AOWebClient aoWebClient;
APIClient client;
int runID;
int caseID;
ITestResult result;

AOWebClient aoWeb;
public LoginTestTest(AndroidDriver<AndroidElement> driver, AOWebClient aoWeb){
    this.driver = driver;
    this.aoWeb = aoWeb;
    PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
public LoginTestTest() {
}
SoftAssert softAssert = new SoftAssert();
@BeforeClass
public void setUp() throws Exception {
    driver = DesiredCapabilitiesSetup.startAppiumServer();
    aoWebClient = DesiredCapabilitiesSetup.getAOWeb();
    client = TestRailSetup.myClient();
    runID = TestRailSetup.TestrailID();
    LogIn logIn = new LogIn(driver,aoWebClient);
    logIn.logIn();
}
@org.testng.annotations.Test
public void goToSettings() throws InterruptedException,IOException {
    caseID =29;
    HeaderMenu header = new HeaderMenu(driver,aoWeb);
    HamburgerMenuList ham = new HamburgerMenuList(driver);
    header.clickHamburgerButton();
    header.clickHamburgerButton();
    softAssert.assertAll();
}
@AfterMethod
public void toTestrail(ITestResult result) throws MalformedURLException, IOException, APIException {
    // System.out.println("Result: "+result);
    TestRailSetup.sendResultsToTestrail(client, result, runID, caseID);
}
@AfterClass
public void tearDown() throws Exception {
    LogOut logOut = new LogOut(driver,aoWeb);
    logOut.logOut();
}

}

TestRailSetup file:

package config;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import org.testng.ITestResult;

import com.gurock.testrail.APIClient;
import com.gurock.testrail.APIException;
public class TestRailSetup{

public static APIClient myClient(){
    APIClient client;
    client = new APIClient("https://mytestraillink.net/");    // i hide it
    client.setUser("userName");// i hide it
    client.setPassword("password");// i hide it
    return client;
}
public static int TestrailID(){
    int runID=5;
    return runID;
}
public static void sendResultsToTestrail (APIClient client, ITestResult result, int runID, int caseID) throws MalformedURLException, IOException, APIException {
    Map<String,Integer> data = new HashMap<String,Integer>();
    int Status=new Integer(result.getStatus());
    if (Status==2){
        Status=Status+3;
    }

//System.out.println("Parameters: "+result.getParameters());
// System.out.println(“method name: " + result.getMethod().getMethodName() + " status: “+Status);
data.put(“status_id”, Status);
client.sendPost(“add_result_for_case/”+runID+”/”+caseID, data);
}
}

And APIClient and APIExceptions files add as default.

My Test Run is definitely number 5 and TC is number 29:
https://filiptsovanton.testrail.net/index.php?/tests/view/29&group_by=cases:section_id&group_order=asc&group_id=5

How i can troubleshoot why it`s not able to locate TC ?


#2

I think i found what was the problem.
I was pointing to T29 instead of pointing to C2.


#3

Hi Anton,

Thanks for your posting! Yes, the case ID refers to the C### numbers and the T### numbers would be test IDs instead. Good to hear that it works now and please let us know in case anything else comes up.

Cheers,
Tobias


#4

APIClient client = new APIClient(“http://…/testrail/”); //hidden
client.setUser("…"); //hidden
client.setPassword("…"); //hidden
JSONObject c = (JSONObject) client.sendGet(“get_case/C596794”);
System.out.println(c.get(“title”));

I’m also getting the same error , case
_id is invalid , whereas my test run is

http://testrail1.ad.london.inspiredbroadcast.net/testrail/index.php?/tests/view/596794&group_by=cases:section_id&group_order=asc&group_id=156303


#5

The case_ID in the get_case call should be 596794, without the C.


#6

I’m still getting the same error ?
one more doubt do i need to give the above complete url in API client constructor or only till /testrail


#7

I think I see the issue. The URL you posted is to view the test, not the case. If you view that test in TestRail and click the View Case button you’ll be taken to the test case where you can get the case_id.


#8

Yes Donalaya , i was looking in the wrong URL. Its working now .Thanks !!


#9

Hey Jatin,

Thank you for the update. We are glad to read that this is working for you now.


#10

Hi Martylavender,

Can you provide me an example of how test rail API should get integrated with a Automation framework to fetch the data from test rail and to send the data to test rail, a fully functional code


#11

Hi Jatin,

Thank you for the reply. This really would depend on what you are wanting to integrate TestRail with and if you are using a specific language i.e., JAVA, etc. Our API documentation gives simple request examples and we also have several different language bindings available on our site.

http://docs.gurock.com/testrail-api2/start