【问题标题】:Using karate-config parameters in a feature file在功能文件中使用 karate-config 参数
【发布时间】:2017-11-23 00:47:43
【问题描述】:

空手道header examples 不显示如何访问除baseUrl 之外的配置值。当我切换环境(在运行命令中传入-Dkarate.env=qual)时,baseUrl 设置正确。

问题是,我想使用其他配置值as shown here,但是当我运行测试时,它无法正确访问config.ApiKey。相反,我得到了这个错误

html report:
file:/C:/bitbucket/karate-checkdigit-api/target/surefire-reports/TEST-features.checkdigitapi.VA.html
Tests run: 250, Failures: 0, Errors: 50, Skipped: 175, Time elapsed: 4.112 sec <<< FAILURE!
* def secretKey = config.apiKey(| XYZ | 2110974841 | 204 | Valid |)  Time elapsed: 0.005 sec  <<< ERROR!
java.lang.RuntimeException: no variable found with name: config
at com.intuit.karate.Script.getValuebyName(Script.java:323)
at com.intuit.karate.Script.evalJsonPathOnVarByName(Script.java:378)
at com.intuit.karate.Script.eval(Script.java:309)
at com.intuit.karate.Script.eval(Script.java:194)
at com.intuit.karate.Script.assign(Script.java:656)
at com.intuit.karate.Script.assign(Script.java:587)
at com.intuit.karate.StepDefs.def(StepDefs.java:265)
at ✽.* def secretKey = config.apiKey(features/checkdigitapi/XYZ.feature:6)

我的.feature 文件和karate-config.js 在下面。

XYZ.feature

@regression
Feature: Checkdigit Algorithm API

Background:
* url baseUrl
* def secretKey = config.apiKey
* configure ssl = true

Scenario Outline: Testing XYZ algorithm

* configure headers = { KeyId: secretKey, Accept: 'application/json' }
Given path 'headers'
And param url = baseUrl
And params { customerId: '<custcode>', algoId: '<algo>' }
When method get
Then status <val>

Examples:
  | algo   | custcode      | val   | comment |
  | XYZ    | 2110974841    | 204   | Valid |
  | XYZ    | 7790011614    | 204   | Valid |
  | XYZ    | 5580015174    | 204   | Valid |
  | XYZ    | 2110974840    | 400   | expected check digit 1 |
  | XYZ    | 211097484     | 400   | not 10 digits |
  | XYZ    | 211097484x    | 400   | not numeric |    

空手道配置.js

function() {    
  //set up runtime variables based on environment
  //get system property 'karate.env'
  var env = karate.env;
  if (!env) { env = 'dev'; }  // default when karate.env not set

  // base config
  var config = {
    env: env,
    baseUrl: 'https://localapi.abc123.example.com/api/v1/validate/customerid',
    apiKey: ''
  }
  //switch environment
  if (env == 'dev') {
  config.baseUrl = 'https://devapi.abc123.example.com/api/v1/validate/customerid';
  config.apiKey  = 'fake-1ba403ca8938176f3a62de6d30cfb8e';
  } 
  else if (env == 'qual') { //Pre-production environment settings
  config.baseUrl = 'https://qualapi.abc123.example.com/api/v1/validate/customerid';
  config.apiKey  = 'fake-d5de2eb8c0920537f5488f6535c139f2';
  }

  karate.log('karate.env =', karate.env);
  karate.log('config.baseUrl =', config.baseUrl);
  karate.log('config.apiKey =', config.apiKey);

  return config;
}

(此处类似问题,使用单独的headers.js:https://github.com/intuit/karate/issues/94)

【问题讨论】:

标签: karate


【解决方案1】:

请记住,karate-config.js 返回的 JSON 对象内 内的所有键都将作为变量注入,仅此而已。所以你不能参考config,但你一定可以参考apiKey。

我想如果你做这个简单的改变,事情就会开始起作用:

* def secretKey = apiKey

另外,我觉得你在场景的第一行有问题,应该是:

* configure headers = { KeyId: '#(secretKey)', Accept: 'application/json' }

【讨论】:

  • 感谢您的及时回复
  • 有效!我一直在使用 maven 和 intellij IDEA 的空手道,不用为所有东西编写胶水代码真是一种解脱。
  • @ropata 很高兴听到这个消息!传播这个词 :) 听起来你以前在愤怒中使用过黄瓜,你可能会发现这是一个有趣的阅读:hackernoon.com/yes-karate-is-not-true-bdd-698bf4a9be39
  • 能否请您将此添加到 intuit/karate github 的主自述文件中?
  • @SaidYusifli 请提出一个新问题
【解决方案2】:

仅供参考,我的最终、正确工作的 XYZ.feature 文件现在看起来像这样。 Given path 'headers' 行导致标题信息潜入 url,因此被删除。

XYZ.feature

@regression
Feature: Checkdigit Algorithm API

Background:
* url baseUrl
* def secretKey = apiKey
* configure ssl = true

Scenario Outline: Testing XYZ algorithm
* configure headers = { KeyId: '#(secretKey)', Accept: 'application/json' }
Given url baseUrl
And params { customerId: '<custcode>', algoId: '<algo>' }
When method get
Then status <val>

Examples:
[...]

【讨论】:

    猜你喜欢
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多