Google Chrome doesn't allow setting request headers from the command line, but you can use the request hook to add a header value based on the environmental variable.
Please refer to the following example. Note that RequestLogger was added only for demonstration purposes:
// test.js
import { RequestLogger, RequestHook } from 'testcafe';
fixture `Set a Custom Referer`
.page`http://example.com/`;
export class MyRequestHook extends RequestHook {
constructor(requestFilterRules, responseEventConfigureOpts) {
super(requestFilterRules, responseEventConfigureOpts);
}
async onRequest(event) {
event.requestOptions.headers['CustomHeader'] = process.env.HEADER_VALUE;
}
async onResponse(event) {
}
}
const hook = new MyRequestHook();
const logger = RequestLogger(
["https://devexpress.github.io/testcafe/example/", "http://example.com/"],
{
logRequestHeaders: true,
}
);
test
.requestHooks([hook, logger])
('Check the Referer Value', async t => {
await t
.navigateTo('https://devexpress.github.io/testcafe/example/')
.expect(logger.contains(r => r.request.url === 'https://devexpress.github.io/testcafe/example/')).ok()
.expect(logger.requests[0].request.headers['CustomHeader']).eql(process.env.HEADER_VALUE);
});
Now, you can run tests on the test server with the following command (POSIX):
export HEADER_VALUE='my value'
testcafe chrome test.js
Please refer to this documentation topic for more information: Create a Custom Request Hook.