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

Issue with implementing add_run for automation. Java(Selenium)


#1

Hello,

I am new newbie to TestRail/Selenium integration. I just figured out how to add to test cases but now I am having a hard time trying to create new test run through automation. When I create a new test run, the test cases are there but are not ran and the test run name is Master. I want to know how I can rename the new test run to anything in the code and run the test cases. Below is the code.

package trail;

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

import org.json.simple.JSONObject;
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                = "1";
public static String TESTRAIL_USERNAME          = "xxxxxxxx";
public static String TESTRAIL_PASSWORD          = "xxxxx";
public static String RAILS_ENGINE_URL           = "xxxxxxxx";
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 {
	
    APIClient client = new APIClient(RAILS_ENGINE_URL);
    client.setUser(TESTRAIL_USERNAME);
    client.setPassword(TESTRAIL_PASSWORD);
    
	// TODO Auto-generated method stub
	System.setProperty("webdriver.chrome.driver", "/chromedriver");
	driver = new ChromeDriver();
	driver.navigate().to("https://www.google.com/");
	
	
	if(driver.findElement(By.xpath("//*[@id=\"hplogo\"]")).isDisplayed()) {

		addResultForTestCase("1", TEST_CASE_PASSED_STATUS, "");
		driver.quit();
		
	} else
	{
		addResultForTestCase("1", TEST_CASE_FAILED_STATUS, "");
		driver.quit();
	}
	

}

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

        String testRunId = TEST_RUN_ID;
		System.out.println(testRunId);
        APIClient client = new APIClient(RAILS_ENGINE_URL);
        client.setUser(TESTRAIL_USERNAME);
        client.setPassword(TESTRAIL_PASSWORD);

        JSONObject c = (JSONObject) client.sendGet("get_case/1/1");
        System.out.println(c.get("title"));
        Map data = new HashMap();
        data.put("status_id", status);
       data.put("comment", "Test Executed - Status updated automatically from Selenium test automation.");
       client.sendPost("add_run/"+testRunId+"/"+testCaseId+"",data );
       client.sendPost("add_result_for_case/"+testRunId+"/"+testCaseId+"",data );
     
    
    }


#2

First and foremost - please edit your post and remove the credentials… everyone can use them, you know ?

Second - do not use your password when accessing the api, generate an API Token (http://docs.gurock.com/testrail-api2/accessing#username_and_api_key).

Regarding the add_run: take a look at http://docs.gurock.com/testrail-api2/reference-runs#add_run - Request fields tell you how you can pass what parameters to the method. Like name in your case to set the name of the run.

In general - the api is well documented and quite helpful: http://docs.gurock.com/testrail-api2/


#3

So I figured out how to use add a run. I was having a hard time understanding the PHP lingo so it was a little trial and error.

Create an object:
JSONObject object = (JSONObject) APIclient.sendGet(“get_case/1/1”);

Create a hash that holds the test data:
Map data = new HashMap();

Add to the data:
data.put(“status_id”, 1);
data.put(“name”, “new name of run”);

Send Post using the add_run call:
client.sendPost(“add_run/”+testRunId+"/"+testCaseId+"",data );

The issue I am having now is adding the data to the new test case. Since the new test cases have new IDs they will not be added. Is there a way I could point the data to the new IDs?

Also thanks for the password API tip bygones.