0
0
Postmantesting~20 mins

Condition block in Postman - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Condition Block Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Postman test script?
Consider this Postman test script that checks the response status code and sets a variable accordingly. What will be the value of the variable testResult after running this script if the response status code is 404?
Postman
if (pm.response.code === 200) {
    pm.variables.set('testResult', 'Success');
} else if (pm.response.code === 404) {
    pm.variables.set('testResult', 'Not Found');
} else {
    pm.variables.set('testResult', 'Other Error');
}
A"Not Found"
Bundefined
C"Other Error"
D"Success"
Attempts:
2 left
💡 Hint
Check the condition that matches the response code 404.
assertion
intermediate
2:00remaining
Which assertion correctly tests if the response JSON has a key 'status' with value 'ok'?
You want to write a Postman test that asserts the JSON response has a key status equal to ok. Which option is the correct assertion?
Apm.test('Status is ok', () => { pm.expect(pm.response.json().status).to.be.undefined; });
Bpm.test('Status is ok', () => { pm.expect(pm.response.json().status).to.eql('ok'); });
Cpm.test('Status is ok', () => { pm.expect(pm.response.json().status).to.be.true; });
Dpm.test('Status is ok', () => { pm.expect(pm.response.json().status).to.be.null; });
Attempts:
2 left
💡 Hint
Check which assertion compares the value exactly to 'ok'.
🔧 Debug
advanced
2:00remaining
Why does this Postman test script fail to set the variable correctly?
This script is intended to set the variable result to 'Passed' if the response code is 201, but it never sets it. What is the error?
Postman
if (pm.response.code = 201) {
    pm.variables.set('result', 'Passed');
} else {
    pm.variables.set('result', 'Failed');
}
AThe single '=' is an assignment, should be '===' for comparison.
BThe variable 'result' is not declared before setting.
Cpm.variables.set cannot be used inside if blocks.
DThe response code should be accessed with pm.response.statusCode.
Attempts:
2 left
💡 Hint
Check the operator used in the if condition.
framework
advanced
2:00remaining
Which Postman test script correctly uses a condition block to check multiple response codes?
You want to write a Postman test that passes if the response code is 200 or 201, and fails otherwise. Which script correctly implements this?
Apm.test('Status is 200 or 201', () => { if (pm.response.code === 200 || pm.response.code === 201) { pm.expect(true).to.be.true; } else { pm.expect.fail('Unexpected status code'); } });
Bpm.test('Status is 200 or 201', () => { pm.expect(pm.response.code === 200 || 201).to.be.true; });
Cpm.test('Status is 200 or 201', () => { if (pm.response.code === 200 && pm.response.code === 201) { pm.expect(true).to.be.true; } else { pm.expect.fail('Unexpected status code'); } });
Dpm.test('Status is 200 or 201', () => { pm.expect(pm.response.code).to.be.oneOf([200, 201]); });
Attempts:
2 left
💡 Hint
Check how to test if a value is one of multiple options.
🧠 Conceptual
expert
2:00remaining
What is the main advantage of using condition blocks in Postman test scripts?
Why do testers use condition blocks (like if-else) in Postman test scripts when validating API responses?
ATo speed up the API response time by skipping some requests.
BTo automatically generate API documentation from test scripts.
CTo execute different test assertions based on varying response data or status codes.
DTo encrypt sensitive data in the response before testing.
Attempts:
2 left
💡 Hint
Think about how tests adapt to different responses.