我必须在6个以上的环境中运行相同的其余api脚本集。因此,我已将所有测试数据和端点/资源路径存储在json文件中。然后,我尝试将此json文件读入我的karate-
config.js文件,这是因为我要获取与从命令行(karate.env)传递的环境相对应的数据,该环境正在读入我的空手道-config.js文件
以下是我的json文件示例
[
{
"qa":{
"username_cm_on":"test_cm_on_qa",
"password_cm_on":"Test123$",
"nonadmin_username_cm_on":"test_non_admin_cm_on_qa",
"nonadmin_password_cm_on":"Test123$",
"username_cm_off":"test_cm_off_qa",
"password_cm_off":"Test123$",
"nonadmin_username_cm_off":"test_non_admin_cm_off_qa",
"nonadmin_password_cm_off":"Test123$",
"zuul_urls":{
"home-sec-uri":"https://qa.abc.com/qa/home-sec-uri",
"home-res-uri":"https://qa.abc.com/qa/home-res-uri"
}
}
},
{
"uat":{
"username_cm_on":"test_cm_on_uat",
"password_cm_on":"Test123$",
"nonadmin_username_cm_on":"test_non_admin_cm_on_uat",
"nonadmin_password_cm_on":"Test123$",
"username_cm_off":"test_cm_off_uat",
"password_cm_off":"Test123$",
"nonadmin_username_cm_off":"test_non_admin_cm_off_uat",
"nonadmin_password_cm_off":"Test123$",
"zuul_urls":{
"home-sec-uri":"https://uat.abc.com/qa/home-sec-uri",
"home-res-uri":"https://uat.abc.com/qa/home-res-uri"
}
}
}
]
下面是我的karate-config.js文件
function() {
// var env = karate.env; // get system property 'karate.env'
var env = 'qa';
var cm = 'ON';
var envData = call read('classpath:env_data.json'); //require("./env_data.json");
// write logic to read data from the json file _ Done, need testing
karate.log('karate.env system property was:', env);
switch(env) {
case "qa":
if(cm === 'ON'){
config.adminusername_cm_on = getData().username_cm_on;
config.adminpassword_cm_on = "";
config.nonadminusername_cm_on = getData().nonadmin_username_cm_on;
config.nonadminpassword_cm_on = "";
}else if(cm === "OFF") {
config.adminusername_cm_off = getData().username_cm_off;
config.adminpassword_cm_off = "";
config.nonadminusername_cm_off = getData().nonadmin_username_cm_off;
config.nonadminpassword_cm_off = "";
}
break;
case "uat":
break;
default:
break;
}
// This method will return the data from the env_data.json file
var getData = function() {
for(var i = 0; i < obj.length; i++) {
for(var e in obj[i]){
var username_cm_on = obj[i][e]['username_cm_on'];
var nonadmin_username_cm_on = obj[i][e]['nonadmin_username_cm_on'];
var username_cm_off = obj[i][e]['username_cm_off'];
var nonadmin_username_cm_off = obj[i][e]['nonadmin_username_cm_off'];
return {
username_cm_on: username_cm_on,
nonadmin_username_cm_on: nonadmin_username_cm_on,
username_cm_off: username_cm_off,
nonadmin_username_cm_off: nonadmin_username_cm_off
}
}
}
}
var config = {
env: env,
data: getData(),
}
return config;
}
我尝试了几种方法将env-data.json文件加载到karate-config.js中,如下所示
var envData = call read('classpath:env_data.json');
因此,尝试以下
var envData = read('classpath:env_data.json');
var envData = require("./env_data.json");
var envData = require('./env_data.json');
但是,在读取json文件时仍然面临问题。对此有帮助。
我认为您过度复杂化了JSON。您只需要一个对象,而无需顶层数组。只需将其用作env_data.json
:
{
"qa":{
"username_cm_on":"test_cm_on_qa",
"password_cm_on":"Test123$",
"nonadmin_username_cm_on":"test_non_admin_cm_on_qa",
"nonadmin_password_cm_on":"Test123$",
"username_cm_off":"test_cm_off_qa",
"password_cm_off":"Test123$",
"nonadmin_username_cm_off":"test_non_admin_cm_off_qa",
"nonadmin_password_cm_off":"Test123$",
"zuul_urls":{
"home-sec-uri":"https://qa.abc.com/qa/home-sec-uri",
"home-res-uri":"https://qa.abc.com/qa/home-res-uri"
}
},
"uat":{
"username_cm_on":"test_cm_on_uat",
"password_cm_on":"Test123$",
"nonadmin_username_cm_on":"test_non_admin_cm_on_uat",
"nonadmin_password_cm_on":"Test123$",
"username_cm_off":"test_cm_off_uat",
"password_cm_off":"Test123$",
"nonadmin_username_cm_off":"test_non_admin_cm_off_uat",
"nonadmin_password_cm_off":"Test123$",
"zuul_urls":{
"home-sec-uri":"https://uat.abc.com/qa/home-sec-uri",
"home-res-uri":"https://uat.abc.com/qa/home-res-uri"
}
}
}
然后这karate-config.js
将起作用:
function() {
var env = 'qa'; // karate.env
var temp = read('classpath:env_data.json');
return temp[env];
}
而且您的测试可以更具可读性:
Given url zuul_urls['home-sec-uri']
在通过Maven运行时,我需要传递给Karate的附加设置可以在karate-config.js中获得。目前,我可以使用karate.env属性传入一个字符串--是否需要将参数编码为JSON对象并通过这个属性传入,或者我可以做如下操作: 我肯定我错过了一些明显的东西...
任何帮助都将不胜感激。谢谢!
我有一个带有函数auth()的basic-auth.js文件。 我想在karate-config.js内部使用该功能,哪种方式最好?是用karate.call? 谢谢!
我试图在gitlab ci管道中使用空手道网络罐。我正在拉一个包含罐子的图像作为管道中的一个步骤。我能够执行测试只是罚款的不安全的服务。像这样: 现在,我试图将凭证传递到安全服务的空手道功能中,但无法从jar接口找到这些功能。 我尝试过这样传递凭据: REQUEST_PASSWORD和REQUEST_USER是gitlab变量,我可以在gitlab-ci中使用。 当我运行管道时,我得到: 空手道N
我想要在mvn命令中传递多个参数,并且应该在karate-config.js文件中读取。 我不想声明任何.prop文件,希望在运行时获得这些值。在支柱下面。定义为读取这些参数,但无法从中获得: 我需要帮助来实现这一点 卡拉特-版本=0.9.0 我还提到了这个链接:通过命令行通过Maven向karate-config.js传递附加参数,但没有成功
我正在使用以下语句在karate-config.js中配置retry: 运行测试时,控制台中出现以下错误: [main]错误com.intuit.karate-javascript函数调用失败:意外的“configure”键:“retry”。 有人能提出解决这个问题的办法吗?