To include an access_token
in extraHTTPHeaders
using @playwright/test, you can create a custom fixture that modifies network requests with extra headers. This fixture should be declared as a dependency for all your testing projects so that it runs before any tests are executed.
Here's a step-by-step guide on how to achieve this:
import { test, expect } from '@playwright/test';
test.extend({
authenticatedContext: async ({ context }, use) => {
const accessToken = await getAccessToken(); // Replace with your method to get the access token
await context.route('**/*', (route, request) => {
route.continue({
headers: {
...request.headers(),
Authorization: `Bearer ${accessToken}`,
},
});
});
await use(context);
},
});
import { test } from '@playwright/test';
test('Example test using access token', async ({ authenticatedContext }) => {
const page = await authenticatedContext.newPage();
await page.goto('https://ray.run/');
// Your test logic here
});
By following these steps, you can automatically include an access_token
in extraHTTPHeaders
for all your test projects using @playwright/test.
If you still have questions, please ask a question and I will try to answer it.
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].