Custom documentation templates in Postman - Build an Automation Script
const newman = require('newman'); newman.run({ collection: './MyCollection.json', environment: './MyEnvironment.json', reporters: ['cli'], reporter: { cli: { noFailures: true } } }, function (err, summary) { if (err) { throw err; } // Check that all requests passed const failures = summary.run.failures; if (failures.length > 0) { console.error('Test failures:', failures); process.exit(1); } // Simulate fetching documentation page (assuming hosted locally or via API) const fetch = require('node-fetch'); const documentationUrl = 'http://localhost:3000/docs/MyCollection'; fetch(documentationUrl) .then(res => { if (res.status !== 200) { throw new Error(`Expected status 200 but got ${res.status}`); } return res.text(); }) .then(html => { if (!html.includes('<header class="custom-header">')) { throw new Error('Custom header not found in documentation'); } if (!html.includes('GET /users')) { throw new Error('Request details not found in documentation'); } console.log('Custom documentation template verified successfully'); }) .catch(err => { console.error('Error verifying documentation:', err.message); process.exit(1); }); });
This script uses Newman, the Postman CLI tool, to run the collection tests first to ensure the API requests work as expected.
After the collection run, it fetches the documentation page URL where the custom template is applied.
It asserts the HTTP status code is 200 to confirm the page loads successfully.
Then it checks the HTML content for the presence of custom template elements like a custom header and the expected request details.
If any assertion fails, the script throws an error and exits with failure.
This approach ensures both the API and the custom documentation template are verified automatically.
Now add data-driven testing with 3 different collections each having different custom documentation templates
Practice
Solution
Step 1: Understand the role of documentation templates
Custom documentation templates let you change how API docs look and feel, making them clearer and tailored.Step 2: Differentiate from other Postman features
Test scripts, automation, and mock servers serve different purposes unrelated to documentation appearance.Final Answer:
To control the appearance and structure of API documentation -> Option AQuick Check:
Custom templates = control doc look [OK]
- Confusing templates with test scripts
- Thinking templates automate requests
- Mixing templates with mock server setup
Solution
Step 1: Identify the templating syntax used by Postman
Postman uses Handlebars syntax, which uses double curly braces like{{variable}}to insert dynamic content.Step 2: Compare with other common syntaxes
Other options like [[ ]], %% %% or ## ## are not used in Postman templates.Final Answer:
{{variable}} -> Option BQuick Check:
Handlebars = {{variable}} [OK]
- Using square brackets instead of curly braces
- Confusing with environment variable syntax
- Using percent signs or hashes incorrectly
{{#each requests}}
- {{name}}
{{/each}}What will this code output?
Solution
Step 1: Understand the Handlebars loop syntax
The{{#each requests}}block loops over all requests in the collection, outputting each request'sname.Step 2: Determine the output of the loop
Each iteration outputs a dash and the request name, creating a list of request names.Final Answer:
A list of request names from the collection -> Option CQuick Check:
Loop over requests outputs names [OK]
- Thinking loops are not supported
- Assuming {{name}} is undefined
- Expecting repeated static text instead of dynamic names
{{#each requests}}
- {{name}
{{/each}}Why does the documentation fail to render correctly?
Solution
Step 1: Check the variable syntax inside the loop
The variable{{name}is missing a closing curly brace, it should be{{name}}.Step 2: Verify loop syntax and tags
The loop uses correct{{#each requests}}and closing{{/each}}tags, so those are not the issue.Final Answer:
Missing closing curly brace in {{name}} -> Option DQuick Check:
All must have matching braces [OK]
- Using wrong loop syntax like #for
- Assuming variable 'name' is missing
- Forgetting to close the loop tag
Solution
Step 1: Use the correct conditional block for existence
Handlebars uses{{#if variable}}to check if a variable exists and is truthy.Step 2: Verify the loop and conditional structure
{{#each requests}} - {{name}} {{#if description}} Description: {{description}} {{/if}} {{/each}}correctly loops over requests, shows name, and conditionally shows description only if it exists.Final Answer:
{{#each requests}} - {{name}} {{#if description}} Description: {{description}} {{/if}} {{/each}} -> Option AQuick Check:
Use {{#if}} to check existence [OK]
- Using {{#unless}} which shows content when variable is missing
- Trying to negate with ! inside {{#if}} which is invalid
- Using non-existent helpers like {{#exists}}
Ready to prove it?
Pass the test and this pattern moves unknown
