config = karate.callSingle('classpath:token-read.js', config);
function fn(config) {
var userAccessToken = Java.type("com.OAuth2Token");
config['OAuth2'] = {
adminUser: function () {
return userAccessToken.getAuthorizationHeader(name, url, users, "ADMIN");
},
};
return config;
}
Feature: Search data
Scenario: Sample
Given url url
Given path '/data'
And header Authorization = OAuth2.adminUser() // Using javascript fun here
And param request = {"up":10}
And print response
When method GET
Then status 200
javascript evaluation failed: OAuth2.adminUser(), TypeError: OAuth2.adminUser is not a function in <eval> at line number 1
以上功能文件在空手道0.9.2上可以正常使用,但在0.9.3上不能使用
答案 0 :(得分:0)
请阅读文档的这一部分:https://github.com/intuit/karate/tree/develop#restrictions-on-global-variables
此处复制:
仅当非JSON值(如Java对象引用或JS函数)位于从karate-config.js返回的JSON的“根”上时,才受支持。因此,这在下面是行不通的:
function fn() {
var config = {};
config.utils = {};
config.utils.uuid = function(){ return java.util.UUID.randomUUID() + '' };
// this is wrong, the "nested" uuid will be lost
return config;
}
推荐的最佳实践是按照此处描述的模式将uuid函数移动到通用功能文件中:
function fn() {
var config = {};
config.utils = karate.call('utils.feature')
return config;
}
但是您可以选择使用karate.toMap()来“包装”东西,以使嵌套对象不会“丢失”:
function fn() {
var config = {};
var utils = {};
utils.uuid = function(){ return java.util.UUID.randomUUID() + '' };
config.utils = karate.toMap(utils);
return config;
}
您可能需要将其升级到0.9.5.RC4,请尝试。