mercredi 25 octobre 2017

How to use conditional statement with Postman's new pm.* API?

I have a question about how to use the new pm.* API with conditional statements. Please take a look at the following example code

if(pm.test("Status code is 200", function() {pm.expect(pm.response.code).to.equal(300);
})){

var token = pm.response.headers.get("Authorization");
pm.environment.set("JWT Token", token.substr(7));

pm.test("Response body is empty ", function () {
    pm.expect(pm.response.text().length).to.equal(0);
});
}

console.log(pm.test("Status code is 200", function() {pm.expect(pm.response.code).to.equal(300)}));

As I may want to perform certain tests e.g. only when 200 is returned I wanted to use if. However, when I deliberately changed the equal value to 300 just to check if this works, both tests are run and the variable is set, even though the first test fails.

The console.log returns an empty object instead of true/false for the assertion. And if I use something like

console.log(pm.expect(pm.response.code).to.equal(300));

I'll get an error message: There was an error in evaluating the test script: AssertionError: expected 200 to equal 300

Does anyone know how to solve this?

Cheers

Aucun commentaire:

Enregistrer un commentaire