Friday, August 12, 2016

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



No comments:

Post a Comment