Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

javascript - Pass Json to karate-config.js file

I have more than 6 environments against which i have to run the same set of rest api scripts. For that reason i have stored all that test data and the end points/resource paths in a json file. I then try to read this json file into my karate-config.js file, this is because i want to fetch the data corresponding to the environment that is being passed from the command line (karate.env), which am reading into my karate-config.js file

Below is my json file sample

[
  {
    "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"
      }
    }
  }
]

and below is my karate-config.js file

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

I tried several ways to load the env-data.json file into karate-config.js as below

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

I know the above is not valid from this stackover flow answer Karate - How to import json data by Peter Thomas

So,tried with the below ones

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

But, still facing issues with reading the json file. Appreciate help on this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you over-complicated your JSON. You just need one object and no top-level array. Just use this as 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"
      }
   }
}

And then this karate-config.js will work:

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

And your tests can be more readable:

Given url zuul_urls['home-sec-uri']

If you have trouble understanding how this works, refer to this answer: https://stackoverflow.com/a/59162760/143475


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...