如何从 CMDB 系统检索初始配置并将其传递给 karate-config

How to retrieve the initial configurations from a CMDB system and pass it to karate-config

我有个情况。我需要向 CMDB 系统发送一个获取请求,并检索初始配置,如端点 URI、路径、数据库连接字符串等作为 JSON 对象。 CMDB 系统将发回一个 JSON 对象。我如何在 karate-config.js 中实现这一点。我应该编写自定义 javascript 函数还是我有任何内置功能?我检查了空手道对象部分,但是,不明白该怎么做。

我为此编写了一些自定义 js 函数。它们在我的系统中独立工作(我的机器上安装了 node.js),但不会在 karate-config.js.

中工作

第一个

var HttpClient = function () {
    this.get = function (aUrl, aCallback) {
        var XMLHttpRequest = require('xhr2');
        var anHttpRequest = new XMLHttpRequest();
        anHttpRequest.onreadystatechange = function () {
            if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
                aCallback(anHttpRequest.responseText);
        }
        anHttpRequest.open("GET", aUrl, true);
        anHttpRequest.send(null);
    }
}


var url = 'https://reqres.in/api/users/2';
var client = new HttpClient();
client.get(url, function (response) {
    var response1 = JSON.parse(response);
    console.log(response1)
});

第二

var axios = require('axios');
// Make a request for a user with a given ID
axios.get('https://reqres.in/api/users/2').then(function (response) {
  console.log(response)
}).catch(function (error) {
  console.log(error);
});

我想你忘记了空手道恰好非常擅长发出 HTTP 请求:)

您需要做的就是编写一个 re-usable 空手道功能文件,使 GET 成为 https://reqres.in/api/users/2。您可能需要计算出任何需要的 headers。

现在可以在 karate-config.js 中使用 karate.callSingle() 方法来完全按照您的意愿进行操作。看第 31 行:

https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/karate-config.js#L31