Skip to content

How To Use If Else Condition In Postman

API Testing Blog

Using If-Else Conditions in Postman for Powerful API Testing

Postman is a widely used tool for API testing, and its scripting capabilities allow you to create dynamic and efficient tests. One of the key features of Postman’s scripting functionality is the ability to use if-else conditions, enabling you to create tests that can adapt to different responses and scenarios. Let’s explore how you can effectively leverage if-else conditions in your Postman tests.

Understanding the Basics: If-Else Conditions in Postman

At its core, the if-else condition in Postman works like it does in most programming languages. It allows you to execute different blocks of code based on the evaluation of a specific condition.

Structure:

if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}

Key Components:

  • Condition: An expression that evaluates to either true or false.
  • True Block: Code executed when the condition is true.
  • False Block (Optional): Code executed when the condition is false.

Practical Example: Validating Status Codes

Imagine you have a POST request that should return a 201 Created status code on success. Let’s craft a test that verifies this:

  1. Create a Postman Request: Set up your POST request in Postman.
  2. Add a Test Script: In the “Tests” tab of your request, write the following code:
pm.test("Status code is 201", function () {
if (pm.response.code === 201) {
pm.expect(pm.response.code).to.equal(201);
} else {
pm.expect.fail("Expected 201 Created, but got " + pm.response.code);
}
});

Explanation:

  • We use pm.response.code to retrieve the status code of the response.
  • The if condition checks if the status code is 201.
  • If true, we use pm.expect to assert that the code is indeed 201.
  • If false, we use pm.expect.fail to report the error including the actual status code.

Chaining Multiple Conditions: Using ‘Else If’

Sometimes, you might have more than two possible outcomes. Here’s how to use the ‘else if’ statement:

Example: Validating Response Body Based on Status Code:

pm.test("Validate response body based on status code", function () {
if (pm.response.code === 200) {
pm.expect(pm.response.json().message).to.equal("Success");
} else if (pm.response.code === 400) {
pm.expect(pm.response.json().error).to.equal("Invalid input");
} else {
pm.expect.fail("Unexpected status code: " + pm.response.code);
}
});

Explanation:

  • We check the status code and then execute different tests depending on the value.
  • In the 200 case, we verify the message in the body is “Success”.
  • In the 400 case, we validate the error message.
  • Any other status code triggers an error.

Building Complex Logic Using Nested If-Else Statements

For more intricate scenarios, you can nest if-else conditions within each other:

Example: Validating User Permissions:

pm.test("Validate user permissions and roles", function () {
if (pm.response.json().role === "admin") {
if (pm.response.json().permissions.includes("read")) {
pm.expect(pm.response.json().user.name).to.be.a('string');
} else {
pm.expect.fail("Admin user should have read permission.");
}
} else if (pm.response.json().role === "user") {
if (pm.response.json().permissions.includes("write")) {
pm.expect.fail("User role should not have write permission.");
}
} else {
pm.expect.fail("Unexpected role: " + pm.response.json().role);
}
});

Explanation:

  • This example nests if-else conditions to verify different scenarios based on user roles and permissions.
  • The outer level checks for “admin” or “user”.
  • The inner level checks for specific permissions based on the role.

Writing Postman Tests Using Ternary Operator

The Ternary Operator provides a concise way to write if-else logic:

Example: Dynamically Setting Variables

pm.test("Dynamically setting variables", function () {
const isSuccessful = pm.response.code === 200 ? true : false;
pm.expect(isSuccessful).to.be.true;
});

Explanation:

  • The ternary operator (condition) ? (value if true) : (value if false) simplifies the code.
  • We use it to set the isSuccessful variable based on the result of the condition.

Conclusion

Mastering if-else conditions in Postman empowers you to write sophisticated and dynamic API tests. By leveraging these conditions, you can create tests that:

  • Handle different responses: Validate different status codes or body content.
  • Adapt to user roles and permissions: Verify specific functionalities based on user identities.
  • Build complex test scenarios: Combine if-else statements to cover numerous possibilities.

Use this guide as a springboard to experiment and create robust, adaptable API tests using the power of if-else conditions in Postman.

API Testing Blog