“Automating Test Cycle Execution in Jira-Zephyr using Java and Rest Assured”
In this blog, we will explore how to update test execution results in JIRA-Zephyr squad using REST API’s in Java using Rest Assured.
Jira is a popular tool for managing software projects, and Zephyr is a plugin for Jira that helps teams manage their testing efforts. One of the key features of Zephyr is the ability to create and manage test cycles, which allow you to group related test cases together and track their progress. In this blog, we’ll show you how to use the Jira REST API and Rest Assured to create a test cycle, add test cases to that cycle, get the execution ID, and update the execution status.
Before we get started, you’ll need to have some prerequisites in place:
- A Jira account with permissions to create test cycles and execute test cases.
- A Zephyr for Jira Cloud
- Basic knowledge of Java and REST APIs.
Flow diagram to update the test execution result in Jira-Zephyr.
Before you can update test execution results in Jira-Zephyr using Rest API’s in Java using Rest Assured, there are several steps you need to follow.
First, you must create test cases in Jira and add test steps using Zephyr. Then, you need to create versions/releases for that project. Once this is done, you must generate an Atlassian API Token( refer :https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/) and obtain the Zephyr Secret Key and Access Key(refer :https://support.smartbear.com/zephyr-squad-cloud/docs/api/api-keys.html).
Next, you need to interact with JIRA REST API’s and authenticate your requests. To do this, you need to specify the Jira Base URL and get the project ID’s using the API-SEARCH-PROJECTS endpoint.
1.JiraBaseURL = https://<Your-UserName>.atlassian.net
2.API-SEARCH-PROJECTS = https://<Your-UserName>.atlassian.net/rest/api/latest/project
Once you have the project ID, you can obtain the version ID’s using the API-SEARCH-VERSIONS endpoint. Additionally, you need to obtain the Issue Key by hitting the API_SEARCH_ISSUES endpoint.
3.API-SEARCH-VERSIONS = https://<Your-UserName>.atlassian.net/rest/api/latest/project/<projecctID>/version
4.API_SEARCH_ISSUES = https://prod-play.zephyr4jiracloud.com/connect/rest/api/2/search
Note: To retrieve the expected values, you can use Postman to hit the four API’s mentioned above.
After you have all the necessary values, you can proceed with the next steps. It is important to note that you must generate a JWT token for every Zephyr endpoint you are hitting. For more information on generating JWT tokens, refer to https://support.smartbear.com/zephyr-squad-cloud/docs/api/jwt-token.html.
Step 1: Create a Test Cycle in Jira-Zephyr
To create a test cycle in Jira-Zephyr, you need to send a POST request to the Jira REST API. Here’s how you can do it in Java using Rest Assured:
zephyrBaseUrl = https://prod-play.zephyr4jiracloud.com/connect
createCycleEndpoint = /public/rest/api/1.0/cycle?expand=&clonedCycleId=
String cycleUri = zephyrBaseUrl + createCycleEndpoint;
Response createCycleResponse = given()
.header("zapiAccessKey", accessKey)
.header("zapiSecretKey", secretKey)
.header("Authorization", WrapperFunctions.getJWT(cycleUri, client))
.header("Content-Type", "application/json")
.body(CommonFunctions.getCycleCreationJsonPayload(cycleName,cycleDescription,startDate,projectId,versionId))
.post(cycleUri)
.then()
.statusCode(200)
.extract()
.response();
String cycleId = createCycleResponse.jsonPath().getString("id");
Step 2: Add Test Cases to the Test Cycle
Once you have created the test cycle, you can add test cases to it. Here’s how you can do it in Java using Rest Assured:
zephyrBaseUrl = https://prod-play.zephyr4jiracloud.com/connect
addTestCaseEndpoint = /public/rest/api/1.0/executions/add/cycle/
String addTestsUri = zephyrBaseUrl + addTestCaseEndpoint + cycleId;
Response addTestsResponse = given()
.header("zapiAccessKey", accessKey)
.header("zapiSecretKey", secretKey)
.header("Authorization", WrapperFunctions.getJWT(addTestsUri,client))
.header("Content-Type", "application/json")
.body("{"
+ "\"issues\": [\"" + String.join("\",\"", issueIds) + "\"],"
+ "\"method\": \"1\","
+ "\"projectId\": " + projectId + ","
+ "\"versionId\": " + versionId
+ "}")
.post(addTestsUri)
.then()
.statusCode(200)
.extract()
.response();
Step 3: Get the Execution ID
After you have added test cases to the test cycle, you need to get the execution ID for each test case. The execution ID is required to update the execution status of the test case. Here’s how you can get the execution ID in Java using Rest Assured:
zephyrBaseUrl = https://prod-play.zephyr4jiracloud.com/connect
getExecutionIDEndPoint =/public/rest/api/1.0/executions/search/cycle/
Response response = RestAssured.given()
.header("Authorization", jwt).header("zapiAccessKey", accessKey)
.get(zephyrBaseUrl+getExecutionIDEndPoint);
int statusCode = response.getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
JSONObject allIssues = new JSONObject(response.getBody().asString());
JSONArray issuesArray = allIssues.getJSONArray("searchObjectList");
if (issuesArray.length() == 0) {
return executionIds;
}
for (int j = 0; j <= issuesArray.length() - 1; j++) {
JSONObject jobj = issuesArray.getJSONObject(j);
JSONObject jobj2 = jobj.getJSONObject("execution");
String executionId = jobj2.getString("id");
long issueId = jobj2.getLong("issueId");
executionIds.put(executionId, String.valueOf(issueId));
}
}
return executionIds;
Step 4: Update Execution Status
Once you have the execution ID for a test case, you can update its execution status. Here’s how you can do it in Java using Rest Assured:
zephyrBaseUrl = https://prod-play.zephyr4jiracloud.com/connect
updateExecutionStatusEndpoint = /public/rest/api/1.0/execution/
JSONObject statusObj = new JSONObject();
statusObj.put("id", status);
JSONObject executeTestsObj = new JSONObject();
executeTestsObj.put("status", statusObj);
executeTestsObj.put("cycleId", cycleId);
executeTestsObj.put("projectId", projectId);
executeTestsObj.put("versionId", versionId);
executeTestsObj.put("issueId", executionId); // Use the provided execution ID
executeTestsObj.put("comment", "Executed by ZAPI Cloud");
String updateExecutionUri = zephyrBaseUrl + updateExecutionStatusEndpoint + executionId; // Use the provided execution ID
System.out.println(updateExecutionUri);
URI uri = new URI(updateExecutionUri);
int expirationInSec = 360;
JwtGenerator jwtGenerator = client.getJwtGenerator();
String jwt = jwtGenerator.generateJWT("PUT", uri, expirationInSec);
System.out.println(jwt);
RequestSpecification request = RestAssured.given();
request.contentType(ContentType.JSON);
request.header("zapiAccessKey", accessKey);
request.header("Authorization", jwt);
request.body(executeTestsObj.toString());
Response response = request.put(updateExecutionUri);
int statusCode = response.getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
JSONObject executionResponseObj = new JSONObject(response.getBody().asString());
JSONObject descriptionResponseObj = executionResponseObj.getJSONObject("execution");
JSONObject statusResponseObj = descriptionResponseObj.getJSONObject("status");
String executionStatus = statusResponseObj.getString("description");
System.out.println(executionResponseObj.get("issueKey") + "--" + executionStatus);
return executionStatus;
} else {
JSONObject executionResponseObj = new JSONObject(response.getBody().asString());
String cycleId = executionResponseObj.getString("clientMessage");
System.err.println("Unexpected response status: " + statusCode);
return "Failed to update test execution status";
}
In conclusion, automating the test cycle execution in Jira-Zephyr using Rest API’s in Java using Rest Assured is a powerful way to streamline your testing process and ensure that all your test cases are executed efficiently.
By following the steps outlined in this blog, you can create a test cycle, add test cases to it, get the execution ID for each test case, and update the execution status. This can save you time and effort in managing your testing process, and help you ensure that your software is thoroughly tested and of high quality.
Rest Assured is an excellent tool for sending RESTful requests and automating API testing, and integrating it with Jira-Zephyr can enhance your testing efforts even further.
For more information: