Hi,
I want to write some custom script, to provide some date for my APIs globally.
My goal is to generate a random string, like a Guid, and at testing use it as a parameter for APIs.
I tired with global variables + custom script but didin’t work, can’t get any string from the script.
And at pre processor, where after i use it was saved as an enviroment variable, which would be good for me, but when i try to use at different APIs, it gave me the same string, but i want to generate different string(id) in every APIs before the request.
Is there any way to do that? What i’m missing?
var randomNumber = Math.floor(Math.random() * (2000 - 1000 )) + 1000 ;
pm.environment.set(“RandomPriceNumber”, randomNumber );
Can you describe your usage in detail? Please explain how you use global variables and custom scripts. If you are unable to retrieve any strings from the script, please ensure that the global variable is assigned a value.
Please also describe how you use them in different APIs. If you are getting the same string when using it in different APIs, make sure to unset the environment variable after the previous API is used.
It would be helpful if you could provide detailed steps and screenshots.
Sorry for the late reply.
So i need some unique string values for the APIs. As i saw, i can go with JS script. Like I can generate a GUID for an ID. My thought is, that i make a global varaible, which generate a GUID every time i use it, before the API run. And if i use on more thread, it will give me different GUIDs. Like simulating parallel buying orders.
So, how can I make unique string as global variables? How can I unset environment variable?
ps: i tried several times, and i didn’t change anything between, sometimes it works, sometimes it doesn’t.
1.Global variables can add a variable named GUID with a string value in the environment.
2.You can also set environment variables to add a variable GUID with a string value in a specific environment, so that the same value will be used each time.
3.To get or unset environment variables, you can implement it through scripts. Please refer to the help document: Postman scripts reference - Apidog Docs
4.Getting global variables can also be achieved through scripts. Please refer to the help document: Using variables in scripts - Apidog Docs
Thank you for your response.
My purpose is to use different strings(ex. GUIDs). And also, my scrpit is working(now) for GUID but still some test cases randomly fails. See in the pictures above. Do you have any idea?
So, to sum up whats not working:
on the testing site i have 2 folders: 1. the default and 1 with flows. I managed to use my script in the default folder, working fine, with multiple thread, and generate multiple ids. But using the same environment variable, same data in the other folder, it just give me 1 ID even if I use multiple threads. Also in the first folder the request is successful, can see it in database, but on the other one, even I get the 200 response code, with the same IDs, its now working. Do you have any idea?
Perhaps you need to provide a screenshot of the details in the failure report within this report. Seeing exactly what errors were reported will allow for a better understanding of what specifically happened.
Sometimes the Retrieve Pre-step Data can’t find the data. I have to use other APIs request or response body to get the same data, with the easiest jshon path.
And also when I imported the test cases, which worked fine, in different Test Scenario half of them failed. Same API, which worked fine now it just stop working (second picture). Tests passed with 400 error. Everywhere the problem with the data, just cant get from the previous APIs. It just not working. Is that some bug or what I’m missing?
It looks like the error is caused by the pre-step variables not being replaced with actual values. Possible reasons might be that the written request JSON format is incorrect, or there’s an issue with the syntax of the pre-step variables.
I noticed in the screenshot you provided that the “cartkey” parameter uses a query parameter, but the syntax used for the pre-step variable in the “updateCartItem” step is written as request.body?
Please check if there’s an issue here, or you could also provide me with screenshots of the request parameters for the second and fourth steps.
It dosen’t matter if I use body or query, or $. or just cartkey in JSON path, and even if its syntax error, why on the first test cases working fine, importing to an other giving error?
Also, some test pass some fails even with 500 error.
I’m lost
A: The first test case
B: Another test case
1.Please check the actual differences in requests between request A and request B after running.
2.If there is no content extracted based on the step id, please check if the corresponding request or response of the associated step id contains response fields.