Showing posts with label Postman. Show all posts
Showing posts with label Postman. Show all posts

Thursday, January 6, 2022

How to set the generated token as an Environment Variable in Postman Tool

Generated Token Response -

{
    "data": {
        "authToken": {
            "getToken": {
                "accessToken""b1hXMCJ9.eyJhdWETQ1NTk2NSwiYWlvIjoiCJhcHBpZCI6IjQ1ZTMA"
            }
        }
    }
}

Use the below code snippet to set the generated token as an environment variable in Postman Tool

Paste the below code in the Test tab (Please modify the code according to your response).
Environment Variable: token (Use this variable across the API's)

Example: In headers - Authorization : {{token}}


var responseObject = JSON.parse(responseBody);

if (responseObject && responseObject.data && responseObject.data.authToken && 
responseObject.data.authToken.getToken  && responseObject.data.authToken.getToken.accessToken)
{
    postman.setEnvironmentVariable("token"`Bearer ${responseObject.data.authToken.getToken.
                                                            accessToken}`);
}

Thanks for watching this space.

Sunday, May 15, 2016

Web Service Testing - POSTMAN Tool

Introduction to Web Service:

A Web Service is a service accessed via Network. Web Service is a way to publish our application over the web and enable other applications to access functions defined by our web service.

Below are the HTTP Methods, that we're going to learn in web service testing (You can relate the below methods with DML commands in SQL)
  • GET -------------> Select Query
  • POST -----------> Insert Query
  • PATCH ---------> Update Query
  • PUT -------------> Update Query
  • DELETE -------> Delete Query 
POST(Create) - With the help of POST method, we can easily push the records into the table. The success response code is 201 Created.

GET(Read) - With the help of GET method, we can easily read the records from the table. The success response code is 200 OK.

PUT(Update) - With the help of PUT method, we can easily update the records in the table. For updating a value, we have to pass the parameters in the request URL. The success response code is 200 OK / 204 No Content.

PATCH(Partial Update) - With the help of PATCH method, we can easily update the single value in the table. For updating a value, we have to pass the parameters in the request URL. The success response code is 200 OK / 204 No Content.

DELETE (Delete) - With the help of DELETE method, we can easily delete the records from the table. The success response code is 200 OK.

All the above-mentioned HTTP methods are using REST protocol.

Learn about REST Protocol:
REST means REpresentational State Transfer protocol. It is an architecture that generally runs over HTTP.

The advantage of using Restful APIs:

Rest API supports both XML and JSON format. It is usually preferred for mobile and web apps as it makes app work faster and smoother.

Sample XML format for POST request:

<InsertCustomer>
<Name>Balaji</Name>
<TelephoneNumber>9999999999</TelephoneNumber>
</InsertCustomer>

Sample JSON format for POST request:
{
    "InsertCustomer": 
     {
        "Name": "Balaji",
        "TelephoneNumber": "9999999999"
     }
}

Learn about POSTMAN:

Postman is a Chrome App. This means that Postman can only run on the Chrome browser.
Postman is a powerful API testing tool. By using this tool, we can easily test our API’s. With postman, we can construct request quickly, save them as collections for later use. Please refer the figure 1.0.

Figure 1.0 - POSTMAN

How to add POSTMAN to chrome browser
1.Launch the Chrome browser.
2.Type the "Postman Chrome" in the address bar field.
3.Click the first search result.
4.In the postman home page, click the "Add to Chrome" button.

Learn about Collection:

A collection allows you to group individual requests together. These requests can be further organized into folders to accurately mirror your API. Please refer the figure 1.1, 1.2 & 1.3.

The collection runner runs requests in a collection in the order in which you set them. It also runs tests for every request and gives you an aggregate summary of what happened. It stores all your test runs so you can compare them and see what has changed.

All you have to do is select a collection, an environment if needed, and hit the Start Test button. Optionally, you can also add a CSV or a JSON file from which Postman can pick up data values.Please refer the figure 1.4 & 1.5.
If you want to run your request as a collection, we have to purchase jet-packs. It costs 9.99 dollars. 

Jetpacks are awesome upgrades, which will make our API workflow faster and more efficient.
Once you activate the Jetpacks upgrade, you get the following features:
1. Test editor and runner: Lets you write and run tests within Postman.
2. Collection runner: Lets you run an entire Postman collection in one go.
3. Data variables: Let’s you parameterize requests with values from a JSON/CSV file. This makes testing multiple scenarios as easy as adding another row to a spreadsheet.A one-time initialization is required. All future updates will be free.

Figure 1.1 - Create Collection

Figure 1.2 - Add Folder to the Collection

Figure 1.3 - Added Folders into the Collection

Figure 1.4 - Collection Runner




Figure 1.5 - Test Result

Learn about Environment:

While working with APIs, you will often need to have different setups. Environments give you the ability to customize requests using variables. This way you can easily switch between different setups without changing your requests.Environments can be downloaded and saved as JSON files and uploaded later.
Each environment is a set of key-value pairs. These can be edited using the key-value editor. The key is the variable name.
Variables can be used in the following form - {{variable Name}}. The string {{variable Name}} will be replaced with its corresponding value. For example, for an environment variable 'URL' with the value 'http://localhost’, you will have to use {{url}} in the request URL field. {{url}} will be replaced by http://localhost when the request is sent. Please refer the figure 1.6 & 1.7.

Figure 1.6 - Add Environment

Figure 1.7 - Select Environment



Learn about Newman:
Newman is a command-line collection runner for Postman. It allows you to run and test a Postman collection directly from the command-line.Newman is built on Node.js. To run Newman, make sure you have Node.js & python installed.

By using the below commands, we can easily install the newman & run the postman collection directly from the command-line on our machine.

npm install -g newman
newman -c mycollection.json –n 10 –e environment variable –H report.html 
d data.json
-c Specify a Postman collection as a JSON [file]
-e Specify a Postman environment as a JSON [file]
-n Define the number of iterations to run
-H Export a HTML report to a specified file
-d Specify a data file to use either json or csv