1

I am struggling here to generate an email ID that consists of Random First name and Random Last name. I don't want to use the dynamic Variable $randomEmail because it generates any random email which is totally different from the First name and Last name and looks very unrealistic.

I have a Pre-Requisite Script in Postman, Where I have two environment variables first_name and last_name, I use the faker library to generate dummy data as Following. The dynamic variable $randomFirstName and $randomLastName, generate and set the Environment variables as following. Let assume that $randomFirstName=John and $randomLastName=Doe

postman.setEnvironmentVariable("first_name", pm.variables.replaceIn('{{$randomFirstName}}'));
postman.setEnvironmentVariable("last_name", pm.variables.replaceIn('{{$randomFirstName}}'));

Now I want to use the Environment variable first_name and last_name to use in the following variable email_formatted to generate the email ID which is consisting of first name and last name.

email_formatted = "{{first_name}}" + '_' + "{{last_name}}" + '@gmail.com';
postman.setEnvironmentVariable("email", email_formatted);
console.log("Email: " + postman.getEnvironmentVariable("email"));

However, on the Consol, I am getting the following result.

ACTUAL "Email: {{first_name}}_{{last_name}}@gmail.com"

EXPECTED "Email: John_Doe@gmail.com"

I think I am doing some small mistakes here in syntax or how the Environment variables are used in the Pre-Requisite Script. Thanks for your time in reading, and any leads would be helpful

Nihir Kumar
  • 283
  • 1
  • 6
  • 19

2 Answers2

1
email_formatted = pm.environment.get("first_name") + '_' + pm.environment.get("last_name") + '@gmail.com';
pm.environment.set("email", email_formatted);
console.log("Email: " + pm.environment.get("email"));

in postman we can refer variable as {{variablename}} only in non script sessions. In script you have use

pm.environment.get('varaiblename')

to access environment varaible ,to access other varaibles use as pm.globals, pm.variables etc

As Danny pointed out postman is moving to new pm.* API , and going to deprecate postman.* in future .

So start using the new api as the documentation is extensively updated and available mostly for pm.* Api .

Note: setnextrequest , is still available only through postman.*

So postman.setNextRequest , works pm.setNextRequest will not

The postman app has auto suggest for pm.* , It shows available methods which makes scripting easier and fun

PDHide
  • 18,113
  • 2
  • 31
  • 46
0

If Still using the old Postman libraries then the Following would also Work.

email_formatted = postman.getEnvironmentVariable("first_name") + '_' + postman.getEnvironmentVariable("last_name") + '@' + postman.getEnvironmentVariable("environment")+ 'gmail.com';
postman.setEnvironmentVariable("email", email_formatted);
console.log("Email: " + postman.getEnvironmentVariable("email"));

Nihir Kumar
  • 283
  • 1
  • 6
  • 19