2

I need to get current exact collection variable value.

In pre-request script of a postman request I'm setting 2 collection variables as follows

pm.collectionVariables.set("firstCollectionVariable", "string_{{$guid}}");
pm.collectionVariables.set("secondCollectionVariable", "second_variable_{{firstCollectionVariable}}");

then I'm using those 2 collection variables in a post request body to set specific data as follows

 { 
    "firstKey": {{firstCollectionVariable}},
    "secondKey" : {{secondCollectionVariable}},
 }

firstKey and secondKey are set as expected

firstKey => "string_c6631d2c-2427-4903-b604-8120662a5e0e"

secondKey => "second_variable_string_c6631d2c-2427-4903-b604-8120662a5e0e"

The problem is when I'm trying to check the response using

pm.expect(pm.response.secondKey).to.eql(pm.collectionVariables.get("secondCollectionVariable"));

I got assertion error

AssertionError: expected 'second_variable_string_c6631d2c-2427-4903-b604-8120662a5e0e' to deeply equal 'second_variable_{{firstCollectionVariable}}'

How I can get the current exact value of collection variable?

  • Does this answer your question? [Accessing Collection Variables in Postman](https://stackoverflow.com/questions/47680580/accessing-collection-variables-in-postman) – Henke Jan 26 '21 at 17:25

2 Answers2

1

maybe using to.be.deep can solve your problem, try something like:

pm.expect(pm.response.secondKey).to.be.deep.equals(pm.collectionVariables.get("secondCollectionVariable"));

or

pm.expect(pm.response.secondKey).to.be.deep.equal(pm.collectionVariables.get("secondCollectionVariable"));

Another nice shot can be just use single quotes to get your collectionVariable like 'secondCollectionVariable'

I hope this works for you, best regards!

Caíque de Paula
  • 424
  • 5
  • 17
0

Converting your variables to string might also solve your problem.

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37