Friday, August 12, 2016

API Automation with postman : Pass Data file in runner


 We go through the collection feature of postman.

Lets start basic usage of collection in runner. To run a collection, open the Collection Runner window by clicking on the link in the navigation bar. Choose a collection from the drop down menu, and hit Start.



So this way you can iterate your collection as many time as you want. So your collection run multiple time as you set value in iteration field.

Second thing is you can run your collection by giving csv or json file to pass your data in your API collection. The Collection Runner let’s you import a CSV or a JSON file and then use the values from the data file inside HTTP requests and scripts. We call these variables data variables. To use them inside the Postman UI, you have to follow the same syntax as environment or global variables. Having the same syntax helps you test individual requests inside Postman using dummy environment values. When you move to the Collection Runner you don’t have to change anything.

Data variables in requests
Variables inside the Postman UI are enclosed inside curly braces. For example, in the screenshot below, {{username}} and {{password}} inside URL parameters would be replaced by corresponding values from the data file:


So like this way you can also pass data to the API and run your collection with different data value.


CSV files
For CSV files to work inside the Collection Runner, the first row needs to consist of variable names that you want to use inside requests. Every subsequent row is used as a data row. Make sure the line endings of the CSV file are in the Unix format. That’s a restriction in our current CSV parser.





JSON files
The JSON file needs to be an array of key/value pairs. The keys are used as variable names while the values are replaced inside requests.







For more information you can check  Using CSV data file post in postman blog.






API Automation with postman : Understand basic writing of test using postman



Postman gives you an environment where you can write and run tests for each request without worrying about any extra setup.
A Postman test is essentially JavaScript code which sets values for the special tests object. You can set a descriptive key for an element in the object and then say if it's true or false.

For example : tests["Body contains post id"] = responseBody.has("userId")

Just see below image that display how to write test for any request.  In second image it is testing request for two things one is content and other is status code. So when request is send to server it will compare response and give test result. By this way you can test your request with different condition.





 
 



Commonly used libraries and utilities

  • Lodash: JS utility library
  • jQuery Deprecated: Cross-platform JavaScript library. This will be removed in future versions of the sandbox.
  • BackboneJS Deprecated: Provides simple models, views, and collections. This will be removed in future versions of the sandbox.
  • SugarJS: Extends native JS objects with useful methods
  • tv4 JSON schema validator: Validates JSON objects against v4 of the json-schema draft
  • CryptoJS: standard and secure cryptographic algorithms. Supported algorithms: AES, DES, EvpKDF, HMAC-MD5, HMAC-SHA1/3/256/512, MD5, PBKDF2, Rabbit, SHA1/3/224/256/512, TripleDES
  • xml2Json(xmlString): This function behaves the same in Newman and Postman
  • xmlToJson(xmlString) Deprecated: This function does NOT behave the same in Newman and Postman
  • postman.getResponseHeader(headerName) Test-only: returns the response header with name "headerName", if it exists. Returns null if no such header exists. Note: According to W3C specifications, header names are case-insensitive. This method takes care of this. postman.getResponseHeader("Content-type") and postman.getResponseHeader("content-Type") will return the same value.

Environment and global variables

  • postman.setEnvironmentVariable(variableName, variableValue): Sets an environment variable "variableName", and assigns the string "variableValue" to it. You must have an environment selected for this method to work. Note: Only strings can be stored. Storing other types of data will result in unexpected behavior.
  • postman.setGlobalVariable(variableName, variableValue): Sets a global variable "variableName", and assigns the string "variableValue" to it. Note: Only strings can be stored. Storing other types of data will result in unexpected behavior.
  • postman.clearEnvironmentVariable(variableName): Clears the environment variable named "variableName". You must have an environment selected for this method to work.
  • postman.clearGlobalVariable(variableName): Clears the global variable named "variableName".
  • postman.clearEnvironmentVariables(): Clears all environment variables. You must have an environment selected for this method to work.
  • postman.clearGlobalVariables(): Clears all global variables.
  • environment: A dictionary of variables in the current environment. Use environment["foo"] to access the value of the "foo" environment variable. Note: This can only be used to read the variable. Use setEnvironmentVariable to set a value.
  • globals: A dictionary of global variables. Use globals["bar"] to access the value of the "bar" global variable. Note: This can only be used to read the variable. Use setGlobalVariable to set a value

Next blog is API Automation with postman : Pass Data file in runner



API Automation with postman : Understand environment and collections

Okay So now we are good with request and response of REST API and how to deal it with POSTMAN.

