← Back to Discord Forum

How can I use extrahttpheaders only for a particular test

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?"

4 replies

Code snipper you added should work

If it does not then file a bug issue on github

If you are hitting the api directly via the request context you would need to ensure the extra headers is configured on that context, but if you are using browser context then the above should work. If you add the above to your global settings it does work?

I understand you only need it for one, just wondering if it does apply it.


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:

  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.

Related Discord Threads

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

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 luc@ray.run.