簡體   English   中英

無法在空手道功能文件中的環境之間切換

[英]Unable to switch between environment in karate feature file

根據 KARATE 文檔,我們應該使用 System.setProperty() 在測試用例級別設置 env,但是該選項在 karate 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

空手道-Config.js 文件-

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

請注意,您不能在功能中調用java.lang.System.setProperty()並期望它能夠正常工作。 在評估karate-config.js時,環境被鎖定,甚至在Feature執行之前。

請閱讀文檔: https ://github.com/karatelabs/karate#switching-the-environment

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM