How Can We Help?

< All Topics

Atlassian Jira is one of the most widely used test management and bug tracking tool. It would make perfect sense to have all the visual bugs form Imagium directly reported into Jira.

Since Imagium is an API driven Visual Testing tool, it can be integrated with any Test management tool. Atlassian Jira is a no exception to this. A simple API call can be made looking at the validation response from Imagium and defects can directly logged into tools like HP ALM, Jira, XRAY/JTMF.

How to generate API token for JIRA?

package Refined;
import java.io.IOException;
import org.json.simple.JSONObject;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Jira {

	static WebDriver driver;
	static String jiraEndPoint = "https://yourProject.atlassian.net/rest/api/3/issue";
	static String authorizationToken =  "Basic YmxvYmFpLmluZm9867578666e89e444347hgyhfcgpZNDlFNA==";
	static String projectKey ="Project key";
	public static void main(String args[]) throws Exception {

		System.setProperty("webdriver.chrome.driver", "C:\\drivers\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.get("https://www.google.com/");
		driver.manage().window().setSize(new Dimension(1024, 768));
		Thread.sleep(2000);
		String scrBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
		String uid = getUID("Google Jira Test", "89ae2891-3b72-4074-ba0e-f89d78357e96");
		postRequest("Step - Home", uid, scrBase64);				
		driver.quit();
	}



	public static String getUID(String testName, String projectKey) {
		try {
			RequestSpecification request = RestAssured.given();
			request.header("content-type", "application/json");
			JSONObject json = new JSONObject();
			json.put("TestName", testName);
			json.put("ProjectKey", projectKey);
			request.body(json.toJSONString());
			Response response = request.when().post("http://192.168.10.13:80/api/GetUID");
			int code = response.getStatusCode();
			String response_id = response.getBody().asString();
			System.out.println("Response id:" + response_id);
			return response_id;
		} catch (Exception ex) {
			return ex.toString();
		}
	}

	public static void postRequest(String stepNam, String uid, String imagebase64) throws IOException {
		RequestSpecification request1 = RestAssured.given();
		request1.header("content-type", "application/json");		
		JSONObject jo = new JSONObject();
		jo.put("TestRunID", uid.replace("\"", ""));
		jo.put("StepName", stepNam);
		jo.put("ImageBase64", imagebase64);
		System.out.println("imagebase64:" + imagebase64);
		request1.body(jo.toJSONString());
		Response response1 = request1.when().post("http://192.168.10.13:80/api/Validate");
		String response_id1 = response1.getBody().asString();
		
		if(response_id1.contains("Default: Failed"))
		{
			reportBugToJira("Visual test failed - "+ stepNam, "Please check Imagium cloud for more details.");			
		}
		System.out.println("response id1:" + response_id1);
	}
	
	
	public static void reportBugToJira(String Summary, String Description) throws IOException {
		RequestSpecification request1 = RestAssured.given();
		request1.header("content-type", "application/json");
		request1.header("Authorization", authorizationToken);
		request1.header("Accept", "application/json");		
		String createPaylod = "{\"fields\":{\"summary\":\""+Summary+"\",\"issuetype\":{\"name\":\"Bug\"},\"project\":{\"key\":\""+ projectKey+"\"},\"description\":{\"type\":\"doc\",\"version\":1,\"content\":[{\"type\":\"paragraph\",\"content\":[{\"text\":\""+Description+"\",\"type\":\"text\"}]}]}}}";
		request1.body(createPaylod);
		Response response1 = request1.when().post(jiraEndPoint);
		String response_id1 = response1.getBody().asString();
		System.out.println("response id1:" + response_id1);
	}

}