Hi there, I wondering anyone can help me. I'm trying to add a bearer token to a header within a test, however when I do, I'm getting 401, however, when I put it into the defineConfig it works fine. My question is does the 'extraHTTPHeaders' overwrite the headers that I set in the test or does it add to it. The docs are not clear as to what happens if you set https headers in the defineConfig, then set them within a test
An example of what I have set up
export default defineConfig({
use: {
// All requests we send go to this API endpoint.
extraHTTPHeaders: {
'Sub-Key': ${Env.subKey}
,
// 'Authorization': ${Env.auth}
*Note I've commented this out to show that I've tried it within the extra HTTPHeaders
},
} });
Test where the header has been added
test('Expect 200 OK when API is returned', async ({ request }) => { const response = await request.get(Url, { headers: { 'Authorization': MY TOKEN HERE } }); console.log(response); expect(response.status()).toBe(200); });
This thread is trying to answer question "Does the 'extraHTTPHeaders' overwrite the headers that I set in the test or does it add to it?"
if you have traces enabled you should be able to view the final report, and dive down into the trace file and see the http request that is being made on the network tab, this should include request headers and response headers, to help you debug your issue to see what is actually getting sent.
I had an issue were the extra headers were not getting applied when I was creating a new request context, so when I created the context I added it on the constructor. @butchmayhew is correct you will need to verify what is happening on the request header. One possible issue is that your Env.auth is not instantiated so it is getting applied as an undefined. You can fix this by adding a global setup to ensure your environment variables have values. Interested to know how you get one. Happy to help more.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].