Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 纽曼测试失败。(JSON.parse语法错误)_Javascript_Json_Postman_Newman_Postman Collection Runner - Fatal编程技术网

Javascript 纽曼测试失败。(JSON.parse语法错误)

Javascript 纽曼测试失败。(JSON.parse语法错误),javascript,json,postman,newman,postman-collection-runner,Javascript,Json,Postman,Newman,Postman Collection Runner,更准确地说,我和邮递员有一个问题,他的命令行对应人员纽曼。《邮递员》中的测试运行和执行都很好,但《纽曼》中的测试失败得很惨 当我从代码中删除两行代码时,我想我已经隔离了源代码 let positiveAnswers = JSON.parse(pm.environment.get("positiveAnswers")); let negativeAnswers = JSON.parse(pm.environment.get("negativeAnswers")); 问题是,当我单独运行它时,它

更准确地说,我和邮递员有一个问题,他的命令行对应人员纽曼。《邮递员》中的测试运行和执行都很好,但《纽曼》中的测试失败得很惨

当我从代码中删除两行代码时,我想我已经隔离了源代码

let positiveAnswers = JSON.parse(pm.environment.get("positiveAnswers"));
let negativeAnswers = JSON.parse(pm.environment.get("negativeAnswers")); 
问题是,当我单独运行它时,它工作正常,与其他代码一起运行时,它会报告语法错误。代码在没有它的情况下运行良好,但在使用它时失败。我已经仔细检查了所有东西,运行语法检查程序Esprima

整个代码看起来像这样(请注意,这是正在进行的工作),所以有人能告诉我这张图片有什么问题吗


另外,我试图解析的数组看起来像i64.tinypic.com/1zbrw5c.png,我通过节点i65.tinypic.com/1zlcapz.png运行它

const newman = require('newman');
const dotenv = require('dotenv');
const ObjectID = require('mongodb').ObjectID;
var async = require("async");
async.series([
 function(next) {
  newman.run({
   collection: require('../alchemy.json'),
   globals: require('../alch_env.json'),
   reporters: 'cli',
   bail: false
  }, next);
}]) 

如有任何建议,我们将给予小费


干杯

另外,我试图解析的数组类似于下面的“如何运行newman”。也许您只是忘记了使用--environment[file | URL](或-e)?您不应该告诉我们在哪里使用Node或CLI中的newman吗?还有,你不应该给我们一张你是如何做到这一点的图片吗?很难帮助你。尝试添加更多信息。@BorysFursov我通过节点运行它
        // Check the status of a request
