NRT
NRT

Reputation: 125

How to integrate karate with testrail

I am new to Java and using karate for API automation. I need help to integrate testrail with karate. I want to use tags for each scenario which will be the test case id (from testrail) and I want to push the result 'after the scenario'.

Can someone guide me on this? Code snippets would be more appreciated. Thank you!

Upvotes: 3

Views: 1005

Answers (2)

Peter Thomas
Peter Thomas

Reputation: 58058

Please look at 'hooks' documented here: https://github.com/intuit/karate#hooks

And there is an example with code over here: https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/hooks/hooks.feature

I'm sorry I can't help you with how to push data to testrail, but it may be as simple as an HTTP request. And guess what Karate is famous for :)

Note that values of tags can be accessed within a test, here is the doc for karate.tagValues (with link to example): https://github.com/intuit/karate#the-karate-object

Note that you need to be on the 0.7.0 version, right now 0.7.0.RC8 is available.

Edit - also see: https://stackoverflow.com/a/54527955/143475

Upvotes: 0

Mert Ekinci
Mert Ekinci

Reputation: 358

I spent a lot of effort for this. That's how I implement. Maybe you can follow it.

First of all, you should download the APIClient.java and APIException.java files from the link 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 tags, test results and error messages to the BaseTest.java file, which I will talk about shortly.

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;
}

Please dont forget give tag to scenario in Feature 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());
    }

}

Upvotes: 4

Related Questions