1

Getting callSingle() cached result is an exception. Here login feature calling basic-auth.js in background which provides basic auth and login endpoint returns bearer token in response. Whenever tried to call feature file with callSingle its throwing exception and getting error as org.graalvm.polyglot.PolyglotException: ReferenceError: "baseURL" is not defined.

Missing something here? tried to go through previous issues but unable to resolve it. Can someone help whats wrong here. Let me know if you need any more details.

login feature file -

Feature: Login API

Background:
  * url baseURL
  * def param = {refId: 'ABC123', institutionId: '1234'}
  * def token = call read('file:src/test/java/scripts/basic-auth.js') { username: 'abc, password: 'test123' }
   

  @smoke
Scenario: Login
  * path '/test/login'
  * request requestPayload = read('file:src/test/java/payloads/loginPayload.json')
  *  header Authorization = token
  * headers param
  * method post
  * status 200
  * def auth = response.payLoad.authorization
  * print auth

config file -

function fn() {
  var env = karate.env; // get system property 'karate.env'
  karate.log('karate.env system property was:', env);
  if (!env) {
    env = 'sit';
  }
  var config = {
    env: env,
    baseURL : '',
    authorisation: auth()
  }
  if (env == 'qa') {
    config.baseURL = 'http://testqa.com/'
    userName = 'abc'
    passWord = 'test123'
  } else if (env == 'sit') {
    config.baseURL = 'http://testsit.com/'
    userName = 'abc'
    passWord = 'test123'
  }
  // var auth = karate.callSingle('file:src/test/java/examples/01_login/login.feature')
  // config.auth
  // karate.log(auth)
  
  function auth() {
    var auth = karate.callSingle('file:src/test/java/examples/01_login/login.feature')
    karate.log(auth)
    return auth;
  }
  return config;
}

Tried to look through existing issues and examples however unable to resolve it. Appreciate if you can provide help or any pointers. Whenever commented callSingle there is no issues and there is no error for baseURL. Thanks in advance.

Sachin
  • 11
  • 3

1 Answers1

0

I think when you make the call, the config processing is not yet complete. But you can force variable setting via a second argument like this:

var auth = karate.callSingle('file:src/test/java/examples/01_login/login.feature', config)
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks Peter. Appreciate your quick response. Tried with passing second argument as config however still getting same issue. – Sachin Mar 20 '23 at 09:27
  • @Sachin ok then I can't help you unless you follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Mar 20 '23 at 09:31
  • Sure. I will follow this process. Thanks a lot. – Sachin Mar 20 '23 at 09:32
  • 1
    Able to solve it by adding token which is getting in the response of login api. var auth = karate.callSingle('file:src/test/java/examples/01_login/login.feature', config).authToken – Sachin Mar 30 '23 at 06:17