提问者:小点点

空手道-config.js中有可能有两个baseUrls吗?


我有一个场景,我将与其他产品进行集成测试。所以我有两个 URL,每个 URL 都有单独的授权令牌。是否可以在空手道配置中有两个 baseUrl.js如果是,我们如何在功能文件中访问它们?

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
                 }


共2个答案

匿名用户

您可以在空手道-config.js文件中进行如下更改,

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;
}

对于授权,您可以创建如下所示的config.feature文件:,

Feature: Auth

  Scenario: Auth
    * def Auth = 'auth'

在功能文件中,您可以使用URL和授权

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

匿名用户

一个最好的方法是用你的应用程序名作为前缀,

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'
            };
        };
    }
}

在功能使用中使用时

* 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>

这样,您可以一次为多个应用程序进行全局配置。