1

In karate-config.js I set the baseUrl based on an environment. I want to be able to get this url in Java so I can get the corresponding server name in the url. Please show me how to do this. My karate-config.js looks something like this:

  if (env == 'test') {
      var baseUrl = 'http://server1:8080/api';
  } else if (env == 'dev') {
      var baseUrl = 'http://server2:8080/api';
  }

I wrote a Java utility to connect to the database and run a query. But I need to know if the database is on server1 or server2 as in the example.

1 Answers1

0

Assuming that baseUrl is added to the JSON returned by karate-config.js:

There is nothing special. baseUrl can be used "as-is" and passed as a method argument if you call any Java code. For example:

* def MyUtil = Java.type('com.myco.MyUtil')
* def result = MyUtil.doSomeWork(baseUrl)

Please read the docs for more: https://github.com/intuit/karate#calling-java

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I think my question is not clear. Supposed my dbUtils have a method called runQuery that executes a query and returns the results. In my feature file I have something like this: ` * def myUtil = Java.type('dbUtils') * def results = myUtil.runQuery(query) ` In the method runQuery I want to get the server name in the baseUrl so I can connect to the database on that server. I actually need to open a config file on that server and read the database connection information in the config file. So the connection may be different depending on what server I'm running the tests on. –  Aug 01 '19 at 21:39
  • No, Sir. I already wrote the method and it works. But I'm hard coding the server name right now. When I run on a different server, I have to change the name. I'm trying to read the server name dynamically. –  Aug 01 '19 at 21:45
  • I found a workaround for now which is getting the server name from within the feature file and pass it back to Java. –  Aug 01 '19 at 21:56
  • @bhc that is not a workaround. that is the solution. if you are getting the server name from a variable. and if the variable is coming from `karate-config.js`. is this so hard to understand ? I want to know so that I can fix the documentation – Peter Thomas Aug 01 '19 at 22:13
  • The documentation is fine. I just don't want to have to modify many of my feature files. I prefer to change only the method so the feature files still work. I'm going to try to see if I can set an environment variable in karate-config.js then I can get it in Java. –  Aug 02 '19 at 00:43
  • @bhc is that all you need ? to use the environment. yes your question is terrible, for example look at the question title itself - it talks about variables. please be more clear next time. and read this, it will answer all your questions: https://stackoverflow.com/a/57261852/143475 – Peter Thomas Aug 02 '19 at 00:52
  • My initial thought was the variable "baseUrl" in karate-config has what I need. So it would solve my problem if I could get it. But now I found out I could not get it. So I thought of another way which is to use an environment variable. Thank you for the link. I know how to get an environment variable in Java but not how to set it in JavaScript or Java. I may not be easy or possible. –  Aug 02 '19 at 01:31
  • @bhc for **you** it may not be easy or possible ! all the best :) – Peter Thomas Aug 02 '19 at 03:29
  • I decided to use System.setProperty and System.getProperty. That worked well for me. Thanks. –  Aug 02 '19 at 12:49
  • 1
    @bhc there are better ways. but I guess reading the documentation is too hard – Peter Thomas Aug 02 '19 at 13:48