Search code examples
javaintellij-ideacucumberjira-rest-apijira-xray

Update Cucumber features and test execution results on Xray in Jira


I'm trying to setup an automation framework with Cucumber and Java. I'm using Itellij IDEA as my IDE. I am able to automate the test cases as of now. I read the Xray documentation and realised that I should use the REST API to update the feature files on Xray.

I have worked a bit with Cucumber and C# where I automated my test cases on Visual Studio and updated the features and execution status on TFS using Hooks. As of now, I wish to implement the same with Java over Xray.

I have done a bit of research and realised that either I should create a test case on Xray written in Cucumber and then export it to automate on Intellij or automate the test cases and use Jenkins to update them on Xray.

Is there a work around or a tutorial which I can follow to update my features on Xray using Java and Cucumber on Intellij?


Solution

  • For those who're stuck with the same problem as mine, this is the way that I am able to export my cucumber features to Xray. You just have to send the path of your Cucumber feature file and the url like this - https://jira.yourdomain.com/rest/raven/1.0/import/feature The other parameters are quite understandable.

    public static void importFeatureFilesToJira(String filePath, String resultTypeUrlValue){
        String jiraUrl = config.getJiraLoginValue();
        log.info(String.format("Starting upload of Cucumber features to XRAY on Jira project: %s\n Using Jira user: %s ", config.getJiraProjectValue(), config.getJiraLoginValue()));
        log.info(String.format("Path to Report: %s", filePath));
        String authentication = config.getJiraLoginValue() + ':' + config.getJiraPassword();
        BASE64Encoder encoder = new BASE64Encoder();
        String encoded = null;
        try {
            encoded = encoder.encode((authentication).getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Client client = ClientBuilder.newBuilder()
                .register(MultiPartFeature.class).property(HttpHeaders.AUTHORIZATION, encoded).build();
    
        //Import type is dynamic below
        StringBuffer url = new StringBuffer(resultTypeUrlValue);
        url.append("?projectKey=").append(config.getJiraProjectValue());
        WebTarget webTarget = client.target(url.toString());
        log.info(String.format("URL of the XRAY API: %s", url.toString()));
        MultiPart multiPart = new MultiPart();
        multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
        FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
                new File(filePath),
                MediaType.APPLICATION_OCTET_STREAM_TYPE);
        multiPart.bodyPart(fileDataBodyPart);
    
        Response response = webTarget.request(
                MediaType.MULTIPART_FORM_DATA)
                .accept(MediaType.APPLICATION_JSON).
                        header(HttpHeaders.AUTHORIZATION, "Basic " + encoded).post(
                        Entity.entity(multiPart, multiPart.getMediaType()));
        log.info(response.getStatus() + " "
                + response.getStatusInfo() + " " + response);
        int responseBody = response.getStatus();
        String responseBodySting = response.toString();
        try{
            if(responseBody==200 && responseBodySting.contains("200")){
                log.info("Cucumber features were uploaded successfully");
            }
        }catch (Exception e){
            log.info("There was an error uploading the Cucumber feature file");
        }
        log.info("End of XRAY file upload publication");
    }