API Automation Interview Questions
API Automation Interview Questions
• GET: Retrieves data from the server. This method is used to request data from a
specified resource. It is read-only and does not change the state of the resource.
given()
.when()
.get("https://api.example.com/users")
.then()
.statusCode(200);
• POST: Sends data to the server to create a new resource. This method submits data to be
processed to a specified resource.
given()
.contentType("application/json")
.body("{ \"name\": \"John\", \"age\": 30 }")
.when()
.post("https://api.example.com/users")
.then()
.statusCode(201);
• PUT: Updates an existing resource on the server. If the resource does not exist, it can
create it.
given()
.contentType("application/json")
.body("{ \"name\": \"John\", \"age\": 31 }")
.when()
.put("https://api.example.com/users/1")
.then()
.statusCode(200);
given()
.when()
.delete("https://api.example.com/users/1")
.then()
.statusCode(204);
given()
.contentType("application/json")
.body("{ \"age\": 32 }")
.when()
.patch("https://api.example.com/users/1")
.then()
.statusCode(200);
2. What is the PUT method?
The PUT method is used to update an existing resource or create a new resource if it does not
exist. It is idempotent, meaning multiple identical requests should have the same effect as a
single request.
Example:
given()
.contentType("application/json")
.body("{ \"name\": \"John\", \"age\": 30 }")
.when()
.put("https://api.example.com/users/1")
.then()
.statusCode(200);
In this example, a user with ID 1 is updated with the name "John" and age 30.
given()
.contentType("application/json")
.body("{ \"name\": \"John\", \"age\": 30 }")
.when()
.post("https://api.example.com/users")
.then()
.statusCode(201);
given()
.contentType("application/xml")
.body("<user><name>John</name><age>30</age></user>")
.when()
.post("https://api.example.com/users")
.then()
.statusCode(201);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("name", "John")
.formParam("age", "30")
.when()
.post("https://api.example.com/users")
.then()
.statusCode(201);
given()
.multiPart(new File("/path/to/file"))
.when()
.post("https://api.example.com/upload")
.then()
.statusCode(201);
• PUT:
o Idempotent: Multiple identical requests have the same effect as a single request.
o Used to update an existing resource or create a resource if it does not exist.
o Example:
given()
.contentType("application/json")
.body("{ \"name\": \"John\", \"age\": 30 }")
.when()
.put("https://api.example.com/users/1")
.then()
.statusCode(200);
• POST:
o Not idempotent: Multiple identical requests can create multiple resources.
o Used to create a new resource.
o Example:
given()
.contentType("application/json")
.body("{ \"name\": \"John\", \"age\": 30 }")
.when()
.post("https://api.example.com/users")
.then()
.statusCode(201);
{ "name": "John" }
{ "age": 30 }
{
"address": {
"street": "123 Main St",
"city": "Anytown"
}
}
To assert the uniqueness of an ID, you can retrieve the list of existing IDs and ensure that the
new ID is not already present in the list.
Example:
7. Let’s say for a bank user with multiple accounts, how do you verify account
numbers array?
To verify the account numbers array, you can assert that the array contains the expected account
numbers.
Example:
given()
.when()
.get("https://api.example.com/users/1/accounts")
.then()
.statusCode(200)
.body("accounts", hasItems("123456", "789012", "345678"));
8. Represent users API response in the form of JSON (structure of the response).
{
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"accounts": [
{
"accountNumber": "123456",
"balance": 1000.0
},
{
"accountNumber": "789012",
"balance": 2500.5
}
]
}
A mocking response is a simulated response for testing purposes. It is used to test the API client
in isolation from the actual server, ensuring that the client can handle various scenarios without
relying on the live server.
given()
.when()
.get("http://localhost:8080/users/1")
.then()
.statusCode(200)
.body("name", equalTo("John Doe"));
wireMockServer.stop();
10. What is API automation testing? How does it differ from UI automation
testing?
API automation testing involves testing the APIs directly to ensure they meet functionality,
performance, and reliability expectations. It focuses on business logic, data responses, and error
handling without a GUI.
UI automation testing involves testing the application's user interface to ensure it behaves as
expected when interacted with by a user. It focuses on the graphical interface and user
interactions.
11. What are the advantages of API automation testing?
12. How do you select the appropriate tools and frameworks for API automation
testing?
• Language Compatibility: Ensure the tool supports the programming language used in
your project.
• Ease of Use: Tools should be easy to configure and use.
• Community Support: Strong community and documentation support are essential.
• Integration: The ability to integrate with CI/CD pipelines and other tools.
• Flexibility: Support for different data formats (JSON, XML) and authentication methods
(OAuth, API keys).
14. What are the commonly used HTTP methods in API testing?
15. What is the difference between GET and POST methods in API testing?
• GET:
o Used to retrieve data from the server.
o Does not change the state of the resource.
o Idempotent: Multiple identical requests have the same effect.
o Example:
given()
.when()
.get("https://api.example.com/users")
.then()
.statusCode(200);
• POST:
o Used to send data to the server to create a new resource.
o Can change the state of the resource.
o Not idempotent: Multiple identical requests can create multiple resources.
o Example:
given()
.contentType("application/json")
.body("{ \"name\": \"John\", \"age\": 30 }")
.when()
.post("https://api.example.com/users")
.then()
.statusCode(201);