0

As a part of Automation testing, I want to point my Testcafe tests to a Test Prod server (Green) with the help of Custom Headers.

How can I pass custom headers to chrome instance while launching to perform tests as arguments.

Tried chrome:userProfile but the headers change for every release.

Require a generic way to pass custom headers.

Note: Testcafe script changes are not preferable.

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47

1 Answers1

3

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.

Shurygin.Sergey
  • 431
  • 2
  • 3
  • Thanks for your input, but Testcafe script changes are not preferred, I am looking for a way to pass arguments chrome browser through command line – Naga Giridhar Nov 17 '20 at 03:25
  • 1
    The Google Chrome browser doesn't allow setting request headers from the command line. So, it's impossible to accomplish the task in the way you want. Please use the suggested variant with a request hook. – mlosev Nov 19 '20 at 05:30