Friday 15 May 2020

Automate GET request with Rest Assured? API Automation interview question on Rest Assured?



Requirement:
- Automate GET api for endpoint: /api/users and validate status code as 200

=================CodeStart================
@Test

        //public is access modifier and void as nothing is returned for getusers()

        public void getusers()
   
        {
           
            // resp will contain the response from the api get method for /api/users?page=2
       
            Response resp = given()
                    .when().get("serverURL/api/users?page=2");
           
            //save the actual status code in integer variable, variable name is actualstatuscode

            int actualstatuscode = resp.getStatusCode();
           
            //print the status code in the console
            System.out.println(resp.getStatusCode());

           //print the response body in console
            System.out.println(resp.getBody().asString());
           
            //validating actual status code that we got from API response with the expected status code 200
            assertEquals(200,actualstatuscode);
           
        }

===============CodeEnd================
Note :
Import below in your package before using the code and add rest assured/testng jar:
import static io.restassured.RestAssured.given;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
import io.restassured.response.Response;
====================================
Note: If anyone need help in designing framework or virtual support for Automation work then reach out to me @sidharth.shukla19@gmail.com/9619094122 
**************************************
For training check the Training link as below:
Training Details
Check below link on top Interview preparation topics:RestAPI
SQL Query   Cucumber-BDD    Linux Commands    Maven   GIT 

TestNG@after@before
 

*************************************************

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions



No comments:

Post a Comment

All Time Popular Posts

Most Featured Post

API Status Codes with examples for QA-Testers

  🔺 LinkedIn: https://www.linkedin.com/in/sidharth-shukla-77b53145/ 🔺 Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 🏮In API testing, it...