Search This Blog

Breaking

Friday, 8 October 2021

[Karate] Read authentication token from config file

 When you start enhancing your karate framework, there comes a time when you want to read your auth token information from your config file, so that it can be set once, and reused in entire test suite run.

If you are in this stage or even if you are seeing how you can read your auth token from config file, then this post will help you.











So initially, I was having a token generation feature file which I was calling in my feature file with the help of callSingle method in background.

* def result = karate.callSingle('classpath:TokenGeneration.feature')

*def token = result.token

* def tokenId = result.tokenId

Once I get the otken and tokenId, I can use it in my feature file.

And header token = token

And header tokenId = tokenId

Later, I thought to put this token feature inside karate config file, so that these values will be set once, and we can reuse it again.

So, for this, we need to go to karate config file and at the bottom, before return config, call this token feature.

var result =  karate.callSingle('classpath:TokenGeneration.feature', config);

authInfo = {authToken:result.token, authTokenId: result.tokenId};

return config;


Now in your feature file, you can use it like in this manner:

*def token =authInfo.authToken

* def tokenId = authInfo.authTokenId







No comments:

Post a Comment