3

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.

1 Answer 1

11

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

1
  • 1
    This is awesome, resolved the issue that i was facing. Also, a much cleaner way of handling as json object rather than json array. Apr 6, 2018 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.