Reputation: 55
I have a scenario where I will be doing the integration test with other product. So I have two URLs and each has separate Authorization Token. Is it possible to have two baseUrls in karate-config.js and if yes how can we have access them in the feature file?
karate-config.js
if(env == 'pre-prod')
{
config.baseUrl='url1'
}
headers.js
function() {
var authheaders = {};
switch(baseUrl){
case 'url1':authheaders = { Authorization: 'auth-token'}
break;
}
return authheaders
}
Upvotes: 2
Views: 1424
Reputation: 76
you can make the following changes in the karate-config.js file like below,
function() {
var env = karate.env; // get java system property 'karate.env'
karate.log('karate.env system property was:', env);
if (!env) {
env = 'local'; // a custom 'intelligent' default
}
var config = { // base config
env: env,
profile: 'local',
DataSetUrl: 'http://localhost:9005/v0.2.4/',
DMUrl: 'http://localhost:8004/v0.2.3/',
SchemaUrl: 'http://localhost:8006/v0.2.3/',
URUrl: 'http://localhost:9005/v0.2.4/',
username: 'null',
password: 'null'
};
if (env == 'staging') {
// over-ride only those that need to be
var config = { // base config
env: env,
profile: 'staging',
baseUrl: 'staging url',
dbname: 'url2',
server_url: '12.12.12.12',
server_port: 31700,
username: 'mongo-staging-user',
dbname: 'db_name',
password: 'passoword'
};
}
}
// don't waste time waiting for a connection or if servers don't respond
// within 5 seconds
karate.configure('connectTimeout', 5000);
karate.configure('readTimeout', 5000);
return config;
}
For the Authorisation you can create a config.feature file which will look like this,
Feature: Auth
Scenario: Auth
* def Auth = 'auth'
And in the feature file you can use the urls and authorization
Feature: Test
Background:
* def config = call read('classpath:features/Config.feature')
* def auth = config.Auth
Scenario:1
* url DMUrl
Given path 'yourpath'
When method get
Then status 200
#
* url DataSetUrl
Given path 'yourpath'
When method get
Then status 200
Upvotes: 3
Reputation: 4239
One of the best way would be prefix with your app-name,
if (env == 'pre-prod') {
config.AppOne = {
baseUrl: 'url1',
getHeader: function() {
return {
Authorization: 'App one token'
};
}
};
config.AppTwo = {
baseUrl: 'url2',
getHeader: function() {
return {
Authorization: 'App two token'
};
};
}
}
while using in feature use
* url AppOne.baseUrl
* path 'apiOne/endpoint'
* headers AppOne.getHeader()
# <other steps for App One>
* url AppTwo.baseUrl
* path 'apiTwo/endpoint'
* headers AppOne.getHeader()
# <other steps for App Two>
This way you can have a global configuration for more than one application at a time.
Upvotes: 2