7

I am new with Postman > Pre-request Script, Can you help me create a random email value for my API Request. Thank you in advance.

Timikko Santos
  • 103
  • 1
  • 1
  • 7

5 Answers5

4

Contrary to Pre-request Script, I would suggest using another efficient way to generate a random email: like qa{{$timestamp}}@gmail.com. Here, {{$timestamp}} will always give you a unique number that never occurred in the past.

enter image description here

Moreover, if you want to stick with the Pre-request Script, then here is the way to do so:

Set the random email in the environment so that you can use it in the request.

Now, access the environment variable in the request as:

You might face 'Unexpected token error in JSON, that can be fixed by putting environment variable in double-quotes.

  • 1
    Hi the pre-requisite code doesn't work. pm.environment.set('a',_.$randomEmail) , now try accessing a – PDHide Jan 08 '20 at 13:39
  • I add in Pre-request Script as: pm.environment.set("randomAge", parseInt(_.random(20, 80))); and in body request, get randomAge as: { "firstName": "{{$randomFirstName}}", "lastName": "{{$randomLastName}}", "age": {{randomAge}} } – Ghulam Abbas Jan 09 '20 at 09:36
  • And above script worked for me. – Ghulam Abbas Jan 09 '20 at 09:36
  • Hi Ghulam , the script you mentioned in comment is entirely different from the answer. The approach in the new script is just a normal operation. It is not using the dynamic variable of postman. My question was about your code of using dynamic variable in postman scripts. – PDHide Jan 09 '20 at 10:54
2

You can use below code in your pre-requist script

 const uuid = require('uuid')
 let email=uuid()+'@domain.com'
 pm.environment.set('email',email)

and access the same using

  1. {{email}} , in non script locations
  2. pm.environment.get('email') in test scripts

You can also use other unique identifier libraries given below, instead of uuid

 const moment = require('moment')
 let email=moment().valueOf()+'@domain.com'
 pm.environment.set('email',email)

Or

 let email=_.random(1000)+'@domain.com'
 pm.environment.set('email',email)
PDHide
  • 11,065
  • 2
  • 14
  • 42
2

I am currently on Postman v7.29.1. It appears that $randomEmail (one of many $random vars) is a built-in variable. So you are free to skip the pre-request script.

// req body - raw/json

{ "firstName": "{{$randomFirstName}}", "lastName": "{{$randomLastName}}", "email": "{{$randomEmail}}", "phone": "{{$randomPhoneNumber}}", }

Note: The double-quotations " around the curly braces {{ }} are important, as are the prefixed dollar signs $ in the variable names.

TIER 0011
  • 121
  • 1
  • 2
    Interesting. There's a reference to the official documentation: https://learning.postman.com/docs/writing-scripts/script-references/variables-list/#domains-emails-and-usernames – pavelsaman Jul 30 '20 at 20:14
1

You can simply generate a UUID and append it to a domain.

const uuid = Math.random().toString(36).substring(2, 15) +
        Math.random().toString(36).substring(2, 15);
const email = "user_" + uuid + "@myDomain.com";
pm.environment.set("currentEmail", email);

Reference: https://stackoverflow.com/a/13403498/2252076

Then, you can reference the {{currentEmail}} variable anywhere on the request or on the tests.

João Farias
  • 10,876
  • 2
  • 18
  • 39
1

use postman Dynamic variables as "{{$postmanDynamicVariables}}" in pre-request Script

Example

const email = "{{$randomEmail}}";

pm.environment.set("email", email);

// or to generate and set random full name

pm.environment.set("name","{{$randomFullName}}");

Jay Soni
  • 111
  • 1