最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - If statement in Postman tests - Stack Overflow

programmeradmin0浏览0评论

I work in Postman and I want to build the test construction like that:

var login_status = pm.environment.get("LOGIN_STATUS");
var jsonData = pm.response.json();
pm.test("TESTS", function () {
    if (login_status === 1) {
        pm.expect...(test 1);
    }else if (login_status === 2) {
                pm.expect...(test 2);
    }else if (login_status === 3) {
                pm.expect...(test 3);

but it doesn't work for me as I want.

In my logic, Postman should check the login status and use only one pm.expect... accordingly to login_status value.

How can I use the if statement in Postman?

I work in Postman and I want to build the test construction like that:

var login_status = pm.environment.get("LOGIN_STATUS");
var jsonData = pm.response.json();
pm.test("TESTS", function () {
    if (login_status === 1) {
        pm.expect...(test 1);
    }else if (login_status === 2) {
                pm.expect...(test 2);
    }else if (login_status === 3) {
                pm.expect...(test 3);

but it doesn't work for me as I want.

In my logic, Postman should check the login status and use only one pm.expect... accordingly to login_status value.

How can I use the if statement in Postman?

Share Improve this question edited May 5, 2022 at 23:43 bad_coder 12.9k20 gold badges54 silver badges88 bronze badges asked Aug 8, 2018 at 10:03 Eugene TruutsEugene Truuts 3792 gold badges5 silver badges17 bronze badges 1
  • What is not working? Can you please provide more details? Does it throw error? – cdoshi Commented Aug 8, 2018 at 10:14
Add a ment  | 

1 Answer 1

Reset to default 2

The parison operator you are using is returning false as it's checking for the same type, try using == rather than ===.

The values stored in the Postman environment file is a string and you're doing a strict equal against a number, in the test.

You could also try changing the number value to a string value like this login_status === "1" or use strict inequality so the type you use doesn't matter, like this login_status !== 2.

You could even just use a parseInt() function when you get the value from the file.

parseInt(pm.environment.get("LOGIN_STATUS"))

It's pletely up to you and the way you want to construct the check.

More information about the parison operator can be found here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Identity

发布评论

评论列表(0)

  1. 暂无评论