0

How to reuse in Postman variable's value in different variable. For example:

I've made three variables in new environment (which is selected as using):

  • name, with initial and current value: {{$randomFirstName}}
  • surname, with initial and current value: {{$randomLastName}}
  • email, with initial and current value: {{name}}{{surname}}@example.com

And in my request JSON I've set:

{
    "name": "{{name}}",
    "surname": "{{surname}}",
    "email": "{{email}}"
}

But the email consists different name and surname.

{
    "name": "Bettye",
    "surname": "Schinner",
    "email": "EthaBotsford@example.loc"
}

Then, I've tried to create some Pre-request sctiprs. I've cleaned mentioned variables from values.

postman.setEnvironmentVariable("name", pm.variables.replaceIn('{{$randomFirstName}}'));
postman.setEnvironmentVariable("surname", pm.variables.replaceIn('{{$randomLastName}}'));
postman.setEnvironmentVariable("email", pm.collectionVariables.get("name")+pm.collectionVariables.get("surname")+"@example.com");

But this way I got still same email, independently from name and surname. What do I wrong?

Bejkrools
  • 1,129
  • 2
  • 17
  • 38

1 Answers1

1

I should use pm.enviornment instead pm.collectionVariables:

postman.setEnvironmentVariable("name", pm.variables.replaceIn('{{$randomFirstName}}'));
postman.setEnvironmentVariable("surname", pm.variables.replaceIn('{{$randomLastName}}'));
postman.setEnvironmentVariable("email", pm.environment.get("name")+pm.environment.get("surname")+"@example.test");
Bejkrools
  • 1,129
  • 2
  • 17
  • 38