Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman: Is it possible to stop a postman call from being executed based on conditions detected in pre-requisite scripts?

Tags:

postman

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.

like image 974
overflower Avatar asked Jun 22 '17 06:06

overflower


People also ask

How do you stop a Postman execution?

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.

What is the use of pre-request script in Postman?

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.

Does Postman support scripting?

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.

How do you write if condition in Postman?

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... }


2 Answers

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.

like image 133
overflower Avatar answered Sep 20 '22 09:09

overflower


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!')
}
like image 36
Filomat Avatar answered Sep 19 '22 09:09

Filomat