0

I've searched related topics. The most significant I found is this Setting user-agent in browsers with testcafe

But it doesn't provide any real aswers.

My goal is to run the test spoofing a different OS: Since I'm in Linux and the app I'm testing isn't supported for that, it shows a couple of warnings that I would want to get rid when tests are running.

We tried cypress, in which you just add the UserAgent string on a config file and that's it. But I haven't found a straightforward way of doing it on testcafe without a CLI parameter.

Is there a way to spoof an OS or userAgent in testcafe?

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
user3303019
  • 142
  • 1
  • 11

1 Answers1

1

You can modify user-agent using the RequestHooks mechanism. I prepared an example to demonstrate this approach:

import { RequestHook } from 'testcafe';

class UserAgentRequestHook extends RequestHook {
    onRequest (e) {
        e.requestOptions.headers['user-agent'] = 'Mozilla/5.0 (Android 4.4; Tablet; rv:41.0) Gecko/41.0 Firefox/41.0';
    }

    onResponse (e) {
    }
}

const hook = new UserAgentRequestHook();

fixture `f`
    .page `https://www.whatismybrowser.com/detect/what-is-my-user-agent/`;

test.requestHooks(hook)(`test`, async t => {
    await t.debug();
});

Please note that TestCafe is using UserAgent internally, so incorrect UA value can lead to unpredictable results.

Alex Kamaev
  • 6,198
  • 1
  • 14
  • 29