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.