Playwright · Middle · Question 71 of 75
How do you create a request context with an auth header for API tests?
const api = await playwright.request.newContext({
baseURL: "https://api.example.com",
extraHTTPHeaders: { Authorization: `Bearer ${token}` },
});- 1
request.newContextwithextraHTTPHeaders - 2Set the header in
page.gotooptions - 3Add the header to
playwright.config.tswebServer
Show the answer
Correct answer: request.newContext with extraHTTPHeaders
Explanation
A dedicated context keeps settings like base URL and headers. Remember to dispose() it when done.