我对 Postman 有疑问,更准确地说,是他的命令行对应对象 Newman。测试在 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");
}
}
任何建议,小费都会被应用。
干杯!