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}` },
});
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.

Further reading

Problem with a question

Playwright · Middle · question 71

Test yourself

Take the Playwright · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz