Java – How to integrate karate with testrail

How to integrate karate with testrail… here is a solution to the problem.

How to integrate karate with testrail

I’m new to Java and am using Karate for API automation. I need help combining testrail with Karate. I want to use a label for each scenario, which will be the test case ID (from testrail) and I want to push the result “after the scene”.

Can someone guide me? Code snippets will be more popular. Thank you!

Solution

I put a lot of effort into it.
This is how I achieved it. Maybe you can follow it.

First, you should download the APIClient.java and APIException.java files from the links below.

TestrailApi in github

Then you need to add these files to the following path in your project.
For example: YourProjectFolder/src/main/java/testrails/

In your karate-config.js file, after each test, you can send your case labels, test results, and error messages to the BaseTest.java file, which I’ll discuss later.

karate-config.js file


function fn() {

var config = {
    baseUrl: 'http://111.111.1.111:11111',
  };
        
karate.configure('afterScenario', () => {
    try{
         const BaseTestClass = Java.type('features. BaseTest');
         BaseTestClass.sendScenarioResults(karate.scenario.failed, 
         karate.scenario.tags, karate.info.errorMessage);
        }catch(error) {
       console.log(error)
       }
    });
      
return config;
}

Don’t forget to tag scenes in the capabilities file.
For example @1111


Feature: ExampleFeature

Background:
    * def conf = call read('.. /karate-config.js')
    * url conf.baseUrl

@1111
  Scenario: Example

Next, create a runner file named BaseTests.java

BaseTest.java file


package features;

import com.intuit.karate.junit5.Karate;
import net.minidev.json.JSONObject;
import org.junit.jupiter.api.BeforeAll;
import testrails. APIClient;
import testrails. APIException;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class BaseTest {

private static APIClient client = null;
    private static String runID = null;

@BeforeAll
    public static void beforeClass() throws Exception {
        String fileName = System.getProperty("karate.options");
        Login to API
        client = new APIClient("Write Your host, for example 
        https://yourcompanyname.testrail.io/");
        client.setUser("[email protected]");
        client.setPassword("password");

Create Test Run
        Map data = new HashMap();
        data.put("suite_id", "Write Your Project SuitId(Only number)");
        data.put("name", "Api Test Run");
        data.put("description", "Karate Architect Regression Running");

JSONObject c = (JSONObject) client.sendPost("add_run/" + 
        TESTRAİL_PROJECT_ID, data);
        runID = c.getAsString("id");
    }
       Send Scenario Result to Testrail
    public static void sendScenarioResults(boolean failed, List<String> tags, String errorMessage) {
        try {
            Map data = new HashMap();
            data.put("status_id", failed ? 5 : 1);
            data.put("comment", errorMessage);
            client.sendPost("add_result_for_case/" + runID + "/" + tags.get(0), 
            data);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (APIException e) {
            e.printStackTrace();
        }
    }

@Karate.Test
    Karate ExampleFeatureRun() {
        return Karate.run("ExampleFeatureRun").relativeTo(getClass());
    }

}

Related Problems and Solutions