To get the 'Date' header value using request.headersArray()
in @playwright/test, follow these steps:
request.headersArray()
to get an array of header objects:const headers = await request.headersArray();
let dateHeader;
for (const obj of headers) {
if (obj.name.toLowerCase() === 'date') {
dateHeader = obj.value;
break;
}
}
Now, dateHeader
contains the value of the 'Date' header. You can use this value as needed in your code.
Here's a complete example using @playwright/test:
import { test, expect } from '@playwright/test';
test('Get Date header value', async ({ page }) => {
await page.route('https://ray.run/**', async (route, request) => {
const headers = await request.headersArray();
let dateHeader;
for (const obj of headers) {
if (obj.name.toLowerCase() === 'date') {
dateHeader = obj.value;
break;
}
}
console.log('Date header:', dateHeader);
await route.continue();
});
await page.goto('https://ray.run/');
});
This test intercepts requests to https://ray.run/**
, retrieves the 'Date' header value, and logs it to the console.
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].