In these examples, we are using the famous console.log in the Pre-request Script and Tests tab
Look at the third picture, to see the console output after we make the request. [3/10]
Let us create an API test that will take a dynamic cost param, and we'll assert that the response had that same value.
๐Pre-Request Script๐
- 1. Use Math JavaScript object to generate a random number
- 2. Postman environmental variable to save the value for later
[4/10]
In the previous tweet, we created a random cost environmental variable, now we can use that in our request.
๐Request๐
- 1. Let the cost param in our request use the `cost` environmental variable - using {{cost}} [5/10]
Now we need to assert once the response returns.
๐Tests๐
- 1. Use the pm.test function to create a test
- 2. Get the cost environment variable-expected value
- 3. Get the response using `pm.response.json()`-actual value
- 4. Compare actual and expected [6/10]
Let's run out the test, and see what happens.๐ง
๐จResults๐จ
- 1. The test failed, since the API returned cost as a string but our actual cost is a number
- 2. We did note that the two values were 77 (both actual and expected)
- 3. This could be a ๐...๐ [7/10]
โ We dynamically created a random cost using JavaScript in our Pre-Request script
โ Then use that value in our Request
โ And finally used that to assert something in our response.
๐ Where we potentially found an issue. Should cost be a string or an integer? [8/10]
๐ In closing, JavaScript is baked in a lot of tools that QA uses on a daily
Understanding JavaScript can be very helpful/beneficial, especially if you want to #LevelUpYourAutomation [9/10]
โLet me know if this tweet was helpful. Did you learn anything? Have a great day!
โข โข โข
Missing some Tweet in this thread? You can try to
force a refresh