1

reqres.feature

Feature: Reqres api test cases

  Background: base url
    Given url base_url
    * def validateResponse = read('classpath:helpers/common_assertions.js')

  Scenario: list single user get request
    Given path single_user_path
    When method get
    Then status 200
    * validateResponse()

common_assertion.js

function common_assertions() {
        var contentType = karate.get("responseHeaders['Content-Type'][0]");
        if (contentType !== 'application/json; charset=utf-8') {
          karate.fail('content type is not json');
        }
        var responseType = karate.get('responseType');
        if (responseType !== 'json') {
          karate.fail('response type is not json');
        }
        var responseTime = karate.get('responseTime');
        if (responseTime > 5000) {
          karate.fail('response is too slow');
        }
      }

karate-config.js

function fn() {
    var env = karate.env; // get system property 'karate.env'
    karate.log('karate.env system property was:', env);
    if (!env) {
        env = 'dev';
    }
    var config = {

        base_url: 'https://reqres.in/api',
        single_user_path: '/users/2'        
    };
    if (env == 'dev') {
        // customize
        // e.g. config.foo = 'bar';
    } else if (env == 'e2e') {
        // customize
    }
    return config;
}

I would like to use common_assertions.js file in karate-config.js file rather than using in the background section so that i can reuse * validateResponse() method across all feature files. Is there a way?Please help

cyrilgeorge153
  • 331
  • 1
  • 2
  • 9
  • sorry I pass on this question, it is too much for me to read and understand. I think I have already sent you a link that explains why I don't recommend this kind of "reuse". please have some empathy for people who need to read your tests in the future: https://stackoverflow.com/a/54126724/143475 – Peter Thomas May 20 '22 at 16:07
  • I have updated my question. Hope it is clear now. – cyrilgeorge153 May 20 '22 at 16:30
  • it is possible, please do some research. look at examples of `karate.call()` in JS: https://stackoverflow.com/a/60590359/143475 | https://stackoverflow.com/a/57312962/143475 - I have nothing more to add, maybe someone else here has better ideas – Peter Thomas May 20 '22 at 17:11
  • I was not able to find any solution in calling js file. But stored the js function in a feature file and stored the result in a variable, later reused the variable in karate-config.js like this.var result = karate.callSingle('classpath:helpers/common_assertions.feature', config); config.validateResponse = result.commonAssertions; Later reused validateResponse() in all feature files – cyrilgeorge153 May 23 '22 at 15:43
  • I strongly recommend that `callSingle()` should never return functions. read the last paragraph here please: https://github.com/karatelabs/karate#karatecallsingle – Peter Thomas May 23 '22 at 17:01
  • I did not get any issues while parallel execution with this approach. Anyway will stick to the documentation and will avoid using callSingle() for feature that return functions. – cyrilgeorge153 May 24 '22 at 07:35

0 Answers0