Hi everyone,
I have a test file which goes through an end to end flow with the api endpoints. For one of the api requests an extra custom header is required. How can I use it only for that test which has that particular endpoint that requires it.
I have tried adding it inside the test block :
test.use({ extraHTTPHeaders: { 'custom-header': 'something', }, })
However, this is not working. Any ideas if this is possible please?
I can't add it in the playwright.config.ts file as it's only needed for 1 endpoint.
This thread is trying to answer question "How can I use extraHTTPHeaders only for a particular test in an end-to-end flow with API endpoints?"
Related Ask AI answer for "How do I set custom HTTP headers for a specific test in Playwright?".
Sure, you can set custom HTTP headers for a specific test in Playwright. Here's how you do it:
First, make sure you've got your Playwright configuration file (playwright.config.ts
). This is where you set global options for all tests.
But for a specific test, you'll need to modify the test file itself. Let's say it's my-test.spec.ts
. You'll need to import the necessary dependencies:
import { test } from '@playwright/test';
Before defining your test case, use the test.use()
method. This lets you pass in an object with the options you want. For extraHTTPHeaders
, you'd do it like this:
test.use({
extraHTTPHeaders: {
'Custom-Header': 'Value',
},
});
Then, you can define your test case using the test()
function:
test('My Test', async ({ page }) => {
// Your test code goes here...
});
This way, Playwright will apply the options you set with test.use()
to this specific test case. In this example, it sets a custom header named 'Custom-Header'
with a value of 'Value'
.
Remember, you can customize each test with different options using test.use()
. This lets you use different extraHTTPHeaders
for each scenario.
If you need more help, check out the official Playwright documentation or community resources.
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].