Principle:MarketSquare Robotframework browser HTTP Request Execution
HTTP Request Execution
Making HTTP requests from within a browser context using the page's fetch API in Robot Framework Browser.
Overview
The Robot Framework Browser library provides the ability to execute arbitrary HTTP requests (GET, POST, PUT, DELETE, PATCH, HEAD) directly from within a browser context. Unlike standalone HTTP client libraries (such as RequestsLibrary), these requests execute inside the browser's JavaScript context using the native fetch() API. This means they automatically share cookies, session state, and context-level HTTP configuration with the browser page.
Core Concept
The HTTP keyword in Robot Framework Browser does not use an external HTTP client. Instead, it:
- Receives the request parameters (URL, method, body, headers) from the Robot Framework test.
- Sends these parameters via gRPC to the Node.js Playwright wrapper process.
- The Node process calls
page.evaluate()to execute afetch()call within the page's JavaScript context. - The response (status, headers, body) is serialized and returned back through gRPC to the Python side.
- The Python side formats the response into a
DotDictfor convenient Robot Framework access.
This architecture means that the HTTP request inherits all browser-level state:
- Cookies set by previous page navigation or API calls are automatically included.
- Context-level headers specified via
extraHTTPHeadersinNew Contextare applied. - Base URL resolution from the context's
baseURLsetting works for relative paths. - CORS policies of the target page apply (since the request originates from the page's origin).
Supported HTTP Methods
The HTTP keyword supports all standard HTTP methods through the RequestMethod enum:
| Method | Description | Body Allowed |
|---|---|---|
GET |
Retrieve a resource. Default method. | No |
POST |
Create a resource or submit data. | Yes |
PUT |
Replace a resource entirely. | Yes |
PATCH |
Partially update a resource. | Yes |
DELETE |
Remove a resource. | Yes |
HEAD |
Retrieve headers only (no body in response). | No |
Automatic Content-Type Detection
When a request body is provided, the library automatically detects whether it is valid JSON. If the body can be parsed as JSON, the Content-Type header is automatically set to application/json. This detection happens in the _get_headers helper function before the request is dispatched. If the body is not valid JSON, no automatic Content-Type is added, and the caller must set it explicitly via the headers parameter.
Response Structure
The HTTP keyword returns a response object with the following attributes:
| Attribute | Type | Description |
|---|---|---|
status |
int |
HTTP status code (e.g., 200, 404, 500). |
statusText |
str |
Status text corresponding to the status code (e.g., "OK", "Not Found"). May not be available in all browsers. |
body |
str | Response body. Automatically parsed as a Python dictionary if the response Content-Type is application/json; otherwise returned as a plain string.
|
headers |
dict |
Dictionary of all response headers. |
ok |
bool |
True if the status code is in the 200-299 range.
|
url |
str |
The final URL after any redirects. |
redirected |
bool |
Whether the request was redirected. |
type |
str |
The response type (e.g., "basic", "cors"). |
The response is wrapped in a DotDict (from robot.utils), which allows dot-notation access in Robot Framework variables: ${response.status}, ${response.body.key}.
Session State Sharing
Because the request runs inside the page's JavaScript context, it has access to:
- Cookies from the browser's cookie jar for the current domain.
- localStorage and sessionStorage values.
- Service worker intercepts (if service workers are enabled in the context).
This makes the HTTP keyword particularly useful for testing APIs after a UI-based login, because the session cookies established during login are automatically sent with API requests.
Limitations
- A page must exist in the context. The
HTTPkeyword usespage.evaluate(), so calling it without a page raises an error. - Requests are subject to the page's CORS policy. If the page is at
http://localhost:3000and you call an API athttp://api.example.com, the browser's CORS restrictions may block the request. - GET requests cannot have a body. The Node-side implementation explicitly excludes the body for GET requests.
- The request does not support streaming or chunked transfers; the entire response body is read as text before being returned.