Thursday 18 June 2020

Top 20 API Interview questions and answers with code for automation using Rest Assured SET-02? Automation of GET-PUT-POST-PATCH


 

 
Check below link for question SET-01:
Top API Interview Question 1-10
 


WHAT IS AN API?

Imagine we are sitting at a table in a restaurant with a menu of choices to order from. The kitchen is the part of the “system” that will prepare your order. What is missing is a critical link to communicate your order to the kitchen and deliver your food back to your table. That’s where the waiter or API comes in. The waiter is the messenger – or API – that takes your request or order and tells the kitchen – the system – what to do. Then the waiter delivers the response back to you; in this case, it is the food.

Here is a real-life API example. We may be familiar with the process of searching for flights online. Just like the restaurant, you have a variety of options to choose from, including different cities, departure and return dates, and more. Let us imagine that you’re booking you are flight on an airline website. 

We choose departure city and date, return city and date, cabin class, as well as other variables. In order to book your flight, you interact with the airline’s website to access their database and see if any seats are available on those dates and what the costs might be.

However, what if we are not using the airline’s website? What if we are using an online travel service, such as Kayak or Expedia, which aggregates information from a number of airline databases?

The travel service, in this case, interacts with the airline’s API. The API is the interface that, like your helpful waiter, can be asked by that online travel service to get information from the airline’s database to book seats, baggage options, etc. The API then takes the airline’s response to your request and delivers it right back to the online travel service, which then shows you the most updated, relevant information.

 

11. Automate GET method and validate the status code?

 

@Test(description="Verify status code for GET method-users/2 as 200")
public static void verifyStatusCodeGET() {

Response resp = given()
                                        .when()
        .get("https://reqres.in/api/users/2");

assertEquals(resp.getStatusCode(),200);

}


 

12.  Automate GET method and fetch response body?

 
 @Test(description="Verify status code for GET method-users/2 as 200")
 public static void verifyStatusCodeGET() {
     
     Response resp=given().when().get("https://reqres.in/api/users/2");
     assertEquals(resp.getBody().asString(),200);
     
 }

Define API Authenticationmethod automation with Rest Assured for basic, Pre-emptive, Digest?

 
Click here for Answers with Scripts

 
 13. Automate GET method and verify value from response body?(validate that total number pages =12)
 

@Test(description="Verify status code for GET method-users/2 as 200")
public static void verifyStatusCodeGET() {

Response resp=given().when().get("https://reqres.in/api/users");

System.out.println(resp.path("total").toString());

assertEquals(resp.getStatusCode(),200);
assertEquals(resp.path("total").toString(),"12");
}




     
 14. How to pass query param with GET method in Rest Assured?

API Query parameters can be defined as the optional key-value pairs that appear after the question mark in the URL. Basically, they are extensions of the URL that are utilized to help determine specific content or action based on the data being delivered. Query parameters are appended to the end of the URL, using a '?

@Test
public void validateQueryParamInGiven() {
 
Response resp = given().queryParam("page", "2").
when().get("https://reqres.in/api/users");

assertEquals(resp.getStatusCode(),200);

System.out.println(resp.getBody().asString());
}



How to Setup API automation framework from scratch using Rest Assured, Gradle with code?


   
15. How to pass header for GET method in Rest Assured?

@Test
public void validateGivenHeader() {
 
Response resp = given().header("Content-Type", "application/json").
when().get("https://gorest.co.in/public-api/users");

assertEquals(resp.getStatusCode(),200);
System.out.println(resp.getBody().asString());
}


16. How to automate PATCH method in rest Assured?

The HTTP PATCH method can be used when a resource needs to be updated. This method is especially useful if a resource is large and the changes being made are small.

@Test(description="validate with jsonpath and json object and pass post body as json file")
public void MethodValidationPUT() throws IOException, ParseException {
 
FileInputStream file = new FileInputStream(new File (System.getProperty("user.dir")+"\\TestData\\put.json"));

Response resp =
given().header("Content-Type" , "application/json").body(IOUtils.toString(file,"UTF-8")).
        when().patch("https://reqres.in/api/users/2");

assertEquals(resp.getStatusCode(),200);
assertEquals(resp.path("job"),"tester");
 
}



