Postman Script在Postman中完美执行,在Newman中失败

时间:2018-07-24 15:30:11

标签: javascript postman newman postman-collection-runner

我在邮递员中有一个脚本,该脚本可以完美执行,但是当我尝试在纽曼中运行时,我在test-script中收到json错误。脚本是:

const state = pm.environment.get("switch");

    if (state == "serbian" && state != "undefined" && state !== null ) {

pm.test("Question is correct", function () {
    pm.expect(pm.response.json().question).to.include("U cilju osiguranja 
sigurnosti i zaštite podataka, molim Vas da odgovorite na par pitanja. Koje 
je ime grada koji odgovara adresi vašeg prebivališta?");
});

} else if (state == "english" && state != "undefined" && state !== null) {
pm.test("Question is correct", function () {
    pm.expect(pm.response.json().question).to.include("OK. Let's start with 
few quick questions.||What's the name of the city you live in?");
});
}

整个测试看起来像这样(这不会影响问题)

// Parse in the response body to check the response
var jsonData = JSON.parse(responseBody);

// Run the common tests, I know eval is evil
eval(globals.commonTests)();

// Suplement the basic data for variables
var positiveAnswers = JSON.parse(pm.environment.get("positiveAnswers"));

// Check if there are still responses to loop through
if ( positiveAnswers.length > 0 && positiveAnswers != "undefined" && 
positiveAnswers !== null ) {

let positiveAnswers = JSON.parse(pm.environment.get("positiveAnswers"));
        pm.environment.set("positiveAnswer", positiveAnswers.shift());
        pm.environment.set("positiveAnswers", 
JSON.stringify(positiveAnswers));

// Check the status of a request
pm.test("Next step is as expected", function () {
    pm.expect(pm.response.json().locationOrder).to.be.equal("A01");
});   
    postman.setNextRequest("Get question D01 Positive");
    console.log ("Answer sent is: " + pm.environment.get("positiveAnswer"));
}
setTimeout(function(){
        console.log('Timeout!');
    },100);
// Do the validation of the response
const state = pm.environment.get("switch");

    if (state == "serbian" && state != "undefined" && state !== null ) {
pm.test("Question is correct", function () {
    pm.expect(pm.response.json().question).to.include("U cilju osiguranja 
sigurnosti i zaštite podataka, molim Vas da odgovorite na par pitanja. Koje 
je ime grada koji odgovara adresi vašeg prebivališta?");
});
} else if (state == "english" && state != "undefined" && state !== null) {
pm.test("Question is correct", function () {
    pm.expect(pm.response.json().question).to.include("OK. Let's start with 
few quick questions.||What's the name of the city you live in?");
});
}

经过一些调试,我发现

const state = pm.environment.get("switch");

在纽曼返回null。

其想法是先检查该值,然后验证为不同语言返回的字符串。因此,我无法对其进行硬编码,而是将其保留在env中,并使用另一个脚本对其进行更改。 尝试做

var state = JSON.parse(pm.environment.get("switch"));
我所做的

将变量放在引号中,结果相同。有谁知道我该如何解决这个问题?

0 个答案:

没有答案