In this post we will understand good reusable feature of postman. 

So lets look what is  Environment in postman. As tester it is necessary to test API in different environment like QA , Stage , Dev etc.  So for each environment there would be different url exist to call server data.

For exp. suppose i have one url :

QA : http://qa.jsonplaceholder.typicode.com/
Stage : http://jsonplaceholder.typicode.com/
Dev : http://dev.jsonplaceholder.typicode.com/
UAT : http://uat.jsonplaceholder.typicode.com/

Thinking.... 

So do i need to re-write each request for different environment ? Really ... ohh then its very confusing and time consuming to test...  Hey Wait ...don't do that there is solution. Postman give you environment feature through which we can use single request to test in different environment. 

Just click on Manage Environment set all URL in it.











Just check above image that need to select environment and just call key with {{keyname}} and click on send. It will get the response from QA environment.  So if user want to check data from any other environment like Stage, UAT then need to just select environment.

Okay so i think we are done with environment brief. But what about Collection ?

Hmm... Good So basically collection provide you set of API requests which can be combine with one name. So this collection can be download , share to other team or used in runner to run all request  at single click.



Suppose you have different GET, POST, PUT, DELETE request and you want to combine all request in single file then one should create collection. To create it just click on create new collection icon from sidebar and then give the name and description. Now save your all requests one by one to collection. So it will create one bunch or set of API requests. Just go through below image.





Make any request and then just simple save that request in collection.


It will save the request in collection.


As above you can share to team or run collection on single click


API Automation with postman : Understand request and response

As we understand basic UI of Postman now we are going to understand how to deal with sending request and how to read response.

So First and foremost we need any testing API URL to work with request and response.

Good things is that  JSONPlaceholder  provide testing of fake REST API.

You can test with GET , POST , PUT , DELETE using this URL.

So Lets start with API testing.

Root URL : http://jsonplaceholder.typicode.com/

GET Example :

Enter http://jsonplaceholder.typicode.com/posts/1 in request url and select method as GET. Add content type header as json/application. And click on send.  So we are sending request to Get some posts data from client server using postman.





As you can see in above image response of request is display in Body part. Also request is OK so display 200 ok response code.

POST Example :

To pass some data to server we required POST request. So user can pass some data and get desire output in response. To insert or input some data in server database POST , PUT are generally used.


As you can see in above image i am passing json data in raw section. Also request type select as POST and enter http://jsonplaceholder.typicode.com/posts/ url and click on send.
It will send request to server and add post data in database. As you can see response  in Body section and also response code as 201 created.


PUT Example :

To change value of existing data or want to update value of existing posts PUT call is generally use. It is used to update resources of data into server.



As you can see in above image if user want to update some resources value for post id 1 then need to enter roots url with post id. Also select request type as PUT and click on Send.
So It will send PUT request to rest client server and update or change the value for post which id = 1. And also give response in Body with response code.



DELETE Example :

 So for simple want to delete some data from rest client server database you just need to select DELETE and put URL with post id and click on Send. So it will give response code 200 OK means operation is successful and it has delete resource which was requested.



 For more information regarding API code refer this link.


Next blog is for API Automation with postman : Understand environment and collections

API Automation : API Testing make easy with Postman

Think About Rest API Automation Then Think About very easy to use tool POSTMAN.

Gr8...

So we are here learn a very basic and very useful things to implement API automation.

Prerequisite.

So postman is chrome App. So you first need to install POSTMAN add-on into chrome browser. So first install  Chrome browser and then install postman.

Click here to install : POSTMAN

Now you are ready to deal with API testing hurrrray....



Lets just take simple go through of postman UI. To test Rest enable client server for happy path running scenario you need to test simple four request /response  for any API.

GET    - To Get some data via API request
POST  - To Insert/Input some data via API request
PUT    - To Update data via API request
DELETE  - To Delete data via API request.


Below images represent where to put request URL and how to make side  by side request.





 (1).  Basically  request editor in UI is section where user can add base URL to check request and response. Also UI has sidebar which maintain collection of request and also maintain history in history tab.



 (2). Enter Base URL request and add parameters with key value using editor if required. Also select request type as GET , POST, PUT etc.



(3) To supplies authentication use header and give auth type and credential.


Now we will understand basic of sending request and response in next post.

For  API Response code refer API Response Code.



You can also check below links to implement API testing in  postman step by step.

API Automation with postman : Understand request and response

API Automation with postman : Understand environment and collections

API Automation with postman : Understand basic writing of test using postman

API Automation with postman : Pass Data file in runner