How to update Cloud JIRA X-RAY test execution automatically using REST API Java?

Yosuva ArulanthuAppium, Selenium, Test Automation, TestingLeave a Comment

Updating JIRA Xray test status and evidence using any programming is simple as we have RestAPI. Below are the two examples to update the status and evidence.

This example is can be used if your JIRA hosted in the cloud(https://comany.atlassian.net) environment. If you are using the on-premises hosted JIRA , please see check

Example:1 Sample JSON request body to Update the Test status
{
    "testExecutionKey" : "DEMO-1206",
    "tests" : [
        {
            "testKey" : "ABC-129",
            "start" : "2014-08-30T11:47:35+01:00",
            "finish" : "2014-08-30T11:50:56+01:00",
            "comment" : "Successful execution",
            "status" : "PASS"
        }
    ]
}
Example:2 Sample JSON request body to Update the Test status and Evidence
{
    "testExecutionKey" : "DEMO-1206",
    "tests": [
        {
            "start" : "2021-08-30T11:47:35+01:00",
            "finish" : "2021-08-30T11:50:56+01:00",
            "comment" : "Successful execution",
            "status" : "PASS",
            "evidence" : [
                {
                    "data": "iVBORw0KGgoAAAANSUhEUgAABkIAAAO9CAYAAADezXv6AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEn(...base64 file enconding)",
                    "filename": "image21.jpg",
                    "contentType": "image/jpeg"
                }
            ]
}

Complete Example using Java

JAR Files used:

  1. gson-2.6.2.jar
  2. commons.io-2.6.jar
  1. gson-2.6.2.jar
  2. commons.io-2.6.jar
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.Base64;

import javax.net.ssl.HttpsURLConnection;

import org.apache.commons.io.FileUtils;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class JIRAUpdate {
	
	public static void main(String args[]) throws IOException
	{
		String evidencePath="<screenshotpath>,<html/pdf path>";
		String evidences[]=evidencePath.split(",");
		JsonArray evidencesJson=new JsonArray();
		
		for (int i=0;i<evidences.length;i++)
		{
			File evidenceFile=new File(evidences[i]);
			byte[] filecontent=FileUtils.readFileToByteArray(evidenceFile);
			String encoded_FileContent=Base64.getEncoder().encodeToString(filecontent);
			JsonObject evidence=new JsonObject();
			evidence.addProperty("data", encoded_FileContent);
			evidence.addProperty("filename", evidenceFile.getName());
			evidence.addProperty("contentType", "application/html");
			evidencesJson.add(evidence);
		}
		
		JsonObject json=new JsonObject();
		json.addProperty("testExecutionKey", "TEST-123");
		
		JsonArray testArray=new JsonArray();
		JsonObject test=new JsonObject();
		test.addProperty("testKey", "TEST123");
		test.addProperty("start", "2020-04-30T11:47:35+01:00");
		test.addProperty("finish", "2020-04-30T11:47:35+01:00");
		test.addProperty("comment", "Executed through automation.Evidences attached");
		test.addProperty("status", "PASS");
		test.add("evidences", evidencesJson);
		testArray.add(test);
		json.add("test", testArray);
		
		HttpsURLConnection con=null;
		InputStreamReader inputStream=null;
		try {
			URL jira_API_URL=new URL("https://jira.example.com/rest/raven/1.0/import/execution");
			String encodeCredentials=Base64.getEncoder().encodeToString("UserName:Password".getBytes("UTF-8"));
			con=(HttpsURLConnection)jira_API_URL.openConnection();
			con.setRequestMethod("POST");
			con.setDoOutput(true);
			con.setRequestProperty("Autherization", "Basic "+encodeCredentials);
			con.setRequestProperty("Content-Type", "application/json");
			con.setRequestProperty("X-Atlassian-Token", "nocheck");
			try(OutputStream os=con.getOutputStream()){
				byte[] input=json.toString().getBytes("UTF-8");
				os.write(input,0,input.length);
			}
			inputStream=new InputStreamReader(con.getInputStream(),"UTF-8");
			try(BufferedReader br=new BufferedReader(inputStream))
			{
				StringBuilder response=new StringBuilder();
				String responseline=null;
				while((responseline=br.readLine())!=null) {
					response.append(responseline.trim());
				}
			}
			if(con.getResponseCode()==200)
			{
				//jira Test execution updated successfully for the specified test
			}
			else
			{
				//some other connection issue
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if(inputStream!=null) {
				try {
					inputStream.close();
				}
				catch (IOException e) {
					// TODO: handle exception
				}
			}
			if(con!=null) {
				con.disconnect();
			}
		}
	}

}


Reference Links:

https://docs.getxray.app/display/XRAYCLOUD/Import+Execution+Results+-+REST+v2

Leave a Reply

Your email address will not be published.