how i can add multiple values in one variable in postman? - postman

i need to run only one request with 4 sets of data, rest all requests i want to run one time so iteration is not an option for me. the request is get and i want to pass multiple values in one of the parameter of get request. want to run script for those many times like if 4 values in variable script should run 4 times. How i can add 4 values in one global parameter? secondly how to later use that parameter to run that many times?
pm.globals.set('my_var', JSON.stringify('["iphone", "ipad"]'))
for ( i=1; i<= 4; i++)
{
console.log (JSON.parse(pm.globals.get('my_var'))[0].value);
}
This is returning me [ as value as [ is present at that point but i want to make it read whole value which is iphone. Please guide

Related

Postman - how to target the final iteration?

I would like to run a Postman test on just the final iteration of a test run - I am building a variable (array) of response time values across all of the iterations and then want to test this variable for extreme values once we've reached the last / final iteration.
I hoped pm.info.iteration would have my answer but didn't see anything relevant.
I'm using a data file - the test runner highlights how many iterations are applicable (rows in the csv) as soon as the file is chosen so I'm guessing that Postman does know 'final' iteration? I just haven't worked out how to get at it.
My workaround is to hard code the number of iterations per test run based on how many rows my csvs currently have (e.g. if(pm.iteration.info === 70) but not ideal as the data file is likely to grow.
As #DannyDainton mentioned you can use iterationCOunt
iteration index starts from 0 ,so use
(pm.info.iteration === (pm.info.iterationCount-1) )

Reuse data across iterations

For testing a POST request to an API, I'm using postman. What I'm trying to achieve is that every request I do selects some data from a big file (randomly) and uses this data to populate the body of the request.
What I would like is that I can select for example 10 iterations and have every one of this iterations pick some random data out of the file I would provide.
I have this working for 1 iteration.
The problem is that in Postman I can't find a way to use the data for the first iteration for all iterations.
A workaround could be to copy-paste the data for every iteration I would like to do, but since this will be a rather large dataset I would like to avoid this.
In short, I'm looking for a way to provide a file to the postman runner and use the data in that file for every iteration I would run.
You can try and call your request in a while function with pm.setNextRequest('nameOfYourRequest');
function reccurrentCall(iterations){
while(iterations !== 0){
postman.SetNextRequest('nameOfYourRequest');
iterations--;
};
And call the function with number of Iterations you want to make
recurrentCall(5);

How to increase Variable value based on the iteration being run in Postman

I have an API request that I need to run in Postman-Collection-Runner thru multiple iterations. The API request uses Variable.
How can I make this variable to automatically increase with each iteration (or maybe set the iteration value as another Variable)?
If I understand your question correctly, you would like to assign different values to a variable in the request in different iterations which is achievable in 2 ways.
a) Using data files
https://learning.getpostman.com/docs/postman/collection_runs/working_with_data_files/
The data files could be in JSON or CSV format. Unfortunately, there is no way in Postman to tie the variable values to another variable unless you want to do it in a hacky way!
b) Pre-request & Tests scripts
1- Initialise the environment variable in the Pre-request Scripts like this:
var value = pm.environment.get("var");
if( !value) {
pm.environment.set("var", 1);
}
2- Increment the variable value in Tests
var value = pm.environment.get("var");
pm.environment.set("var", value+1);
This creates an environment variable and increments it after each iteration. depending on how you structure your collection you might need to consider flushing/resetting the environment variable to be ready for the next run
It worth mentioning that Pre-request Scripts and Tests running before and after the requests respectively, so you can write any scripts that would like to run after the request in the Tests. It shouldn't be necessarily a test!
1. Using Global pm.* functions and Variables in Pre-Request Scripts/Tests
Pre-Request script - runs before executing the request
Tests - runs after executing the request
a.
pm.variables.set("id", pm.info.iteration);
Ex: example.com/{{id}}/update gives
example.com/0/update
example.com/1/update etc...
Number of iterations is set in Collection Runner. pm.info.iteration key has the current iteration number, starting at 0.
b.
var id = +pm.globals.get("id");
pm.globals.set("id", ++id);
The variables can be in any scope - globals/collection/environment/local/data.
In Collection Runner, check the Keep Variable Values checkbox, to persist the final value of the variable in the session (here id).
Note: If the variable is accessed via individual scopes (via pm.globals.* or pm.environment.* or pm.collectionVariables.*), then the mentioned checkbox should be toggled as required. Else if accessed via local scope (pm.variables.*), the value will not be persisted irrespective of the checkbox.
Ex: Same as above
More on variables and scoping
2. Using Dynamic variables
These variables can be used in case random values are needed or no specific order is necessary.
a. $randomInt - gives a random Integer within 1 - 1000.
Ex: example.com/{{$randomInt}}/update gives
example.com/789/update,
example.com/265/update etc...
b. $timestamp - gives current UNIX timestamp in seconds.
Ex: example.com/{{$timestamp}}/update gives
example.com/1587489427/update
example.com/1587489434/update etc...
More on Dynamic variables
Using Postman 7.22.1, while answering this. New methods may come in future.

Passing variable from one iMacros to another?

I want to create a script, where the users inputs in the beginning something, and this variable input should be used for the rest of the loop (without asking for input again).
The best way i could come up with was to create two iMacros, one which asks for the input, and the other one who uses the input and loops it.
The major problem is, i can't save the input in Downloads/Datasource because the same iMacros should be used with other firefox instances. Which means, if the user inputs A on the first instance and on the next B then after the second loop of both all instances are using the varaible B. And i can't make multiple scripts since the user should have the ability to use unlimited times this script.
You can save easy the input in ‘Downloads/Datasource’. However make your second macro read this input only once:
SET !LOOP 1
SET !DATASOURCE inputs.csv
SET input EVAL("('{{!LOOP}}' == 1) ? '{{!COL1}}' : '{{input}}';")
' your further code here '

SOAPUI - changing request element value

I read some articles about SOAPUI, one of them is this SoapUI getting request parameters in mock service script and I think the solution I am looking for is something using Groovy.
I have a SOAP Web Service that I want to run some testes with a dynamically changing request. This request...
<soapenv:Body>
<req:MyrRquest>
<req:number>XPTO</req:number>
</req:MyrRquest>
</soapenv:Body>
My idea is to run a loop from a starting value increasing 1 until I reach my maximum. And I would like to replace XPTO with this changing value.
Did anyone ever attempted this? What is the best way to do that?
Here is the way it can be done, by the use of groovy step.
Define a test case with two test steps:
Test Request step(soap, the one you shown)
Groovy Script Step(this is the additional one which I am proposing)
Define below three test case level custom properties like what you needed min and max times it should be repeatedly executed and provide values as per the test and keep CURRENT_VALUE same as MIN_VALUE which is one time job. Because, CURRENT_VALUE that gets incremented each time and do not want to alter MIN_VALUE each time the test runs. That way, do not have reset the value after each time test case is executed.
MIN_VALUE
MAX_VALUE
CURRENT_VALUE
Note that this cannot run individual steps i.e., the test case has to be executed in order to fulfill your need as it has to repeat the number of times, and hope that is ok for you.
In the test request, need to use the current value place holder.
Change: <req:number>XPTO</req:number>
To : <req:number>${#TestCase#CURRENT_VALUE}</req:number>
Here is the groovy script code:
//Read the test case level properties as integers
def min = context.testCase.getPropertyValue('MIN_VALUE') as Integer
def max = context.testCase.getPropertyValue('MAX_VALUE') as Integer
//Get the previous step name
def pStepName = context.testCase.testStepList[context.currentStepIndex-1].name
//min+1, because already test request is executed once
((min+1)..max).each {
//update the current value incremented by 1
context.testCase.setPropertyValue('CURRENT_VALUE', it.toString())
log.info "Running step ${pStepName} for ${it} time"
//run the previous test step
testRunner.runTestStepByName(pStepName)
}
//finally resetting current value to min value as test finishes
context.testCase.setPropertyValue('CURRENT_VALUE', min.toString())
This groovy script step basically takes care of running the first step for n-1 times, because step 1 already executed before groovy script test step where n is the total number of times required to be executed(n = max - min).
And as mentioned earlier, just run the test case.