3

I am trying to set karate.env through netty server and respond back what environment is used. Update made to karate.env using java system.setProperty is not reflecting in feature files. But java have updated system.setProperty correctly.

Any input will help. Thanks.

Background:
* def setEnvVar = Java.type('CMA_Release.Java_Lib.setEnvVar')

Scenario: pathMatches('/orangeFMW/psd2_cma/cctoken') && methodIs('post')
* call setEnvVar.set('karate.env',request.Environment)
* print request.Environment
* print karate.env
# * call read('classpath:karate-config.js')
# few other operations
* def response = "Selected Environment : " + karate.env

Response : Selected Environment : null

Server log
scenario matched: pathMatches('/orangeFMW/psd2_cma/cctoken') && 
methodIs('post')
UAT // Java update the value correctly
not a js function or feature file: 
setEnvVar.set('karate.env',request.Environment) - [type: NULL, value: null]
[print] UAT
[print] 

Java class
public class setEnvVar {

public static void set(String key, String value){
    System.setProperty(key, value);
    System.out.println(System.getProperty(key,value));
}
Umesh Kandhalu
  • 123
  • 3
  • 10

1 Answers1

0

EDIT: did not notice you were asking about the server side (netty).

No, you cannot rely on the karate.env being passed like this from client to server. What you should do is when you start your test, BOTH the client and server should know about the environment. You already know how to do this for the client (karate-config.js).

When you start the netty server, you can optionally pass a Java Map argument, and all keys and values will be available in the server-side feature file. So all you need to do is pass a key like karateEnv in this Map.

server = FeatureServer.start(file, 0, false, Collections.singletonMap("karateEnv", "blah"));

Also see this answer for more ideas: https://stackoverflow.com/a/52272220/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248