17. How to automate PUT method in Rest Assured?

A PUT method puts or places a file or resource precisely at a specific URI. In case a file or a resource already exists at that URI, the PUT method replaces that file or resource. If there is no file or resource, PUT creates a new one.
 

@Test(description="validate with jsonpath and json object and pass post body as json file")
public void MethodValidationPUT() throws IOException, ParseException {
 
FileInputStream file = new FileInputStream(new File (System.getProperty("user.dir")+"\\TestData\\put.json"));

Response resp =
given().header("Content-Type" , "application/json").body(IOUtils.toString(file,"UTF-8")).
        when().put("https://reqres.in/api/users/2");

assertEquals(resp.getStatusCode(),200);
assertEquals(resp.path("job"),"tester");
 
}





18. How to automate POST method in Rest Assured?

 POST requests are used to send data to the API server to create or update a resource. The data sent to the server is stored in the request body of the HTTP request

@Test(description="validate with jsonpath and json object and pass post body as json file")
public void MethodValidationPOST() throws IOException, ParseException {
 
FileInputStream file = new FileInputStream(new File (System.getProperty("user.dir")+"\\TestData\\put.json"));

Response resp =
given().header("Content-Type" , "application/json").body(IOUtils.toString(file,"UTF-8")).
        when().post("https://reqres.in/api/users");

assertEquals(resp.getStatusCode(),201);
assertEquals(resp.path("job"),"tester");
 
}



Check below link for SET-1 interview question on API Testing 1 to 10:

To check API related topics and interview tips check below and keep reading:

    

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

✍️AUTHORLinkedIn Profile

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

Learn (API-Microservice-Automation)Testing + Selenium UI Automation + SDET with Self Paced Videos prepared by FAANG employees and LIVE Doubt Session 

SDET TRANING VIDEOS AVAILABLE with Live Doubt Session. Check out the Training Page for Course Content or reach out @whatsapp +91-9619094122. 

This includes class-notes, 400+ interview questions, 3 projects, 20+ Java Coding question set for product companies along with career guidance from FAANG employees for Automation and SDET. We provide full support in clearing doubts & it is paid with very minimal amount.

We will also help you to design the best utility package and framework for your next project. Any doubts then reach out to us at the below whatsapp link.

Learning is directly proportional to earning so keep learning....

For more details whatsapp: https://lnkd.in/dnBWDM33


Check below link for question and answers with the code:

Top API Interview Question 1-10

Top API INterview Questions 11-20 

Top API AUTOMATION Interview Q&A - 21-30

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

SeleniumWebdriver Automation Testing Interview Questions:

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

API Testing Interview Question Set:

https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html

DevOps Interview Q&A:

https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html 

Kubernetes Interview Question Set

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

Docker Interview Question Set

https://automationreinvented.blogspot.com/Docker

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/2021/09/top-40-git-interview-questions-and.html

Coding Interview Question Set:

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

Mobile Testing Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

Python Interview Question Set for QAE - SDET - SDE:

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


 

3 comments:

  1. Response resp = given().header("Content-Type", "application/json").body(IOUtils.toString(file, "UTF-8")).when()
    .put("https://reqres.in/api/users/2");

    Error observed and does not compile:

    The method toString() in the type Object is not applicable for the arguments (FileInputStream, String)

    Hi Sidhradh, could you please help ?

    ReplyDelete
    Replies
    1. Import the correct package for IOUtils, use below:
      import org.apache.commons.io.IOUtils;

      Delete
    2. Excellent, Thank you very much.

      I have added the following dependency (https://mvnrepository.com/artifact/commons-io/commons-io/2.6) in pom.xml (as I was using a maven project)


      commons-io
      commons-io
      2.6


      Error is gone.

      PS: User might need to add the put.json etc in the project for the tests to pass in their machine.

      Anyway, such a useful resource for learners. Thank you again.

      Delete

All Time Popular Posts

Most Featured Post

Scenario Based Frequently Asked Interview Q&A on TestNG

  🔺 LinkedIn: https://www.linkedin.com/in/sidharth-shukla-77b53145/ 🔺 Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl Welcome to our compr...