1

我在邮递员中有一个脚本,它可以完美地执行,但是当我尝试在 Newman 中运行时,我在测试脚本中得到一个 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");

在 Newman 中返回 null。

这个想法是它检查值,然后验证为不同语言返回的字符串。所以我不能硬编码它,而是将它保存在 env 中,并用另一个脚本更改它。试图做

var state = JSON.parse(pm.environment.get("switch"));

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

4

0 回答 0