I have an issue with Postman, to be more precise, his command-line counterpart, Newman. The tests run and execute just fine in Postman, but they fail miserably in Newman.
I THINK I've isolated the source as when I delete 2 lines from the code
let positiveAnswers = JSON.parse(pm.environment.get("positiveAnswers"));
let negativeAnswers = JSON.parse(pm.environment.get("negativeAnswers"));
Problem is when I run it just by itself, it works fine, in conjunction with the rest of the code, it reports a syntax error. The code runs fine without it but fails with it. I've doublechecked everything, ran syntax checker Esprima.
The whole code looks like this (mind you, it's work in progress) so can anybody tell me what is wrong with this picture.
Also, the array I am trying to parse looks like this i64.tinypic.com/1zbrw5c.png and I run it via node 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);
}])
and
// 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");
}
}
Any advice, tip would be appriciated.
Cheers!