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

Need additional help trying to understand how to run TestRail with Selenium(Java)


#1

For some reason, I am having the hardest time trying to figure out how to configure TestRails with my selenium automation. I’ve looked at the documentations as well as some examples and still am having a hard time understanding how to use the methods to send JSON results back to Testrail. I was able to connect to the server using the API files. Below is my test code that I am having issues with. Currently I am getting the error “TestRail API returned HTTP 400(“Field :run_id is not a valid ID.”)”. The server and code are all tests so feel free to run them.

package trail;

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

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import testrail.APIClient;
import testrail.APIException;

public class run {

public static String TEST_RUN_ID                = "C1";
public static String TESTRAIL_USERNAME          = "the007scientistemail@yahoo.com";
public static String TESTRAIL_PASSWORD          = "cbs12345";
public static String RAILS_ENGINE_URL           = "https://lawcrens.testrail.io";
public static final int TEST_CASE_PASSED_STATUS = 1;
public static final int TEST_CASE_FAILED_STATUS = 5;
public static WebDriver driver;	

public static void main(String[] args) throws IOException, APIException {
	
	
	// TODO Auto-generated method stub
	System.setProperty("webdriver.chrome.driver", "/Users/lcren1026/Drivers/chromedriver");
	driver = new ChromeDriver();
	driver.navigate().to("https://www.google.com/");
	
	if(driver.findElement(By.xpath("//*[@id=\"hplogo\"]")).isDisplayed()) {
		trails.addResultForTestCase("120254", TEST_CASE_PASSED_STATUS, "");
		
	} else
	{
		trails.addResultForTestCase("120254", TEST_CASE_FAILED_STATUS, "");
	}
	

}

  public static void addResultForTestCase(String testCaseId, int status,
            String error) throws IOException, APIException {

        String testRunId = TEST_RUN_ID;
        System.out.println("Test");

        APIClient client = new APIClient(RAILS_ENGINE_URL);
        client.setUser(TESTRAIL_USERNAME);
        client.setPassword(TESTRAIL_PASSWORD);
        Map data = new HashMap();
        data.put("status_id", status);
        data.put("comment", "Test Executed - Status updated automatically from Selenium test automation.");
        client.sendPost("add_result_for_case/"+testRunId+"/"+testCaseId+"",data );
    }

}


#2

Hey there,

Thanks for the post. Taking a look at the code sample you provided the run_id is set to “C1”, which isn’t a valid run ID. For the purposes of the API you would need to include only the numerical ID, and not include the prefix.

In the TestRail UI, the ID for all entities include a single letter prefix to quickly denote what it is. When viewing a test run, in the top left corner you should see the ID like R22 indicating it is a run with ID 22. Test cases will use the prefix C, projects P, etc.

Try running the script with the numerical ID of an existing open run and let me know if you’re still having any issue.


#3

I figured it out. That was part of the reason. I was also calling another method from another class.