- April 12, 2023
- Posted by: AFourTech
- Category: Blogs
In this blog, we will explore how to update test execution results in JIRA-Zephyr squad using REST APIs 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 execute test cycles, which allow you to group related test cases 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, 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 permission 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 APIs in Java using Rest Assured, you need to follow a few steps.
1. 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).
2. Next, you need to interact with JIRA REST API’s and authenticate your requests. For this, you need to specify the Jira Base URL and get the project ID’s using the API-SEARCH-PROJECTS endpoint.
- JiraBaseURL = https://<Your-UserName>.atlassian.net
- API-SEARCH-PROJECTS = https://<Your-UserName>.atlassian.net/rest/api/latest/project
3. 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.
- API-SEARCH-VERSIONS = https://<Your-UserName>.atlassian.net/rest/api/latest/project/<projecctID>/version
- 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 APIs mentioned above.
- After you have all the necessary values, you can continue 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. Here is 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 adding test cases to the test cycle, you need to get the execution ID for each test case. The execution ID is needed to update the execution status of the test case. Here is 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 = 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 is how you can do it in Java using Rest Assured:
- zephyrBaseUrl = https://prod-play.zephyr4jiracloud.com/connect
- updateExecutionStatusEndpoint=/public/rest/api/1.0/executions/
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 = new URI(updateExecutionUri);
int expirationInSec = 360;
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 = 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";
}
Conclusion:
Automating the test cycle execution in Jira-Zephyr using Rest APIs 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 evaluated and of high quality.
Rest Assured is an excellent tool for sending RESTful requests, automating API testing and integrating it with Jira-Zephyr can further enhance your testing efforts.
Lastly, automating test cycle execution in Jira-Zephyr using Java and Rest Assured provides an efficient and effective solution for AFourtech to organize and run test cases. AFourtech’s Test Automation Services can help save time, minimize errors, and boost productivity by using these tools. Furthermore, using a test automation framework gives a uniform and standardized approach to testing, allowing Afourtech to manage and maintain test cases effortlessly over time. As technology continues to evolve, investing in test automation is essential for AFourtech to stay competitive in today’s fast-paced development environment.
For more information:
Create Test cycle is not working for me. It says “you hit a dead URL”. can you pls check if the path is updated