Take a value from a dataset and never use it again

#1

I want to have a list of 100 items, and have one used in the payload of a request every time I run my collection, without the item ever being repeated.

So today I’ll run the collection and value A will be used. Tomorrow, value B, and so on.

Does anyone know how I’d go about this? Apologies if this is solved elsewhere :slight_smile:

#2

Hi @daniel.goddard,
The problem will be where to store your list of 100 items so that you can update it each time you run it. Unfortunately, because Postman runs in a sandbox you will not be able to just read and update a file.

  • You could setup some external API yourself (eg using node.js/express) with end points that handle the list, and then you could create an initial Postman request to fetch the item that you could save to globals and use as your payload. But you’d have to be comfortable with that and would probably be overkill.
  • You could use a node.js script to run Newman, and then you can read & update a file and craft a globals object containing the item you wanted to use each time. Again, you’d have to be comfortable with node.js and running your collection through Newman, also probably overkill
  • You could store the list as a global variable, and then each time pick the first item to use, delete it from the list, and update the list back to the globals. You would need to tick the ‘Keep variable values’ in the collection runner. This makes you dependent on always using the Collection Runner, and keeping your globals in good running order. (or you could do this with environments as opposed to globals)
  • Lastly, if you are running your collection exactly once a day, you could map date values to an index in the list, eg first run is on date x, thereafter work out the number of days since the first run, and use that value as the index into the list.

These answers are a bit high level (and vague), but the right one depends on your context, eg how you use Postman, how comfortable you are with other javascript technologies, where you need to see your results, and if/how you share your collection with others.

reply to let us know if any of the suggestions sound right for you, we can then look into more detailed implementations

#3

Thanks @VincentD123. I think at first glance option 2 may be the way forward, I’m happy to run my collection in Newman but I’d need to massively level up my knowledge of node.js to be able to read & update a file.

A key point is that this is all to check an endpoint returns the correct status code, but I have to have a different variable in the payload each time.

I could perhaps increase the dataset - would there be a way of getting a value from it at random? I suppose I could then write a test that ignores the specific status code that arises if I’ve had that payload previously.

#4

hi @daniel.goddard

I’d recommend just giving node.js a go, it’s surprising easy to install and to get going with. Just go to nodejs.org and install it. Do lots of googling of anything you’d like to try and do and just try a few code snippets. Get used to using npm to install new modules to try out (but do it from within your project folder) . If you like go through slides 9 to 11 of this workshop I ran, that will help you install and set up your first project.

To be able to access the file system, execute the following from within your project folder

npm install file-system --save

In postman create an environment with a variable ‘list’ with the value

[“value1”,“value2”,“value3”,“value4”,“value_etc”]

now export that to a file inside your node.js project folder

Then the following code should remove the first element from list (after you’ve used it in your collection)


const fs = require("fs"); // the filesystem module
const fileName = './env with list.postman_environment.json' // your env file from postman file saved in same directory as this script
var env = require(fileName)

console.log("environment file: ",env)

var listJSON = env.values.find(value => value.key === 'list')
try{ 
	var list = listJSON && listJSON.value && JSON.parse(listJSON.value)
}catch(e){} // ignore any json parsing errors 

if(list){
	console.log("Entire list: ",list)
	
	
	console.log("The first item of the list",list.shift()) 
	// Note: shift returns the first item, as well as removing from the list

	console.log("List - with first item removed",list)

	// replace the env variable with updated list
	env.values.find(value => value.key === 'list').value = JSON.stringify(list)
	
	// write the list back to the file
	fs.writeFile(fileName, JSON.stringify(env), (err) => {
		if (err) {
			console.error(err);
			return;
		};
		console.log("Env File has been updated");
	});
}

You can run this in a powershell script with

node path/scriptName.js

assuming you saved the code above into path/scriptName.js

or to debug it

node --inspect-brk path/scriptName.js

and then open chrome, go to Developer Tools (F12), and then click on the cube icon for Open dedicated DevTools for Node.js.
image
this will launch the chrome javascript debugger

From experience beginners would probably get this far within a couple of hours, but its possible you may hit some kind of obstacle, but you won’t be the first, so google it for a fix.

Goodluck @daniel.goddard, I was where you are a year ago, it’s easier than you might think.

2 Likes