I am using the pre-request script in the first call to dynamically generate essential environment variables for the entire script. I also want the users to be notified of those failures when running via collection runner without having to look up to the console. Is it possible to generate information in tests or some other alternative so failures are explicit in the collection runner results?
e.g. if the ip has not been provided in the environment, it does not make sense to run the login call. So i would like to write in a pre-requisite script:
if (!environment['IP']) {
//do not execute any further and do not send the REST call
}
I tried using:
if (!environment["xyz"]) {
tests["condtion1"]=false
}
but it gives the error:
There was an error in evaluating pre-requisite script: tests is not defined
Is there any workaround - I don't want to move this code to the tests tab as I don't want to clutter the code there with unrelated environment conditioning.
Stopping a workflow To stop a workflow, add the following code on the Tests tab of a request. postman. setNextRequest(null); The collection run will stop after Postman completes the current request.
You can use pre-request scripts in Postman to execute JavaScript before a request runs. By including code in the Pre-request Script tab for a request, collection, or folder, you can carry out pre-processing such as setting variable values, parameters, headers, and body data.
Scripts in Postman You can add JavaScript code to execute during two events in the flow: Before a request is sent to the server, as a pre-request script under the Pre-request Script tab. After a response is received, as a test script under the Tests tab.
You could make use of pm.environment.name here to check the active environment name, rather that a value within it: if(pm.environment.name === 'local') { // do something... } else if (pm.environment.name === 'develop') { // do something... } else { // do not do anything... }
A throw works just fine. (Updated with excellent tip from @Joe White)
if (!environment['X']) {
throw new Error('No "X" set')
}
This prevents the REST call from going through. But in the collection runner mode it stops the entire test suite.
But when coupled with newman collection runner it works just fine.
A throw error works fine with this test:
var value = pm.environment.get('X')
if (value == undefined || value == null || value.length == 0) {
throw new Error('No "X" set!')
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With