javascript Postman 测试中的 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51743915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:40:57  来源:igfitidea点击:

If statement in Postman tests

javascripthtmljsonpostmanqa

提问by Gene Truuts

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

我在 Postman 工作,我想构建这样的测试结构:

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.

在我的逻辑中,邮递员应该检查登录状态并根据 login_status 值只使用一个 pm.expect...。

How can I use the if statement in Postman? Thank you!

如何在 Postman 中使用 if 语句?谢谢!

采纳答案by Danny Dainton

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

您正在使用的比较运算符正在返回,false因为它正在检查相同的type,请尝试使用==而不是===

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

存储在 Postman 环境文件中的值是 astring并且您number在测试中对 a 进行了严格等于。

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

您还可以尝试将number值更改为这样的stringlogin_status === "1"或使用严格的不等式,这样您使用的类型就无关紧要了,就像这样login_status !== 2

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

parseInt()当您从文件中获取值时,您甚至可以只使用一个函数。

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

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

这完全取决于您和您想要构建支票的方式。

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

有关比较运算符的更多信息,请访问:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Identity