Make Selenium Easy

Postman Tutorial Part 33 – Retrieve Path Variables Value in Tests Script in Postman

We have already seen Usage of Path variables in Postman. We may need to retrieve provided path variables for assertion in scripts in Postman. We have already seen about Retrieving query parameters in tests in Postman. In this post we will see retrieving Path variables in tests in Postman.

We can retrive provided path variables only in “Pre-request script” as once request is hit , path variables can not be retrieved as final URI is constructed using path variables. You will get null values if try to retrieve path variables in “Tests” tab. If you want to use path variables values in “Tests” tab, you can store in environment or global variable and use it.

To get value of specific path variable :
pm.request.url.variables.get(‘id’);

To get all path variable in a list:
pm.request.url.variables.all();

Let’s create a URI with Path variables :-

Add below code to “Pre-request Scripts” to retrieve path variables:-

Hit the request and check Postman console:-

Import above example collection from here:-

https://www.getpostman.com/collections/c7b0ba6698adfd1f897f

Refer basics of API Testing here.

Refer detailed tutorial of Postman here.

More about API Testing in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyApiTesting

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Postman Tutorial Part 33 – Retrieve Path Variables Value in Tests Script in Postman

by Amod Mahajan time to read: 1 min
0
%d bloggers like this: