Class APIRequest
java.lang.Object
net.serenitybdd.screenplay.playwright.interactions.api.APIRequest
- All Implemented Interfaces:
net.serenitybdd.core.SkipNested,net.serenitybdd.screenplay.Performable
Make API requests within the browser session context.
This allows API testing with the same cookies and authentication as the browser,
enabling hybrid UI+API test scenarios.
The API requests share the browser's session, so if you've logged in via the UI, API calls will automatically include authentication cookies.
Sample usage:
// Simple GET request
actor.attemptsTo(APIRequest.get("/api/users"));
// POST with JSON body
actor.attemptsTo(
APIRequest.post("/api/users")
.withJsonBody(Map.of("name", "John", "email", "john@example.com"))
);
// Request with headers
actor.attemptsTo(
APIRequest.get("/api/protected")
.withHeader("X-Custom-Header", "value")
);
// Query the response
int status = actor.asksFor(LastAPIResponse.statusCode());
String body = actor.asksFor(LastAPIResponse.body());
Map<String, Object> json = actor.asksFor(LastAPIResponse.jsonBody());
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic APIRequestCreate a DELETE request.failOnStatusCode(boolean fail) Configure whether to throw an exception on non-2xx status codes.static APIRequestCreate a GET request.static StringGet the key used to store the last API response in actor's memory.static APIRequestCreate a HEAD request.static APIRequestCreate a PATCH request.<T extends net.serenitybdd.screenplay.Actor>
voidperformAs(T actor) static APIRequestCreate a POST request.static APIRequestCreate a PUT request.static APIRequestCreate a request with a custom HTTP method.withBody(byte[] body) Set the request body as bytes.Set the request body as a string.withContentType(String contentType) Set the Content-Type header.withHeader(String name, String value) Add a header to the request.withHeaders(Map<String, String> headers) Add multiple headers to the request.withJsonBody(Object jsonBody) Set the request body as a JSON object.withQueryParam(String name, String value) Add a query parameter to the request URL.withQueryParams(Map<String, String> params) Add multiple query parameters to the request URL.withTimeout(int timeoutMs) Set the request timeout in milliseconds.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.serenitybdd.screenplay.Performable
then
-
Method Details
-
get
Create a GET request.- Parameters:
url- The URL or path to request
-
post
Create a POST request.- Parameters:
url- The URL or path to request
-
put
Create a PUT request.- Parameters:
url- The URL or path to request
-
patch
Create a PATCH request.- Parameters:
url- The URL or path to request
-
delete
Create a DELETE request.- Parameters:
url- The URL or path to request
-
head
Create a HEAD request.- Parameters:
url- The URL or path to request
-
to
Create a request with a custom HTTP method.- Parameters:
method- The HTTP methodurl- The URL or path to request
-
withHeader
Add a header to the request.- Parameters:
name- Header namevalue- Header value
-
withHeaders
Add multiple headers to the request.- Parameters:
headers- Map of header names to values
-
withQueryParam
Add a query parameter to the request URL.- Parameters:
name- Parameter namevalue- Parameter value
-
withQueryParams
Add multiple query parameters to the request URL.- Parameters:
params- Map of parameter names to values
-
withJsonBody
Set the request body as a JSON object. Automatically sets Content-Type to application/json.- Parameters:
jsonBody- Object to serialize as JSON (Map, List, or POJO)
-
withBody
Set the request body as a string.- Parameters:
body- The request body
-
withBody
Set the request body as bytes.- Parameters:
body- The request body
-
withContentType
Set the Content-Type header.- Parameters:
contentType- The content type (e.g., "application/xml")
-
withTimeout
Set the request timeout in milliseconds.- Parameters:
timeoutMs- Timeout in milliseconds
-
failOnStatusCode
Configure whether to throw an exception on non-2xx status codes. Default is false (does not throw).- Parameters:
fail- If true, throws on non-2xx responses
-
performAs
public <T extends net.serenitybdd.screenplay.Actor> void performAs(T actor) - Specified by:
performAsin interfacenet.serenitybdd.screenplay.Performable
-
getLastApiResponseKey
Get the key used to store the last API response in actor's memory.
-