无法在空手道功能文件中的环境之间切换

发布于 2025-01-30 01:04:53 字数 1849 浏览 1 评论 0原文

根据空手道文档的方式,我们应该使用system.setProperty()在测试案例级别设置env,但是该选项在空手道1.2.0中不起作用(在先前版本中未验证)

功能


Feature: Testing env changing
  Scenario: Reading base url of different env
  * print 'Before changing env...---> '+ karate.env
  * print 'Testing karate...'
  * print baseUrl
  * java.lang.System.setProperty("karate.env","STAGE")
  * def newevn = java.lang.System.getProperty('karate.env')
  * print 'After changing env...---> '+ newevn
  * def fun = call read('file:karate-config.js') 
  * print 'Changed env...---> '+ fun.baseUrl

karate-config.js file-

function fn() {
  karate.configure('connectTimeout', 5000);
  karate.configure('readTimeout', 5000);
  var env = karate.env;
  karate.log('karate.env system property was:', env);

  var baseUrl = 'testingbase.com';
  karate.log('Env---->' + env)
  var port = karate.properties['demo.server.port'] || '8080';
  var protocol = 'http';

  var config = {
    env: env,
    baseUrl: 'https://localapi.abc123.example.com/api/v1/validate/customerid',
    apiKey: ''
  }
  if (karate.properties['demo.server.https'] === 'true') {
    protocol = 'https';
    karate.configure('ssl', true);
  }
  var config = { demoBaseUrl: protocol + '://127.0.0.1:' + port };
  if (env== 'mock') {
    // karate.configure('callSingleCache', { minutes: 1 });
    // 'callSingle' is guaranteed to run only once even across all threads
    //var result = karate.callSingle('classpath:demo/headers/common-noheaders.feature', config);
    // and it sets a variable called 'authInfo' used in headers-single.feature
   // config.authInfo = { authTime: result.time, authToken: result.token };
  }
  else if(env=='BETA'){
 config.baseUrl='tetsing.beta.base.com';

  }
  else if(env=='STAGE'){
config.baseUrl='tetsing.stage.base.com';

  }
  return config;
}

As per KARATE documentatoion we should use System.setProperty() to set env in test case level, But that option is not working in karate 1.2.0 (Didn't verify in previous versions)

Feature


Feature: Testing env changing
  Scenario: Reading base url of different env
  * print 'Before changing env...---> '+ karate.env
  * print 'Testing karate...'
  * print baseUrl
  * java.lang.System.setProperty("karate.env","STAGE")
  * def newevn = java.lang.System.getProperty('karate.env')
  * print 'After changing env...---> '+ newevn
  * def fun = call read('file:karate-config.js') 
  * print 'Changed env...---> '+ fun.baseUrl

Karate-Config.js file-

function fn() {
  karate.configure('connectTimeout', 5000);
  karate.configure('readTimeout', 5000);
  var env = karate.env;
  karate.log('karate.env system property was:', env);

  var baseUrl = 'testingbase.com';
  karate.log('Env---->' + env)
  var port = karate.properties['demo.server.port'] || '8080';
  var protocol = 'http';

  var config = {
    env: env,
    baseUrl: 'https://localapi.abc123.example.com/api/v1/validate/customerid',
    apiKey: ''
  }
  if (karate.properties['demo.server.https'] === 'true') {
    protocol = 'https';
    karate.configure('ssl', true);
  }
  var config = { demoBaseUrl: protocol + '://127.0.0.1:' + port };
  if (env== 'mock') {
    // karate.configure('callSingleCache', { minutes: 1 });
    // 'callSingle' is guaranteed to run only once even across all threads
    //var result = karate.callSingle('classpath:demo/headers/common-noheaders.feature', config);
    // and it sets a variable called 'authInfo' used in headers-single.feature
   // config.authInfo = { authTime: result.time, authToken: result.token };
  }
  else if(env=='BETA'){
 config.baseUrl='tetsing.beta.base.com';

  }
  else if(env=='STAGE'){
config.baseUrl='tetsing.stage.base.com';

  }
  return config;
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪恋 2025-02-06 01:04:53

请注意,您无法在功能中调用java.lang.system.setproperty(),并期望它能起作用。在评估空手道config.js时,环境已锁定,即feature甚至执行之前。

Please read the docs: https://github.com/karatelabs/karate#switching-the - 环境

Please note that you can't call java.lang.System.setProperty() within a feature and expect that to work. The environment is locked in at the time that karate-config.js is evaluated, which is before a Feature even executes.

Please read the docs: https://github.com/karatelabs/karate#switching-the-environment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文