pm.test("Retrieval of a question(s) successful!", function () {
    pm.response.to.have.status(200);
});
// Check the response time 
pm.test("Response time is acceptable", function () {
    pm.expect(pm.response.responseTime).to.be.below(3500);
});
// Check if there is a response body
var contentTypeHeaderExists = responseHeaders.hasOwnProperty("Content-Type");
tests["Has Content-Type"] = contentTypeHeaderExists;
if (contentTypeHeaderExists) {
    tests["Content-Type is application/json"] = 
      responseHeaders["Content-Type"].has("application/json");
}
// Parse in the response body to check the response
var jsonData = JSON.parse(responseBody);
if (responseCode.code === 200) {
//Check for fields presence
pm.test("Returns a location order", function () {
    pm.expect(jsonData).to.have.property("locationOrder");
});
if(typeof jsonData.locationOrder  === "string"){
    console.log('Location order is generated');
}else{
    console.log('Location order is not generated');
}
pm.test("Returns a question", function () {
    pm.expect(jsonData).to.have.property("question");
});
if(typeof jsonData.question  === "string"){
    console.log('Question is generated');
}else{
    console.log('Question is not generated');
}
// Suplement the basic data for variables
const step = jsonData.locationOrder;
const startstep = "A01";
const startresponse = "Blank";
const pass = "pass_question";
const positive = "yes";

let positiveAnswers = JSON.parse(pm.environment.get("positiveAnswers"));
let negativeAnswers = JSON.parse(pm.environment.get("negativeAnswers"));

// if (pm.environment.get("step") != "A01" && pm.environment.get("response") != "Yes"){
// pm.environment.set("step", startstep);
// pm.environment.set("response", positive);
// }

// Do we even want schema validation?
 var schema = {
  "properties": {
    "locationOrder": {
      "type": "string"
    },
    "question": {
      "type": "string"
  },
    "additionalProperties": false,
    "required": ["locationOrder", "question"]
 }
};
tests["Valid json format is returned"] = tv4.validate(jsonData, schema);
tv4.validate(jsonData, schema, false, true);
console.log(positiveAnswers);
// Start the dance
switch (true) {
    case step == "A01":
// Check if there are still responses to loop through
if (positiveAnswers.length > 0) {
// Check the status of a request
pm.test("Retrieval of a next question is successful!", function () {
    pm.response.to.have.status(200);
});
// Check the status of a request
pm.test("Next step is as expected", function () {
    pm.expect(pm.response.json().locationOrder).to.be.equal("A01");
});    
    pm.environment.set("response", positiveAnswers.shift());
    pm.environment.set("positiveAnswers", JSON.stringify(positiveAnswers));
// Set next request to be executed as well as what parameters to be used
  postman.setNextRequest("Get question CI");
} else {
    postman.clearEnvironmentVariable("positiveAnswer");
    postman.clearEnvironmentVariable("positiveAnswers");
}
        console.log("Data inputed is: " + pm.environment.get("response"));

        break;
    case step == "A02":
        console.log("Data inputed is: " + pm.environment.get("city"));
        pm.environment.set("step", jsonData.locationOrder );  
        pm.environment.set("response", pass);  
        pm.environment.set("question", jsonData.question);  
        break;
    case step == "A03":
        console.log("Data inputed is: " + pm.environment.get("city"));    
        pm.environment.set("step", jsonData.locationOrder );    
        pm.environment.set("question", jsonData.question);          
        break;
     // If this step is positive/(pass), it will transfer you to B01
    case step == "A04":
        console.log("Data inputed is:" + pass );        
        pm.environment.set("step", jsonData.locationOrder );  
        pm.environment.set("response", pass);  
        pm.environment.set("question", jsonData.question);  
        break;
    case step == "A05":
        console.log("Data inputed is: " + pm.environment.get("response"));
        pm.environment.set("step", jsonData.locationOrder );  
        pm.environment.set("response", positive );  
        pm.environment.set("question", jsonData.question);  
        break;
    case step == "A06":
        console.log("Data inputed is: " + pm.environment.get("city"));
        pm.environment.set("response", pass);  
        pm.environment.set("step", jsonData.locationOrder ); 
        pm.environment.set("question", jsonData.question);  
        break;   
    case step == "A07":
        console.log("Data inputed is: " + pm.environment.get("city"));
        pm.environment.set("response", pass);  
        pm.environment.set("step", jsonData.locationOrder ); 
        pm.environment.set("question", jsonData.question);  
        break;    
    case step == "A08":
        console.log("Data inputed is: Ringmaster Über Alles!");
        pm.environment.set("step", startstep );   
        pm.environment.set("response", startresponse ); 
        pm.environment.set("question", jsonData.question);   
        break;  
    case step == "B01":
        console.log("Data inputed is: " + positive);
        pm.environment.set("response", positive);  
        pm.environment.set("step", jsonData.locationOrder );    
        pm.environment.set("question", jsonData.question); 
        break;    
          // If step is B01
    case step == "B02":
        console.log("Data inputed is: " + positive);
        pm.environment.set("response", positive);  
        pm.environment.set("step", jsonData.locationOrder ); 
        pm.environment.set("question", jsonData.question); 
        break;          
         // If conversation is completed
        case step == "Complete":
        console.log("Data inputed is: " + pm.environment.get("response"));
        pm.environment.set("step", startstep );   
        pm.environment.set("question", jsonData.question);  
        postman.clearEnvironmentVariable("response");
        break;  
             // Any other step
    default:
        console.log("Iteration successful!");
}
if (pm.environment.get("question") != "This is the end. Thank you." ){
    postman.setNextRequest ("Get question CI");
}else{
    postman.setNextRequest ("Get question II");
}
}