Making GET on dynamic environment variable

I set a dynamic variable in the pre-request script using pm.environment.set(“artistName”,pm.variables.replaceIn(’{{$randomLastName}}’));
This set fine

I wanted to call it back in a test using pm.test(“Artist name is set correctly”, function(){
const jsonData = pm.response.json();
pm.expect(jsonData.contact.name).to.eql(pm.environment.get(’{{artistName}}’));

and this gave me the response: AssertionError: expected ‘Schiller’ to deeply equal undefined

what have i done wrong?

you can get a varaible value in two ways:

first :

pm.environment.get('artistName')
pm.environment.replaceIn("This will replace {{artistName}} its actual value")

thank you, works great now

Just updated the answer ,for more clarity . I answered it from phone last time . ypou can use any of the above approach to get value of a variable .

and you can refer this variable in any other part except scripts as {{varname}}

hi i wanted to ask based on this, i have another place where i had a very similar scenario
i used the script you specified and its not working
i have saved an environment variable using the pre-request script: -
pm.environment.set(“minimumPayment”, pm.variables.replaceIn(’{{$randomInt}}’));
and in the body called this back using “{{minimumPayment}}” and all seems fine
However when i run a test i wrote it as follows:-
pm.test(“Minimum Payment is populated by minimumPayment”, function(){

const jsonData = pm.response.json();

pm.expect(jsonData.minimumPayment).to.eql(pm.environment.get('minimumPayment'));

})

and it is failing saying something like:-
Minimum Payment is populated by minimumPayment | AssertionError: expected 16 to deeply equal ‘16’

can you help me? thanks

pm.expect(jsonData.minimumPayment).to.eql(pm.environment.get('minimumPayment'));

you have stored the payment as string than number, the deep comparison will consider the object type also . so convert it to Number type :tada::tada::tada:


pm.expect(jsonData.minimumPayment).to.eql(Number(pm.environment.get('minimumPayment')));
1 Like

thanks so much, wks great