首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在要素文件中使用空手道配置参数

在要素文件中使用空手道配置参数
EN

Stack Overflow用户
提问于 2017-11-23 08:47:43
回答 2查看 19.6K关注 0票数 12

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

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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
@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 |    

karate-config.js

代码语言:javascript
运行
复制
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.jshttps://github.com/intuit/karate/issues/94)

EN

Stack Overflow用户

回答已采纳

发布于 2017-11-23 10:28:44

请记住,karate-config.js返回的对象中的所有键都将作为变量注入,而不是其他。因此,您将无法引用config,但您肯定能够引用apiKey

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

代码语言:javascript
运行
复制
* def secretKey = apiKey

另外,我认为你在场景的第一行有一个问题,它应该是:

代码语言:javascript
运行
复制
* configure headers = { KeyId: '#(secretKey)', Accept: 'application/json' }
票数 11
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47445984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档