Wednesday 20 July 2016

API Automation Testing

What is an API?
a set of functions and procedures that allow the creation of applications

API is an acronym for Application Programming Interface.
It enables communication and data exchange between two separate software systems

HTTP Methods
GET     -Requests data from a specified resource
POST    -Submits data to be processed to a specified resource
HEAD    -Same as GET but returns only HTTP headers and no document body
PUT    -Uploads a representation of the specified URI
DELETE  -Deletes the specified resource
OPTIONS -Returns the HTTP methods that the server supports
CONNECT -Converts the request connection to a transparent TCP/IP tunnel

Swagger is a simple yet powerful representation of your RESTful API

Test Cases for API Testing:
Return value based on input condition:
Does not return anything:  
Trigger some other API/event/interrupt:
Update data structure:
Modify certain resources:


What is JSON ?
    JSON stands for JavaScript Object Notation
    JSON is a lightweight data-interchange format
    JSON is language independent *
    JSON is "self-describing" and easy to understand


JSON Syntax Rules
JSON syntax is derived from JavaScript object notation syntax:

    Data is in name/value pairs
    Data is separated by commas
    Curly braces hold objects
    Square brackets hold arrays

JSON Values
    A number (integer or floating point)
    A string (in double quotes)
    A Boolean (true or false)
    An array (in square brackets)
    An object (in curly braces)
    null

Example:
"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter","lastName":"Jones"}
]
====================================================================

Advantage of API Testing
-Testing core functionality
-Time Effective / Quick Response time
-Language Independent
-Easy Integration with GUI


API Testing Tools:
PostMan
RestLet Client
SOAP UI

REST:

-Representational State Transfer
-Its not protocol

XML, JSON,

What is Rest-Assured:

-is an pen Source Java Domain-Specific Language [DSL]
-Supports both XML & JSON
-Can eliminate large amount of code to test complex API Response and output

REST-Assured Support other Request
-POST -GET
-PUT-
-DELETE
-OPTIONS
-PATCH
-HEAD

*Only GET will give us data
*Other will make changes to server

http://rest-assured.io/

1.Creare Simple Maven Project
2.Add in pom.xml
<dependency>
  <groupId>io.rest-assured</groupId>
  <artifactId>rest-assured</artifactId>
  <version>${rest-assured.version}</version>
</dependency>
https://mvnrepository.com/artifact/io.rest-assured/rest-assured/

2.Add package
3. Add class

Paste this code
public class getData { @Test public void testResponseCode() { Response resp =RestAssured.get("http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22"); int code =resp.getStatusCode(); System.out.println("Status Code is: " +code); Assert.assertEquals(code , 200); } }

Oputput



Open API Example:

1. https://openweathermap.org/
- Sign In to get API Key
-here we will get JSON Data exmple

2. http://parabank.parasoft.com/parabank/services/bank/customers/12212
here we will get xml data

to try Postman
https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en

Select GET and paste
http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22






2 comments:

MobileAutomation - Handle Pop-ups, Alerts and Prompts in Automate and App Automate Tests

This article shows you how to handle user permission pop-ups, alerts, and prompts in your automated tests on BrowserStack. Introductio...