-1
I am performing several registration tests via Postman and I would like to know if there is any way to automatically generate a CPF in the POST body that I am sending.
Who knows via Faker?
-1
I am performing several registration tests via Postman and I would like to know if there is any way to automatically generate a CPF in the POST body that I am sending.
Who knows via Faker?
4
For any kind of data you can set the variables in the Pre-request Script, whether it is a random value for CPF or for something else.
Use pm.globals
to define a global variable, example:
pm.globals.set("foo", "bar");
Use pm.collectionVariables
to set a variable for collection, example:
pm.collectionVariables.set("foo", "bar");
Use pm.environment
to set a variable for the Environment (in the currently selected environment, check in the menu the option Environment):
pm.environment.set("foo", "bar");
If you have view but not edit access to an Environment, your script code will only affect the current value and will not be synchronized or shared with your team.
For your case set a local variable, ie no Pre-request Script, which will be a temporary value should serve very well (unless it will create a process with a dynamically triggered step by step, but depends on the case and or need), example:
pm.variables.set("foo", "bar");
It’s not necessary Faker.js, this is only a facilitator, you can write any script anyway the time you think necessary, have an example in this question of how to generate a CPF: CPF generator in Javascript
Finally do the following, in your Pre-request Script add:
pm.variables.set("cpf", gerarCPF());
function gerarCPF() {
return ...;
}
it’s just one pseudocode to get an idea of how to implement, and then if your request is GET, do something like http://foo/bar?cpf={{cpf}}
, see the result in the http://postman-echo.com/get
:
If it is POST just pass the variable {{cpf}}
no body, see the result in the image of http://postman-echo.com/post
:
pm.sendRequest
No need to use specifically Faker.js and he is not exactly integrated, in fact "integrate" is something we do using pm.sendRequest
, so if you know of another library that has CPF generator, CNPJ or any other need, you can use the pm.sendRequest
added to a eval
(this runs in Sandbox) to inject the script:
pm.sendRequest("https://cdn.foobar/lib.js", (error, response) => {
if (error || response.code !== 200) {
pm.expect.fail('Could not load external library');
}
eval(response.text());
pm.variables.set("cpf", gerador.CPF());
});
Some libraries use the object window
to have access to lib, such as Pre-request Script wheel in a sandbox is necessary to create an object window
own:
window = {};
pm.sendRequest("https://cdn.barbaz/lib.js", (error, response) => {
if (error || response.code !== 200) {
pm.expect.fail('Could not load external library');
}
eval(response.text());
pm.variables.set("cpf", window.gerador.CPF());
});
Note that you can also use require(...)
(if the used library supports), which can make it much easier.
Taking advantage, you can use the pm.sendRequest()
to create step-by-step specific tests where you need a sequence of ordered requests to get a result, but this is a subject far beyond.
Documentation: https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#sending-requests-from-scripts
+1 for the good answer and explain the use of the parameters, I have been looking for this these days and I found nothing complete in a single answer :)
Browser other questions tagged api testing postman
You are not signed in. Login or sign up in order to post.
It even has the Faker integrated, but I think not with the option to generate CPF.
– Woss