Welcome to the Playwright Java Quiz! This blog post features 25 multiple-choice questions that explore the concepts of API Testing in Playwright Java.
1. Which Playwright Java class is primarily used for API testing?
a) Page
b) Browser
c) APIRequestContext
d) Locator
Answer 1
c) APIRequestContext
APIRequestContext provides methods such as get(), post(), put(), patch(), and delete() for making HTTP requests independently of browser UI interactions.
2. How do you access the API request functionality from a Playwright instance?
a) playwright.api()
b) playwright.http()
c) playwright.request()
d) playwright.rest()
Answer 2
c) playwright.request()
playwright.request() returns the API request facility. A new APIRequestContext can then be created with newContext().
APIRequestContext request = playwright.request().newContext();
3. Which method sends a GET request using APIRequestContext?
a) fetchGet()
b) request()
c) get()
d) httpGet()
Answer 3
c) get()
APIResponse response = request.get("https://example.com/api/users");
The returned APIResponse contains the response status, body, headers, and other information.
4. Which class represents the response returned by a Playwright API request?
a) HttpResponse
b) APIResponse
c) ResponseBody
d) RequestResponse
Answer 4
b) APIResponse
APIResponse represents the server’s HTTP response and can be used to examine status codes, headers, and body content.
5. How do you obtain the HTTP status code from an APIResponse?
a) response.status()
b) response.statusCode()
c) response.getCode()
d) response.httpStatus()
Answer 5
a) response.status()
int status = response.status();
6. What does response.ok() indicate?
a) The status is exactly 200
b) The status is in the 200–299 range
c) The response contains JSON
d) The request contains authentication
Answer 6
b) The status is in the 200–299 range
APIResponse.ok() indicates that the HTTP response status belongs to the successful 200–299 range.
Therefore, 200, 201 and 204 are all examples of successful responses.
7. Which Playwright Java method is used to send a POST request?
a) request.create()
b) request.post()
c) request.sendPost()
d) request.insert()
Answer 7
b) request.post()
APIResponse response = request.post(url, options);
Request options can contain data, headers, query parameters, and other configuration.
8. Which option is commonly used to provide a POST request body?
a) setData()
b) setBody()
c) setJSON()
d) setPayloadBody()
Answer 8
a) setData()
Request option objects such as RequestOptions allow request data to be supplied using setData().
RequestOptions.create().setData("{\"name\":\"Vibha\"}");
9. Which method sends a DELETE request in Playwright Java?
a) request.remove()
b) request.delete()
c) request.destroy()
d) request.erase()
Answer 9
b) request.delete()
APIResponse response = request.delete("https://example.com/api/users/123");
10. A POST request times out from the client’s perspective. The test retries it. The first request actually reached the server, so now two orders could be created. What API design feature is especially useful here?
a) Idempotency tokens
b) Idempotency keys
c) Idempotency headers
d) Idempotency identifiers
Answer 10
b) Idempotency keys
For APIs that support them, an idempotency key helps the server recognize repeated attempts as the same logical operation.
If a network failure triggers a retry with the same key, the API can avoid creating a duplicate order.
11. A user contains:
{
"name": "Vibha",
"department": "QA",
"active": true
}
You only want to change:
{
"active": false
}
Which method is typically best suited to this partial update?
a) GET
b) DELETE
c) PATCH
d) HEAD
Answer 11
c) PATCH
PATCH conventionally represents a partial modification.
12. How can the response body be retrieved as text?
a) response.bodyText()
b) response.text()
c) response.getText()
d) response.content()
Answer 12
b) response.text()
This is useful for reading JSON, XML, plain text, or other text-based responses.
String body = response.text();
13. Which method provides response headers?
Choose one option
a) response.headers()
b) response.getHeaders()
c) response.httpHeaders()
d) response.headerMap()
Answer 13
a) response.headers()
14.What is the typical HTTP header used for Bearer-token authentication?
a) Authentication
b) Authorization
c) Security
d) Access-Token
Answer 14
b) Authorization
Bearer tokens are normally passed as:
Authorization: Bearer <token>
15. Consider this test:
APIResponse response = request.get("/users/123");
assertTrue(response.ok());
assertTrue(response.text().contains("Vibha"));
What is this test validating?
a) Only that the browser opened
b) Both successful HTTP response and expected response content
c) Only authentication
d) Only URL navigation
Answer 15
b) Both successful HTTP response and expected response content
The test performs two levels of validation:
response.ok()verifies a successful 2xx HTTP- response.
response.text().contains("Vibha")verifies expected data is present in the response body.
16. An authenticated standard user attempts:
Choose one option
DELETE /admin/users/123
The token is valid, but the user doesn’t have administrator permission. Which response is normally most appropriate?
a) 200
b) 201
c) 401
d) 403
Answer 16
d) 403
403 Forbidden normally indicates that the request is understood but the authenticated identity does not have permission to perform the operation.
17. Playwright API test sends:
Authorization: Bearer <expired-token>
The API rejects it. Which status would commonly be expected?
a) 201
b) 204
c) 401
d) 404
Answer 17
c) 401
An expired or invalid access token commonly produces 401 Unauthorized.
18. Ten API tests run in parallel and every test creates:
{
"username": "apiuser"
}
The server requires unique usernames. Tests fail intermittently. What’s the best solution?
a) Add longer sleeps
b) Disable assertions
c) Generate unique test data per test
d) Use the same account for every test
Answer 18
c) Generate unique test data per test
Parallel-safe tests should avoid shared mutable data.
String username = "apiuser-" + UUID.randomUUID();
19. What does HTTP status code 400 indicate?
a) Bad Request
b) Unauthorized
c) Forbidden
d) Internal Server Error
Answer 19
a) Bad Request
400 Bad Request means the server considers the client’s request invalid, for example due to malformed JSON or invalid request parameters.
20. Which status code usually means the server understood the request but refuses to authorize it?
Choose one option
a) 200
b) 401
c) 403
d) 404
Answer 20
c) 403
403 Forbidden means the server understood the request but refuses to authorize the operation.
21. What does HTTP status code 500 indicate?
a) Successful request
b) Client validation issue
c) Server-side failure
d) Redirect
Answer 21
c) Server-side failure
500 Internal Server Error indicates an unexpected problem occurred while the server processed the request.
22. Which option can configure additional HTTP headers for an API request context?
a) setHTTPHeaders()
b) setExtraHTTPHeaders()
c) addGlobalHeader()
d) setAPIHeaders()
Answer 22
b) setExtraHTTPHeaders()
Extra headers can be configured when creating an API request context. This is useful for headers that should be applied across multiple requests, such as authorization or custom API headers.
23. What is an important reason to call dispose() on an APIRequestContext?
a) It deletes the REST API
b) It releases resources associated with the context
c) It closes the application’s database
d) It clears the server cache
Answer 23
b) It releases resources associated with the context
When an API request context is no longer needed, disposing it releases its resources.
request.dispose();
24. What is negative API testing?
a) Testing only successful GET requests
b) Testing invalid inputs and error scenarios
c) Testing only DELETE endpoints
d) Testing APIs without assertions
Answer 24
b) Testing invalid inputs and error scenarios
Negative tests verify how an API handles incorrect or forbidden scenarios.
Examples include: Missing mandatory fields, Invalid authentication token, Expired token, Insufficient permissions
25. What is a good end-to-end Playwright strategy combining API and UI testing?
a) Perform every setup step through the UI
b) Use APIs for setup, UI for user behavior, and APIs where useful for verification/cleanup
c) Never mix API and browser testing
d) Replace all UI testing with API testing
Answer 25
b) Use APIs for setup, UI for user behavior, and APIs where useful for verification/cleanup
Playwright is particularly useful because API and browser automation can work together.
We would love to hear from you! Please leave your comments and share your scores in the section below