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

时间:2018-06-18 12:39:38

标签: javascript json postman newman postman-collection-runner

我对邮递员有一个问题,更确切地说,他的命令行对手纽曼。测试在Postman中运行并执行得很好,但是他们在Newman中失败了。

我认为当我从代码中删除2行时,我已经隔离了源代码

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

        // 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");
}
}

任何建议,小费都会受到关注。

干杯!

0 个答案:

没有答案