A Condition block is a part of the test script where you check if something is true or false, like checking if a response status is 200. It helps decide what to do next based on the result.
Click to reveal answer
beginner
How do you write a simple condition block to check if the response status is 200 in Postman?
Use an if statement like:
if (pm.response.code === 200) { pm.test('Status is 200', () => { pm.expect(pm.response.code).to.equal(200); }); }
Click to reveal answer
beginner
Why are condition blocks useful in Postman tests?
They let you run tests only when certain things happen, like testing a feature only if the previous request succeeded. This saves time and makes tests smarter.
Click to reveal answer
beginner
What happens if the condition in a Postman condition block is false?
The code inside the block does not run. This means the test inside won’t execute, so you can control which tests run based on conditions.
Click to reveal answer
intermediate
Show an example of using an else block in Postman test scripts.
Example:
if (pm.response.code === 200) { pm.test('Success', () => { pm.expect(true).to.be.true; }); } else { pm.test('Failure', () => { pm.expect.fail('Response was not 200'); }); }
Click to reveal answer
What does a condition block in Postman test scripts do?
ARuns code only if a condition is true
BRuns code regardless of any condition
CDeletes the response data
DSends a new request automatically
✗ Incorrect
A condition block runs the code inside it only when the condition is true, helping control test flow.
Which JavaScript keyword is used to start a condition block in Postman tests?
Aif
Bfor
Cwhile
Dswitch
✗ Incorrect
The 'if' keyword starts a condition block to check if a condition is true.
What will happen if the condition in an if block is false in Postman test scripts?
AThe test always passes
BThe code inside the block runs
CPostman crashes
DThe code inside the block does not run
✗ Incorrect
If the condition is false, the code inside the if block does not run.
How can you test for a response status of 200 using a condition block in Postman?
Apm.test('Status is 200')
Bif (pm.response.code === 200) { pm.test(...) }
Cpm.response.code = 200
Dpm.expect(200).to.equal(pm.response.code)
✗ Incorrect
Using if (pm.response.code === 200) lets you run tests only when the status is 200.
What is the purpose of an else block in Postman test scripts?
ATo stop the test execution
BTo repeat the if block
CTo run code when the if condition is false
DTo send another request
✗ Incorrect
An else block runs code when the if condition is false, allowing alternative actions.
Explain how a condition block works in Postman test scripts and why it is useful.
Think about checking response status before running tests.
You got /4 concepts.
Write a simple Postman test script using a condition block to check if the response status is 200 and explain each part.
Start with if, then pm.test inside, then pm.expect to check status.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of a condition block in Postman tests?
easy
A. To send multiple requests at once
B. To run code only when a specific condition is true
C. To store variables permanently
D. To format the response body
Solution
Step 1: Understand the role of condition blocks
Condition blocks like if let you run code only when certain conditions are met.
Step 2: Compare with other options
Sending requests, storing variables, or formatting responses are different features, not condition blocks.
Final Answer:
To run code only when a specific condition is true -> Option B
Quick Check:
Condition block = run code if true [OK]
Hint: Condition blocks run code only if conditions are true [OK]
Common Mistakes:
Confusing condition blocks with request sending
Thinking condition blocks store variables
Assuming condition blocks format responses
2. Which of the following is the correct syntax for a condition block in Postman test scripts?
easy
A. check (pm.response.code === 200) { console.log('Success'); }
B. when (pm.response.code == 200) { console.log('Success'); }
C. if pm.response.code = 200 then { console.log('Success'); }
D. if (pm.response.code === 200) { console.log('Success'); }
Postman test scripts use JavaScript, so if (condition) { } is correct syntax.
Step 2: Check each option
if (pm.response.code === 200) { console.log('Success'); } uses correct if with triple equals and braces. Others use invalid keywords or assignment instead of comparison.
Final Answer:
if (pm.response.code === 200) { console.log('Success'); } -> Option D
Quick Check:
Use if (condition) { } in Postman [OK]
Hint: Use JavaScript if (condition) { } syntax [OK]
Step 1: Analyze the condition with response code 200
The if checks if code is 404; 200 is not 404, so else block runs.
Step 2: Identify which test runs in else block
The else block runs a test named 'Other status' that expects code not to be 404, which is true for 200.
Final Answer:
'Other status' test runs and passes -> Option C
Quick Check:
Response 200 triggers else block test [OK]
Hint: If condition false, else block runs test [OK]
Common Mistakes:
Assuming 'Not Found' test runs for 200
Thinking both tests run simultaneously
Believing no test runs if condition false
4. Identify the error in this Postman test script condition block:
if pm.response.code == 200 {
pm.test('Status is 200', () => {
pm.expect(pm.response.code).to.equal(200);
});
}
medium
A. Missing parentheses around the condition
B. Using double equals instead of triple equals
C. Missing semicolon after pm.test
D. Incorrect function arrow syntax
Solution
Step 1: Check syntax of the if statement
JavaScript requires parentheses around the condition in if, but here they are missing.
Step 2: Verify other syntax elements
Double equals is allowed in JS, semicolon after pm.test is optional, and arrow function syntax is correct.
Final Answer:
Missing parentheses around the condition -> Option A
Quick Check:
Always use parentheses in if (condition) [OK]
Hint: Always wrap condition in parentheses in if statements [OK]
Common Mistakes:
Omitting parentheses around condition
Confusing == and === as syntax errors
Expecting semicolons after function calls
5. You want to write a Postman test that checks if the response JSON has a status field equal to 'success'. If true, test should pass with message 'Success status'. Otherwise, it should fail with message 'Status not success'. Which condition block correctly implements this?
hard
A. if (pm.response.json().status === 'success') {
pm.test('Success status', () => {
pm.expect(true).to.be.true;
});
} else {
pm.test('Status not success', () => {
pm.expect.fail('Status not success');
});
}
B. if pm.response.json().status = 'success' {
pm.test('Success status', () => {
pm.expect(true).to.be.true;
});
} else {
pm.test('Status not success', () => {
pm.expect.fail('Status not success');
});
}
C. if (pm.response.json().status == 'success') {
pm.test('Success status', () => {
pm.expect(false).to.be.true;
});
} else {
pm.test('Status not success', () => {
pm.expect.fail('Status not success');
});
}
D. if (pm.response.json().status === 'success') {
pm.test('Success status', () => {
pm.expect.fail('Status not success');
});
} else {
pm.test('Status not success', () => {
pm.expect(true).to.be.true;
});
}
Solution
Step 1: Check condition syntax and comparison
Correct syntax requires if (pm.response.json().status === 'success') with parentheses, strict equality ===, and proper JSON access. Other options lack parens, use assignment, wrong expectations, or swapped logic.
Step 2: Verify test pass/fail logic
If true, pm.test('Success status', () => { pm.expect(true).to.be.true; }) passes. Else, pm.test('Status not success', () => { pm.expect.fail('Status not success'); }) fails with message.
Final Answer:
if (pm.response.json().status === 'success') {
pm.test('Success status', () => {
pm.expect(true).to.be.true;
});
} else {
pm.test('Status not success', () => {
pm.expect.fail('Status not success');
});
} -> Option A
Quick Check:
Use strict equality and fail test in else block [OK]
Hint: Use if (condition) { pass } else { fail } pattern [OK]
Common Mistakes:
Using single equals '=' instead of '==='
Failing test inside success block
Passing test inside failure block
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.