BROWSER

HTTP traffic

HTTP traffic

Send requests, wait for them, and fail, mock or change them. Which keyword sees which request, and the rules they share for matchers, bodies and headers.

Browser has keywords to send HTTP requests, to wait for them and to change them:

KeywordDoesPage
HTTPSends a request from the page, with its fetchSending requests
Send API RequestSends a request from the context, outside any pageSending requests
Wait For Request, Wait For ResponseWait for a request or response of the pageWaiting for traffic
Abort Requests, Mock Responses, Modify ResponsesFail, answer or change requests of the pageRoute rules
Remove Route Rule, Remove All Route RulesEnd route rulesRoute rules

Page traffic and API traffic

There are two kinds of requests, and they see different things:

page traffic     the page loads, fetches, posts …  and HTTP                 Page ──▶ route rules ──▶ network ──▶ Server                   └── seen by Wait For Request / Wait For ResponseAPI traffic      Send API Request                 Context ───────────────▶ network ──▶ Server
Page traffic, HTTPSend API Request
Route rulesapplydo not apply
Wait For Request / Responsesee itdo not see it
Set Offlinerequests failno effect
Context cookiesby the credentials option of fetchalways; cookies it gets reach the pages
Extra HTTP headerssentsent, but not those set on=Page
HTTP credentialssentsent
Relative URLagainst the page URLagainst baseURL, fails without one
CORS, headers such as Cookiebrowser rules applyno rules
Needsa pagea context

So HTTP tests what the page can do, and Send API Request sets up or checks data next to it, for example with the session the page logged in with.

Matchers

Wait For * and the route rules take the same matcher:

MatcherExample
Glob: * stops at /, ** does not, {a,b} is either**/api/orders/*
JavaScript RegExp between slashes, with optional flags gimsuy/\/api\/orders\/\d+$/i
JavaScript arrow function that returns a booleanurl => url.pathname.endsWith('.png')
  • An invalid RegExp fails the keyword. It never falls back to a glob.
  • Anything that starts and ends with / is a RegExp: /api/users/ is one. Write **/api/users/ for the glob.
  • The arrow function gets the request URL in a route rule, and the request or response in Wait For *.
  • Escape backslashes for Robot Framework: \\d instead of \d.

Bodies

body of HTTP, Mock Responses and Modify Responses, request_body of Modify Responses and data of Send API Request take the same values:

ValueSent as
Dictionary, list, number, booleanCompact JSON, application/json
TextUTF-8, as given. The Content-Type differs per keyword, see its page
bytesUnchanged, byte for byte
Robot Framework SecretIts text, never logged

A Content-Type in headers always wins. A value JSON cannot hold, such as NaN or a date, fails before anything is sent.

Headers

Headers are a dictionary. A value is text, a Secret, a number or a boolean; numbers and booleans are sent as JavaScript writes them, 5 and true. In Modify Responses, None removes a header and "" sends it empty.

Responses

HTTP, Send API Request and Wait For Response return a dictionary with the same core keys: status, statusText, ok, url, headers, bytes, text and json. Each adds its own, see HttpResponse, APIResponse and CapturedResponse.

Secrets

Give tokens as a Robot Framework Secret, as a header value or as the whole body. The keywords log header names, never values. Two limits:

  • A Secret inside text, such as {"token": "${token}"}, becomes the text <secret>. Give the whole body as the Secret.
  • With enable_playwright_debug, the values show in the Playwright debug log.