I am trying to run my postman collection from my CLI using Newman, but I keep getting this error TypeError in prerequest-script. Kindly put me through on what I am not doing correctly. Below is my pre-request script:
var seed = Math.floor(Math.random() * 899999999999 + 100000000000);
var d = new Date();
var sessionID = '{{source_bank_code}}' + d.getFullYear().toString().substr(-2)+("0"+(d.getMonth()+1)).slice(-2)+("0" + d.getDate()).slice(-2)+("0" + d.getHours()).slice(-2) + ("0" + d.getMinutes()).slice(-2)+("0" + d.getSeconds()).slice(-2) + seed;
var beneficiary_account_number = "2345678907";
pm.collectionVariables.set("session_id", sessionID);
pm.collectionVariables.set("beneficiary_account_number", beneficiary_account_number);
pm.collectionVariables.set("name_enquiry_ref", sessionID);
At the end of the test, the failure detail says, 01. TypeError: Cannot read property 'set' of undefined at prerequest-script inside "Source Bank Agent / Transfers / Account Enquiry / Get Account Details (Valid)"
pm.collectionVariables.set is a newly introduced API and you need to update Newman to the latest version. Otherwise, it will not work.
This is causing the error âTypeError: Cannot read property âsetâ of undefinedâ. pm.collectionVariables is undefined as it does not exist yet in your version of Newman.
Just using double curly braces in a string (like {{source_bank_code}}) wonât motivate Postman to replace them for you.
You can use the pm.* API to fetch the respective value.
The version of newman that I run on is 4.5.4 which is pretty recent. Nevertheless, Iâll try to update it to the latest version (v4.5.6) and see if it would handle it differently.
As regard the use of the double curly bracket to place variables in the script, it works just fine with postman, so I assumed newman shouldnât have an issue with it. But also, I will do well to explore that option of fetching the value first and placing it in a variable local to the script.
Thanks all @amit for the reply. After updating my newman to the latest version, it worked without any issue.
I was only able to use the following command to update my newman:
Do you managed to fix this error? I am getting similar error as well. Collection running through postman runner without any error, but in newman CLI getting error as Type error in script. I am using newman 5.3.0
I have no contextual information here, just 2 screenshots.
Your error might be similar but your context is different. I donât know what your workflow is here or what scripts you have in place that could be storing variables.
I donât know what Newman command youâre using or if youâre using any environment files with that command. I donât know if you have exported an environment file which might not contain values if you only have them in the Current Value.
You need to fully explain everything in your own context so that people will have the best chance to help you.
Also, as this old post already had a solution, which you would have been told about that when you replied, I would recommend opening up a new topic when asking for help. It saves a lot of back and forth questions trying to clarify information.
Hi @danny-dainton, Apologies for the late response so i am adding all my steps in detail now.
Step 1: User login to the mobile app and I am saving the general details and access token of that user in the form of collection variable. BTW I also tried with Global or environment variable.
@michaelderekjones ,
My login api is working fine and the collection was working as expected when i execute it on postman runner but its not working on newman.
As I mentioned in my last post. You need to check the status of the 1st request when its run in Newman.
Console log anything important, and you should also be able to see the console log results in Newman as well.
Try these troubleshooting steps first.
If its running ok in the runner, but not Newman, then its usually due to variables. So you need to check to logs to see which ones arenât being set correctly, and work from there.
As @danny-dainton mentioned. This topic has been solved and ideally this should be a new topic. Your Newman screenshot needs to show the login request (which is where I suspect the problem is). You need to troubleshooting by using the console log.