30 Days Developer Challenge - Day 12 - `collection_uid` set as environment variable in `Postman API env`

In the collection named Day 12: Postman API, collection_uid is set as environment variable when creating the environment named Postman API env as part of the day’s challenge. This restricts a user from setting the collection_uid as a collection variable to be used in submit request inside Submit your solution folder for all upcoming days’ challenges Postman API env is to be used.

This restriction is due to the scope of variables where an environment variable overrides the collection variable.

For an instance, in Day 13: Newman, the submit request uses the Postman API env as the environment which has a variable named collection_uid. The value of this variable corresponds to Day 12: Postman API collection. Thus, a user cannot set a collection variable collection_uid in Day 13: Newman collection because the environment variable will always take precedence. If not done so, the test cases will fail. In such a case, the user cannot use the request URL https://api.getpostman.com/collections/{{collection_uid}} and instead has to type in the entire collection ID in the request URL as a string literal.

Can we change the documentation of Day 12: Postman API to set collection_uid as the collection variable instead of environment variable?

1 Like

Hello @srishtigupta9,
I have come across this as well while completing the challenge. But, I do not think the documentation has to be changed because there’s a simple workaround:
Just update the collection_uid environment variable in the pre-request scripts for all days after Day 12. This way, each time the test is evaluated the collection_uid is dynamically updated to that of the current collection.

@AnirudhBabu


When I am attempting to complete Day 12 for the 30 Day Challenge, when I submit my solution, three of the tests are getting failed.

@daminimehra
Hmm, I see. I need you to check some stuff.
Do the tests for the individual requests pass?
If no, is your auth type for the collection set to API Key?

Let me know if that helps.

yes, I am done with that but still getting failed three of the tests.


yes, this helps me but still get this.

@daminimehra
You are good to go!
The animation for Day 12 always had an issue.

Have fun with Day 13!

1 Like

Hi, was wondering if you could help me with my Day 12 assignment. These are the errors I am getting back. Let me know if you need any more information!

The reason that the cat visualization displays “please try again” although all five tests have been passed with success, is that the test script runs asynchronously: More precisely, the evaluation “if (pass == totalToPass) {…” takes place when totalToPass is still 2. The variable totalToPass reaches value 2 almost immediately, whereas the remaining 3 increments , each of value 1, comes a little later, as the responses returned from the Postman API comes in.

Basically, it should be along the lines of: When, and only when, all three responses (get collection, get environment, and get workspace) have been received, then the test should evaluate the expression pass=totalToPass.

A simple solution, which is acceptable, make the Day 12 test work, is to encapsulate the final evaluation in a “setTimeout” context, as shown below. In this example, the final test is asserted after 1 second (1000 milliseconds), which is enough for the Postman API to return its three responses. If the Postman API was slower, we would have to increase the timeout to 2000 milliseconds (2 seconds), or even more. This also reveals the limitation of this simple timeout construction: When we do not know the time spent by the Postman API, to return the three responses, we do not know how long time to wait. In fact, we might be tempted to set the timeout to a value larger than necessary, hereby causing a delay which is longer than really necessary. Hence, a more precise reconstruction of the test script would be to use a fetch-promise-then construction, hereby letting the final test await the arrival of the three responses, but not more than that.

setTimeout(() => {
// visualization for test results
let template
console.log(totalToPass)
console.log(pass)
if (pass == totalToPass) {
template = 🍪 passing! <br /> <img src="https://media4.giphy.com/media/gKfNj8cYeGN63bLRkF/giphy.gif?cid=ecf05e47h66ifoa2zze9t2jpukur7nd4h7dzqaq2ab4rt2yi&rid=giphy.gif&ct=g" />
} else {
template = 🙅 please try again <br /> <img src="https://media1.giphy.com/media/iqzp1pBJtNQUU/giphy.gif?cid=ecf05e47h66ifoa2zze9t2jpukur7nd4h7dzqaq2ab4rt2yi&rid=giphy.gif&ct=g" />
}
pm.visualizer.set(template)
}, 1000)

Cheers,
Kåre