1

I have config-base.js which is loading the 'env_data.json' file. The java script in config-base.js is not setting the values in config as expected. I am trying to collect list of fields under common and appA parentElement and setting under config Json. This I could not achieve it.

I am passing appName as 'appA' and env as 'uat' via CLI command as like below java -cp karate:. -Dkarate.config.dir=. -Dkarate.appName=appA -jar karate.jar myFeature.feature -T 1 -t @smoke -e uat

I am trying to build config JSON as like below via JS.

config {
    baseUrl : 'http://uat.someurl.com'
    parameterX : 'x2',
    parameterY :'y2'
    someOtherBackendUrlA : 'http://uat.appA.com'
    parameterA : 'a2',
    parameterB :'b2'
}

My env_data.js is look like this

    [{
    appName : 'common',
    data: {
        uat: {
            baseUrl : 'http://uat.someurl.com'
            parameterX : 'x2',
            parameterY :'y2'
        }
    },{
    appName : 'appA',
    data: {
        uat: {
            someOtherBackendUrlA : 'http://uat.appA.com'
            parameterA : 'a2',
            parameterB :'b2'
        }
    }}]

My karate-base.js is written like below

function fn() {
  var env = karate.env;
  karate.log('karate.env system property was :', env);
  if (!env) {
    env = 'dev';
  }
  var listOfApplications = read('./env_data.json');
  var config = {};
  switch (env) {
    case "uat":
      for (var i = 0; i < listOfApplications.lenght; i++) {
        var appObject = listOfApplications[i];
        if (karate.properties['karate.appName'] == appObject.appName) {
          config = appObject.data[env];
        } else if ('common' == appObject.appName) {
          config.map(element => {
              element.baseUrl = appObject.data[env].baseUrl;
            }
          }
          break;
    }
karate.configure('connectTimeout', 5000);
karate.configure('readTimeout', 5000);
return config;
}

I can understand that, I am not mapping config values properly in else condition. I have tried map and foreach. but for these both functions, I got error saying org.graalvm.polyglotException:TypeError: config.map is not a funtion

I like to construct my config JSON along with common parameters on each application values. Please show some light on how can I fix this.

Simbu
  • 766
  • 1
  • 12
  • 37
  • sorry I'm going to pass on this question until / if you can make it simpler. if you need urgent support, follow this process (and make sure you follow the instructions) https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Apr 20 '22 at 16:32
  • @PeterThomas simplified my question. Please consider to help – Simbu Apr 20 '22 at 17:37
  • sorry, I pass. your code example is too complex. my suggestion is please simplify it, or get it reviewed by someone who knows javascript well. also please read this answer, maybe that's all you need to do: https://stackoverflow.com/a/49693808/143475 - I repeat, submit a minimal example, and then maybe someone will help you – Peter Thomas Apr 20 '22 at 18:23

1 Answers1

1

I have fixed the issue by below snippet.

fn();

function fn() {
  const env = 'uat';
  const userInput = 'appA';
  var listOfApplications = [
   {
      "appName":"common",
      "data":{
         "uat":{
            "baseUrl":"http://commonurl.com",
            "parameterX":"x",
            "parameterY":"y"
         }
      }
   },
   {
      "appName":"appA",
      "data":{
         "uat":{
            "baseUrl":"http://uat.appA.com",
            "parameterA":"a",
            "parameterB":"b"
         }
      }
   }
];
  
  var config={};
      for (var i = 0; i < listOfApplications.length; i++) {
          if (userInput == listOfApplications[i].appName) {
              config[userInput] = {...listOfApplications[i].data[env]};
          }else if ('common' == listOfApplications[i].appName) {
              config = {...listOfApplications[i].data[env]};
          }
      }
  
  //console.log(config);
  console.log(JSON.stringify(config));
  return config;
}
Simbu
  • 766
  • 1
  • 12
  • 37