首页 > 其他 > 详细

Using variables inside Postman and Collection Runner

时间:2017-03-31 18:07:10      阅读:262      评论:0      收藏:0      [点我收藏+]

 

Variables are among the most powerful features in Postman. Using variables in your Postman requests, eliminates the need to duplicate requests, which can save a lot of time!

A very common scenario while testing APIs is that the API infrastructure might be present on your local machine, a staging setup, and a production setup. Using variables you can call all these APIs without having to replace values manually. You can also use variables to test for different users (for example, they might need different access tokens), different input values, and a lot more.

With Jetpacks(http://blog.getpostman.com/2014/01/16/ridiculously-easy-api-testing-introducing-jetpacks-for-postman/), you get even more power to play with variables. You can extract data from responses and set variable values automatically. You can chain requests one after the other with minimal effort. Variable values are also available inside tests to help you make them more generic.

Data variables let you test multiple instances of a request in one go. Imagine that you want to test an API which let‘s users post comments on a post. Using data variables you can test for hundreds of variations of a request with different IDs, tokens or content bodies.


Let‘s look at how you can use variables in your workflow inside Postman.

Create an environment

For this example, let‘s assume we want to create two environments, production and dev.

Click on "No environment" in the header.

技术分享
1-dropdown
2. Select "Manage environments" and then on the "Add" button in the modal that comes up.
技术分享

2-manage-environments
3. Fill in the values as shown in the screenshot below
技术分享
3-add-environment

Press submit.
To create the dev environment, we can just duplicate this environment so that we don‘t have to type in variable keys all over again.

4-duplicated-environment

技术分享

Click the duplicate environment icon, and then click on the environment name to edit. Fill in the values, as shown in the screenshot below.

4-2-duplicate-environment-values

技术分享

Variables can be used almost everywhere inside Postman. They are available inside:

URL
URL params
Header values
form-data/url-encoded values
Raw body content
Helper fields
To use a variable you need to enclose the variable name with double curly braces – {{my_variable_name}}.

With our environments created, let‘s try out a sample request. Set the base URL field for the API to {{url}}/post.

6-url-request

技术分享

Now select an environment from the environment selection dropdown.

5-dropdown-filled

技术分享

Once an environment is selected, Postman will replace all instances of a variable with it‘s corresponding value. If no environment is selected, then Postman will try to find a matching global variable.

On hitting send here with the "Production" environment, we see that the request is made to "httpbin.org".

7-sent-request

技术分享

Change the environment to "dev", and hit send. The request will be made to localhost:5000. It‘s as easy as that!

Now, let‘s modify this request by adding a POST body and setting a variable inside the body.

8-post-request

技术分享

On hitting send, we see that the API received the response body with the variable replaced by it‘s value.

9-post-request-response

技术分享

Variables with test scripts

You will need the Jetpacks upgrade for writing test scripts. If you still haven‘t purchased it, I strongly recommend that you do!

Inside Postman test scripts, you can set environment and global variables using the postman.setEnvironmentVariable and postman.setGlobalVariable functions. You can also access these values using the special environment and globals dictionaries.

Check out this blog entry about how you can extract values from response bodies and assign them to variables. Subsequent calls are made using these values. This is an extremely powerful feature and I am sure you will wonder how you went by without it!

Data variables

Data variables are used inside the Collection Runner. The Collection Runner can run a collection or a folder multiple times. For every iteration, it will pick up new values from a JSON or a CSV file and replace variables in the request with these values.

Let‘s save our request above in a collection and use data variables to change the response body.

10-data-variables-request

技术分享

As shown in the screenshot above, I have added a couple of parameters to the JSON body. Values for these will be filled by Postman from a data file.

11-data-variables-test

技术分享

I have also added a couple of tests which checks whether the correct value was received in the response. You can access the data values for a specific iteration using the special data variable. You can log values to the Chrome DevTools console inside test scripts. Make sure to enable Chrome DevTools first.

Postman lets you import CSV or JSON files as data sources.

The CSV file needs to have the variable names in it‘s first row. Subsequent rows should be separated by line breaks while the values should be separated by commas.
Download a sample CSV file(http://getpostman.com/samples/test_data_file.csv) to find out how it looks like.

profile_url, username
http://www.google.com, a85
http://www.google.com, google

 

The JSON file is an array of objects, with each object having key/value pairs. Postman supports only simple numerical and string values inside the JSON file.
Download a sample JSON file(http://getpostman.com/samples/test_data_file.json) to find out how it looks like.

[
{
"profile_url": "http://www.google.com",
"username": "a85",
"password": "blah"
},
{
"profile_url": "http://www.getpostman.com",
"username": "larry",
"password": "nocolors"
}
]

 

12-data-variables-start-test-run

技术分享

Click on "Choose Files" and import the file that has the data with which the API is to be tested. For this example, we‘ll just run 2 iterations. I am running this example in the dev environment. Note that environment variables, global variables and data variables can be used together.

After clicking start, the requests are run one after the other. The Collection Runner window tells us that all our tests passed!

13-data-variables-test-run-results

技术分享

Using variables is essential to maximising your Postman workflow. I hope this tutorial helped you gain a better understanding of this feature. If you have any questions, do let me know in the comments.

 

http://blog.getpostman.com/2014/02/20/using-variables-inside-postman-and-collection-runner/

 

Using variables inside Postman and Collection Runner

原文:http://www.cnblogs.com/softidea/p/6652604.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!