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

时间:2018-04-06 10:42:12

标签: javascript json karate

我有超过6个环境,我必须运行相同的rest 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');

我知道Karate - How to import json data

从这个堆叠流程回答Peter Thomas开始,上述内容无效

所以,试过以下的

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

但是,仍然面临阅读json文件的问题。感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

我认为你过度复杂了你的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']