Raymond
Raymond

Reputation: 614

How to pass multiple parameters to callSingle karate on karate-config.js

I have a .feature file that will receive argument from another feature file as __arg to generate token for oAuth

 Given url urlRefreshToken
 Given def json = __arg
 And header Content-Type = 'application/json; charset=utf-8'
 And request json
 * header Authorization = 'Bearer' + __arg.refresh_token
 When method POST
 Then status 200
 * def bearer = 'Bearer ' + response.access_token

I am trying to centralise the token generation for only one time on karate-config.js. However I cannot seem to be able to use karate.callSingle() with passed parameters.

I use the feature file to generate token on other feature files as:

* def getToken = call read('classpath:features/Utils/GetToken.feature') refreshTokenRaymond
* header Authorization = getToken.bearer

I am trying to invoke the feature file for generating token on karate-config.js to no avail. I tried to pass in the additional parameter like this on karate-config.js:

var config = {
    baseUrl: 'url',
    urlRefreshToken: 'url',
    refreshToken: '{refreshToken: refreshToken}'
  };
var token = karate.callSingle('classpath:features/Utils/GetToken.feature', [config, config.refreshToken])

I wonder if it is possible to pass multiple parameter to karate.callSingle() called from karate-config.js?

Any help will be greatly appreciated. Thanks!

Upvotes: 1

Views: 2024

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

call and karate.callSingle() take only one argument, but you can easily create a new JSON out of other JSONs. Actually since you seem to be passing config as the argument - you can easily access config.refreshToken as __arg.refreshToken.

Your code is very confusing - but hope that this hint is what gets you on your way:

Given def json = __arg.refreshToken

If you need to create a JSON out of other data - I think you already know how to do that:

var temp = { config: config, refreshToken: refreshToken };
var token = karate.callSingle('classpath:features/Utils/GetToken.feature', temp);

Upvotes: 3

Related Questions