是否可以使用空手道config.js文件中调用JavaScript函数。

发布于 2025-01-30 17:04:20 字数 1631 浏览 3 评论 0原文

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

我想在空手道中使用common_assertions.js文件-config.js文件,而不是在背景部分中使用,以便我可以在所有功能文件中重复使用 *有效pateresponse()方法。有办法吗?请帮忙

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文