1

Looking to set custom referrer for my tests with Test Cafe but cannot find right solution for that. On Firefox you can easily change referrer with some plugins but how to do it within Test Cafe ?

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
mr.H
  • 13
  • 2

1 Answers1

4

You can use the Request Hooks mechanism for this purpose. I created an example to demonstrate this approach:

import { RequestHook } from 'testcafe';

fixture `fixture`
    .page `http://example.com`;


export class MyRequestHook extends RequestHook {
    constructor (requestFilterRules, responseEventConfigureOpts) {
        super(requestFilterRules, responseEventConfigureOpts);
    }
    async onRequest (event) {
        event.requestOptions.headers['Referer'] = 'http://my-modified-referer.com';
    }
    async onResponse (event) {
    }
}

const hook = new MyRequestHook();

test.requestHooks(hook)('referer', async t => {
    await t.navigateTo('https://www.whatismyreferer.com/');

    await t.debug();
});
Alex Kamaev
  • 6,198
  • 1
  • 14
  • 29