Why API Automation?
Early Defect Detection
Most UI layers use REST webservices to fetch data from backend and do the business validation any issues with REST services will found before developers actually consumer that web services. It will saves development time over the defected webservices.
Contract Validation
In distributed environment different scrum teams involved in the single functionality. If one team changes some parameters related to web service which consume by team by last sprint and if they failed notify the team. This will lead to production defect of that functionality which is not part of the current sprint.so to avoid this validate contract is very important thing.
Stops further build
If test failed in automation environment, It stops the further build until the error is rectified. Its the very good practice to pass testers for testing.
Fast Result
This help in Regression testing suit for an application. Its very important in CI/CD (Continuous integration/continuous deployment)environments will take less time to complete test cycle.
Reliable
If tests are passed without any issue then business layer is not having any defect.
Test Pyramid
API Automation involves significant role in building the test pyramid. Unit test cases have more numbers which helps automation framework.
What is REST?
REST(Representational State Transfer) is an approach to communicate in the development of Webservices
REST uses HTTP protocols to communicate
HTTP methods: GET,PUT,POST,DELETE
Uses URIs
How to send request using HTTP GET?
Request GET :https://reqres.in/api/users/2
Status code : 200
{
"data": {
"id": 2,
"email": "janet.weaver@reqres.in",
"first_name": "Janet",
"last_name": "Weaver",
"avatar": "https://reqres.in/img/faces/2-image.jpg"
},
"support": {
"url": "https://reqres.in/#support-heading",
"text": "To keep ReqRes free, contributions towards server costs are appreciated!"
}
}
What is REST ASSURED?
REST Assured is a java library for testing of REST services
Integrates with existing java based testing frameworks like Junit,TestNG and Selenium WebDriver
REST Assured Features
Support for HTTP methods
supports for BDD/Gherkin(Given,When,Then)
Uses Hamcrest matches for checks(like equql to)
What REST Assured gives?
Format HTTP request
Send it over to the network
Validating HTTP Status code of a request
validating response code
REST Authentication mechanisms
Basic Authentication(Username and password)
OAuth
Digest
sessionbased Authentication
Getting started with REST Assured
Maven Dependency of REST Assured
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rets-assured</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
Writing Test
given() ----------> Information about the URI
statements
When() ------------> Action
statements
Then() --------------> Validation
statements
How to write HTTP GET Request?
request=given()
.contentType("application/json")
.auth()
.basic(username(),password())
.When()
.Then()
.statuscode()
This is the simple way of sending request and validating the response code.