postmanlabs / newman

Newman is a command-line collection runner for Postman
https://www.postman.com
Apache License 2.0
6.83k stars 1.16k forks source link

Cannot read properties of undefined (reading 'setNextRequest') #3253

Closed ajberry closed 4 weeks ago

ajberry commented 1 month ago

Using newman as a CLI on Windows. Newman v5.3.2 and Node v16.20.2

I have a basic collection that is iterating some posts using setNextRequest. The collection works just fine in the Postman Collection Runner, but not in the CLI environment. Eventually, this will be run as part of an Azure DevOps pipeline.

My Pre-Request code:

var CaptureTypes = pm.environment.get("CaptureTypes");

if (CaptureTypes.length == 0) {
    CaptureTypes = ["Type1", "Type2", "Type3", "Type4"];
    }

var currentCaptureType = CaptureTypes.shift();
pm.environment.set("CaptureType", currentCaptureType);
pm.environment.set("CaptureTypes", CaptureTypes);

My Post-Response code:

var CaptureTypes = pm.environment.get("CaptureTypes");

if (CaptureTypes && CaptureTypes.length > 0) {
    pm.execution.setNextRequest('CaptureTypes');
}
else {
    pm.execution.setNextRequest(null);
}

pm.test("Status code is 201", function () {
    pm.response.to.have.status(201);
});

The issue is that the first request is made successfully, but then it's failing when calling the setNextRequest('CaptureTypes').

This collection runs as expected in the collection runner, it's only when I export the collection/environment and it then doesn't work. Any ideas on what may be causing the issue?

The command i'm running is: newman run ".\LoopTest.postman_collection.json" -e ".\LoopTest.postman_environment.json"

and i'm using an environment file to store CaptureType/CaptureTypes etc and they are empty by default in the env files

I525970 commented 1 month ago

Hi

I have same problem here, did you fix this already?

BR, Mering

ajberry commented 4 weeks ago

I haven't been able to fix this as of yet

cchretien commented 4 weeks ago

You can still use postman.setNextRequest() in 5.x. Or you can upgrade to 6.x to use pm.execution.setNextRequest() .

ajberry commented 4 weeks ago

Yes, I missed I was running v5 of newman and that meant i'd need to use postman. instead. I've updated the tool to v6 and it's working as expected

lblnop commented 3 weeks ago

I am facing same problem in my DevOps CI/CD pipeline. My updated code has pm.execution.setNextRequest() but, getting error as cannot read properties of undefined 0 setNextRequest()

ajberry commented 3 weeks ago

@lblnop - What version of newman is your devops pipeline installing? Make sure it's getting the latest version of newman (and you'll need node greater than version 16)

A1811-qbite commented 3 weeks ago

Istory