一尘不染

将Json传递给karate-config.js文件

json

我必须在6个以上的环境中运行相同的其余api脚本集。因此,我已将所有测试数据和端点/资源路径存储在json文件中。然后,我尝试将此json文件读入我的karate-
config.js文件,这是因为我要获取与从命令行(karate.env)传递的环境相对应的数据,该环境正在读入我的空手道-config.js文件

以下是我的json文件示例

[
  {
    "qa":{
      "username_cm_on":"test_cm_on_qa",
      "password_cm_on":"Test123$",
      "nonadmin_username_cm_on":"test_non_admin_cm_on_qa",
      "nonadmin_password_cm_on":"Test123$",
      "username_cm_off":"test_cm_off_qa",
      "password_cm_off":"Test123$",
      "nonadmin_username_cm_off":"test_non_admin_cm_off_qa",
      "nonadmin_password_cm_off":"Test123$",
      "zuul_urls":{
        "home-sec-uri":"https://qa.abc.com/qa/home-sec-uri",
        "home-res-uri":"https://qa.abc.com/qa/home-res-uri"
      }
    }
  },
  {
    "uat":{
      "username_cm_on":"test_cm_on_uat",
      "password_cm_on":"Test123$",
      "nonadmin_username_cm_on":"test_non_admin_cm_on_uat",
      "nonadmin_password_cm_on":"Test123$",
      "username_cm_off":"test_cm_off_uat",
      "password_cm_off":"Test123$",
      "nonadmin_username_cm_off":"test_non_admin_cm_off_uat",
      "nonadmin_password_cm_off":"Test123$",
      "zuul_urls":{
        "home-sec-uri":"https://uat.abc.com/qa/home-sec-uri",
        "home-res-uri":"https://uat.abc.com/qa/home-res-uri"
      }
    }
  }
]

下面是我的karate-config.js文件

function() {
  // var env = karate.env; // get system property 'karate.env'
  var env = 'qa';
  var cm = 'ON';
  var envData =  call read('classpath:env_data.json'); //require("./env_data.json");
  // write logic to read data from the json file _ Done, need testing

  karate.log('karate.env system property was:', env);

  switch(env) {
    case "qa":
        if(cm === 'ON'){
            config.adminusername_cm_on = getData().username_cm_on;
            config.adminpassword_cm_on = "";
            config.nonadminusername_cm_on = getData().nonadmin_username_cm_on;
            config.nonadminpassword_cm_on = "";
        }else if(cm === "OFF") {
            config.adminusername_cm_off = getData().username_cm_off;
            config.adminpassword_cm_off = "";
            config.nonadminusername_cm_off = getData().nonadmin_username_cm_off;
            config.nonadminpassword_cm_off = "";
        }
        break;
    case "uat":
        break;
    default:
        break;
  }

  // This method will return the data from the env_data.json file
  var getData = function() {
      for(var i = 0; i < obj.length; i++) {
          for(var e in obj[i]){
            var username_cm_on = obj[i][e]['username_cm_on'];
            var nonadmin_username_cm_on = obj[i][e]['nonadmin_username_cm_on'];
            var username_cm_off = obj[i][e]['username_cm_off'];
            var nonadmin_username_cm_off = obj[i][e]['nonadmin_username_cm_off'];
              return {
                username_cm_on: username_cm_on,
                nonadmin_username_cm_on: nonadmin_username_cm_on,
                username_cm_off: username_cm_off,
                nonadmin_username_cm_off: nonadmin_username_cm_off
              }
          }
      }
    }

  var config = {
    env: env,
    data: getData(),
  }

  return config;
}

我尝试了几种方法将env-data.json文件加载到karate-config.js中,如下所示

var envData =  call read('classpath:env_data.json');

因此,尝试以下

var envData =  read('classpath:env_data.json');
var envData =  require("./env_data.json");
var envData =  require('./env_data.json');

但是,在读取json文件时仍然面临问题。对此有帮助。


阅读 327

收藏
2020-07-27

共1个答案

一尘不染

我认为您过度复杂化了JSON。您只需要一个对象,而无需顶层数组。只需将其用作env_data.json

{
   "qa":{
      "username_cm_on":"test_cm_on_qa",
      "password_cm_on":"Test123$",
      "nonadmin_username_cm_on":"test_non_admin_cm_on_qa",
      "nonadmin_password_cm_on":"Test123$",
      "username_cm_off":"test_cm_off_qa",
      "password_cm_off":"Test123$",
      "nonadmin_username_cm_off":"test_non_admin_cm_off_qa",
      "nonadmin_password_cm_off":"Test123$",
      "zuul_urls":{
         "home-sec-uri":"https://qa.abc.com/qa/home-sec-uri",
         "home-res-uri":"https://qa.abc.com/qa/home-res-uri"
      }
   },
   "uat":{
      "username_cm_on":"test_cm_on_uat",
      "password_cm_on":"Test123$",
      "nonadmin_username_cm_on":"test_non_admin_cm_on_uat",
      "nonadmin_password_cm_on":"Test123$",
      "username_cm_off":"test_cm_off_uat",
      "password_cm_off":"Test123$",
      "nonadmin_username_cm_off":"test_non_admin_cm_off_uat",
      "nonadmin_password_cm_off":"Test123$",
      "zuul_urls":{
         "home-sec-uri":"https://uat.abc.com/qa/home-sec-uri",
         "home-res-uri":"https://uat.abc.com/qa/home-res-uri"
      }
   }
}

然后这karate-config.js将起作用:

function() {
  var env = 'qa'; // karate.env
  var temp = read('classpath:env_data.json');
  return temp[env];
}

而且您的测试可以更具可读性:

Given url zuul_urls['home-sec-uri']
2020-07-27