Code Sample - Java
This article will show simplified code samples to invoke AIO Rest APIs in Java to report automated test results in the AIO Tests execution.
The AIO Tests REST APIs use a mix of GET, POST and Multipart-form data requests to get details on execution cycles and create them, report results against individual cases or import results into AIO Tests with the results file for all cases.
In this documentation, you’ll understand:
Setup
For the purpose of this sample, we are going to use
REST Assured - to make API calls
Gson - for JSON response parsing. It can be replaced with any JSON parsing library like Jackson or JSON-simple. (Dependencies)
Sample
Config
We will be using the following RestAssured config to define the common setup.
The Authorization header as shown in the AIO Tests API Token.
setupRestAssured() {
RequestSpecBuilder builder = new RequestSpecBuilder();
builder.setBaseUri("https://tcms.aiojiraapps.com");
builder.setBasePath("/aio-tcms/api/v1");
builder.addHeader("Authorization", "AioAuth <Your API Token>");
builder.log(LogDetail.METHOD).log(LogDetail.URI);
defaultRequestSpec = builder.build();
}
1. Get Cycle Details [Swagger]
Get Cycle Details and other Cycle information APIs can be used to get details of existing cycles, run summary and cycle folder structures.
public class AIORestAssuredExample {
public static final String GET_CYCLE_DETAILS = "/project/{projectKey}/testcycle/{cycleKey}/detail";
private static RequestSpecification defaultRequestSpec;
public static void main(String[] args) {
//Setup Rest assured config
RequestSpecBuilder builder = new RequestSpecBuilder();
builder.setBaseUri("https://tcms.aiojiraapps.com").setBasePath("/aio-tcms/api/v1");
builder.addHeader("Authorization", "AioAuth <Your API Token>");
builder.log(LogDetail.METHOD).log(LogDetail.URI);
defaultRequestSpec = builder.build();
//Get cycle details
Response cycleDetails = doGet(GET_CYCLE_DETAILS, "ATSMPL", "ATSMPL-CY-37");
System.out.println(cycleDetails.jsonPath().get("ID").toString());
}
public static Response doGet(String path, String ...pathParams) {
Response response = given(defaultRequestSpec).when().get(path,pathParams).andReturn();
return response;
}
}
2. Create Cycle [Swagger]
Depending on your organization’s process/needs, automation execution results can be captured in AIO Tests in two ways.
By creating a new cycle for each execution [e.g. Regression Release 1_Day 1, Regression Release 1_Day 2 are individual cycles and only one execution is captured as part of each of these cycles]
OrBy creating a new run in an existing cycle for each execution [e.g. Regression Release 1 is a cycle and daily executions are captured as multiple runs in the same cycle]
Below code sample (code is in #3) shows both creating a cycle, adding cases to it and reporting results.
3. Mark Execution Results [Swagger]
The below sample creates a new cycle and reports the execution results and duration of the run of your test cases.
The sample methods can be added to your test framework’s listeners or hooks which execute either after a case finishes or after execution finishes. [e.g. IInvokedMethodListener
in TestNG / TestCaseFinished
event in Cucumber / RunListener/TestExecutionListener
in JUnit]
These case runs show up with Execution Mode as Automated in AIO Tests.
public class AIORestAssuredExample {
public static final String CREATE_CYCLE = "/project/{projectKey}/testcycle/detail";
public static final String MARK_CASE = "project/{projectKey}/testcycle/{cycleKey}/testcase/{caseKey}/testrun?createNewRun={createNewRun}";
private static RequestSpecification defaultRequestSpec;
public static void main(String[] args) {
setupAIORestAssuredConfig();
//Create a new cycle
String newCycleKey = createCycle(projectKey);
//Mark test execution status of cases - status/duration can come from Result object of test frameworks
markCaseStatus(projectKey, newCycleKey, Arrays.asList("ATSMPL-TC-1","ATSMPL-TC-2"), "Passed");
}
public static String createCycle(String projectKey) {
Map<String, Object> newCycleDetails = new HashMap<>();
newCycleDetails.put("title", "Regression Release 1");
newCycleDetails.put("objective", "Trial Run");
//If cycle needs to be created in specific folder, add folder information
newCycleDetails.put("folder", Collections.singletonMap("ID","213"));
Response response = doPost(CREATE_CYCLE, newCycleDetails, projectKey);
String newCycleKey = response.jsonPath().getString("key");
return newCycleKey;
}
public static void markCaseStatus(String projectKey, String cycleKey, List<String> caseKeys, String testRunStatus) {
Map<String, Object> runStatus = new HashMap<>();
runStatus.put("testRunStatus", testRunStatus);
runStatus.put("effort", "60000");
for(String caseKey : caseKeys) {
//Passing run status, path params and createNewRun as true
doPost(MARK_CASE, runStatus, projectKey, cycleKey, caseKey, "true" );
}
}
public static Response doPost(String path, Map<String, Object> params, Object ...pathParams) {
Response response = given(defaultRequestSpec).contentType(ContentType.JSON).body(params).when().post(path,pathParams).andReturn();
return response;
}
public void setupAIORestAssuredConfig() {
RequestSpecBuilder builder = new RequestSpecBuilder();
builder.setBaseUri("https://tcms.aiojiraapps.com").setBasePath("/aio-tcms/api/v1");
builder.addHeader("Authorization", "AioAuth <Your API Token>");
builder.log(LogDetail.METHOD).log(LogDetail.URI);
defaultRequestSpec = builder.build();
}
}
4. Import Execution Results from JUnit/TestNG XML Files [Swagger]
As a post-execution step, AIO Tests currently supports the JUnit XML Report format and the TestNG report format.
To import results from either of these formats to AIO Tests, the Import Results API can be used in post-execution listeners/hooks.
5. Upload an Attachment to a Case as Evidence [Swagger]
Users can choose to upload an attachment to cases in the cycle as evidence. There are two attachment APIS, one to attach to the latest run of a case, which takes as input just the case ID, and the other API is to attach to an older run or a specific run. The below sample code can be used in hooks of frameworks to attach files:
Dependencies
For further queries and suggestions, please feel free to reach out to our customer support service via help@aiotests.com.