Introduction to API automation

API stands for application programming interface. API helps 2 or more programs to communicate with each other. Web API use REST based communication.

SOAPUI / Readyapi

ReadyAPI is a very popular tool to develop and test REST and SOAP APIs. Main features of ReadyAPI are
  • Easy to use UI
  • Send any type of HTTP requests (GET, POST, PUT, DELETE etc)
  • Ability to add HTTP headers and JSON payloads
  • Monitor the raw HTTP request and response

Postman

Postman is a very popular tool to develop and test REST and SOAP APIs Main features of Postman are
  • Easy to use UI
  • Send any type of HTTP requests (GET, POST, PUT, DELETE etc)
  • Ability to add HTTP headers and JSON payloads
  • Use collections to group similar requests
  • Export and import collections easily
  • Monitor the raw HTTP request and response
  • Free version of the tool is also available. But If you want more advanced features you can go for pro version as well

Rest Assured

Rest assured is the framework written in Java to test rest and soap services. Just follow below steps to test rest apis.
  • Create a request
  • Send get or post request to server
  • Read the response and do assertions
You can create a request as shown in below example

 RestAssured.baseURI = url;

 RestAssured.requestSpecification = new RequestSpecBuilder().
                addHeader("Content-Type", "application/json").
                addHeader("Origin","https://xyz.com.au").build();

Then Send the get request

 Response response = given().when().get();
 response.print();

You can send the post request as shown in below example

Response response = given()
.body("{"quoteRequests":[{"@type":"imt/1-0-0/quoteRequest","@type": 
"xyz/1-0-0/quoteRequest","from":{"amount":100,"currency":"AUD"},
"to":"USD",
"created":"2018-11-23T06:17:31.818589Z"}], "verticals":["xyz"]}")
.post();

Then you can do assertions as shown below.

assertThat(response.getStatusCode()).isEqualTo(200);
List results = response.jsonPath().getList("code");
assertThat(response.jsonPath().getList("label")).contains("Australian Dollar");
assertThat(response.jsonPath().getList("symbols").get(0)).asList().contains("24");

        response.print();
        assertThat(response.getStatusCode()).isEqualTo(200);

Complex problems, Simple Solutions