7_API testing Interview Questions and Answers
7_API testing Interview Questions and Answers
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.
29. What are the main differences between API and Web Service?
- All Web services are APIs but not all APIs are Web services.
- Web service uses three styles of use: SOAP, REST and XML-RPC for
communication whereas API may be exposed in multiple ways.
- Web service needs a network to operate but APIs don’t need a network to operate.
Explain API Chaining with examples ?
https://www.linkedin.com/posts/sidharth-shukla-77b53145_sidpost-apitesting-
testautomation-activity-7118075315326746624-tJQh?
utm_source=share&utm_medium=member_desktop
- REST (Representational State Transfer) is an architectural style for developing web services
which exploit the ubiquity of HTTP protocol and uses HTTP method to define actions. It revolves
around resource where every component being a resource that can be accessed through a
shared interface using standard HTTP methods.
- In REST architecture, REST Server provides access to resources and client accesses and
makes these resources available.
- Each resource is identified by URIs or global IDs, and REST uses multiple ways to represent a
resource, such as text, JSON, and XML.
What all 4xx status code you have used in your project, can you explain some of the
error codes?
https://www.linkedin.com/posts/sidharth-shukla-77b53145_testing-testautomation-automation-
activity-7091624769585905664-eUKK?utm_source=share&utm_medium=member_desktop
What is JSON Schema and how to perform Schema Testing with Rest Assured?
Ans:
https://www.linkedin.com/posts/sidharth-shukla-77b53145_testing-sidpost-apitesting-activity-
7178593745385238528-ysS4?utm_source=share&utm_medium=member_desktop
What is Input injection and what are different ways to simulate user
input?
Ans::
Input Injection: It is the act of simulating user input, in several ways you can
simulate user input.
Ans::This is one of the fundamental Web API interview questions. Bellows are four common Web
API architectural styles:
Which purpose does the OPTIONS method serve for the RESTful Web
services?
Ans: The OPTIONS Method lists down all the operations of a web service supports. It creates
read-only requests to the server.
GET/POST/PUT/PATCH/DELETE/HEAD/OPTIONS
P2 : PUT
P3 : PATCH
accepted
request
4xx client error – the request contains bad syntax or cannot be fulfilled
5xx server error – the server failed to fulfil an apparently valid request
3. What are the status codes you have come across in your API testing
project?
https://automationreinvented.blogspot.com/2019/03/what-are-most-used-api-
status-codes.html
The HEAD method asks for a response identical to that of a GET request, but
The OPTIONS method returns the HTTP methods that the server supports for
Let’s list down when to use POST and when to use PUT :
"id"
PUT is Idempotent
POST is not Idempotent.
10. Http vs HTTPS
In HTTP, URL begins with “http://” whereas URL starts with “https://”
1. HTTP uses port number 80 for communication and HTTPS uses 443
5. HTTP does not require any certificates and HTTPS needs SSL
Certificates
11. Automate GET method and validate the status code?
assertEquals(resp.getStatusCode(),200);
Response resp=given().when().get("https://reqres.in/api/users/2");
assertEquals(resp.getBody().asString(),200);
13. Automate GET method and verify value from response body?
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
@Test
public void validateQueryParamInGiven() {
when().get("https://reqres.in/api/users");
assertEquals(resp.getStatusCode(),200);
System.out.println(resp.getBody().asString());
}
when().get("https://gorest.co.in/public-api/users");
assertEquals(resp.getStatusCode(),200);
System.out.println(resp.getBody().asString());
}
@Test(description="validate with jsonpath and json object and pass post body as
json file")
public void MethodValidationPUT() throws IOException, ParseException {
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");
case a file or a resource already exists at that URI, the PUT method
new one.
@Test(description="validate with jsonpath and json object and pass post body as
json file")
public void MethodValidationPUT() throws IOException, ParseException {
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");
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
{
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");
Ans: When using SOAP, users often get the firewall security mechanism as the biggest obstacle.
This block all the ports leaving few like HTTP port 80 and the HTTP port used by SOAP that
bypasses the firewall. The technical complaint against SOAP is that it mixes the specification for
Ans: Contract testing is immediately applicable anywhere where you have two
services that you need to communicate - such as an API client and a web front-end.
Although a single client and a single service is a common use case, contract testing
really shines in an environment with many services (as is common for a microservice
architecture). Having well-formed contract tests makes it easy for developers to avoid
version hell. Contract testing is a killer app for microservice development and
deployment.
To learn more about the use of the pact in contract testing please refer:
ContractTestingWithPact
What are the status codes you have come across in your API testing
project?
Do you have the scope set to test when you are adding the mentioned dependencies?
This limits the code from accessing that dependency's classes within the source code. That
is, we can access those classes within your testsources (ex:
${project.dir}/src/test/java/<package>,
${project.dir}/test/<package>.
If that is not your intended use case, just remove the scope attribute.
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.1.1</version>
</dependency>
Scenario 1:
Question:
You are tasked with testing the authentication functionality of an API using Rest
Assured. How would you approach this scenario?
Answer:
Firstly, I would ensure that I have clear documentation or understanding of the
authentication mechanism used by the API, whether it's basic authentication, OAuth, or
token-based authentication. Then, I would write test cases using Rest Assured to verify
that the authentication process works as expected. This would involve sending requests
with valid credentials and ensuring that the API responds with the expected status
codes and authentication tokens. Additionally, I would simulate scenarios such as
providing invalid credentials or missing authentication tokens to verify that the API
handles these cases appropriately, returning the correct error responses.
Scenario 2:
Question:
You need to test an API endpoint that retrieves user data based on certain criteria using
Rest Assured. How would you design your test cases for this scenario?
Answer:
To test the user data retrieval endpoint, I would first identify the criteria that can be used
to filter or retrieve specific user data, such as user IDs, usernames, or other attributes.
Then, I would design test cases to verify that the endpoint returns the correct user data
based on different combinations of criteria. This would involve sending requests with
various parameters using Rest Assured and validating that the API responds with the
expected user data. I would also include test cases to verify edge cases, such as
requesting data for non-existent users or providing invalid criteria, to ensure that the API
handles these scenarios gracefully and returns appropriate error responses.
Additionally, I would consider testing performance aspects by sending requests with
different load levels to assess the endpoint's scalability and response times.
https://drive.google.com/file/d/1vlhMeb0jSB9fH6Z78nMG-
iDg6pP8HADn/view?usp=sharing