Search code examples
testingautomated-testse2e-testingtestcafebrowser-automation

TestCafe RequestLogger - How to implement for every request in the test framework


We are trying to track down a network issue in our company which causes a Browser Disconnect General Error. I want to use RequestLogger timestamp to help us highlight when this intermittent issue occurs and also any additional request/response information at that time.

In the Request Logger documentation the .requestHooks(logger) is initiated at every test case level. And then console.log(logRecord.X.X) is used to log the record at that specific time.

But how can I have a continuous logging throughout my whole test framework without using console.log(logRecord.X.X) on every line?

Is it somehow possible to have the RequestLogger continuously running via my test-runner function?

if(nodeConfig.util.getEnv('NODE_ENV') == "jenkins-ci")
        {
            // @ts-ignore
            // createTestCafe("localhost", port1, port2).then(tc => {
            createTestCafe().then(tc => {
                this.testcafe = tc;
                this.runner = this.testcafe.createRunner();

                return this.runner
                    .src(testPath)
                    .filter(filterSettings)
                    .browsers(environment.browserToLaunch)
                    .concurrency(environment.concurrencyAmount)
                    .reporter(reporterSettings)
                    .run(runSettingsCi);
            })
            .then(failedCount => {
                console.log('Location ' + testPath + ' tests failed: ' + failedCount);
                this.testcafe.close();
                process.exit(0);
            })
            .catch((err) => {
                console.log('Location ' + testPath + ' General Error');
                console.log(err);
                this.testcafe.close();
                process.exit(1);
            });
        }

Solution

  • TestCafe doesn't allow attaching request hooks with the test runner class. At the same time, you can attach it to each fixture. RequestLogger will collect information about all requests.

    For example:

    import { Selector, RequestLogger } from 'testcafe';
    
    const logger = RequestLogger();
    
    fixture `Log all requests`
        .page`devexpress.github.io/testcafe`
        .requestHooks(logger)
        .afterEach(() => console.log(logger.requests));
    
    test('Test 1', async t => {
        await t
            .click(Selector('span').withText('Docs'))
            .click(Selector('a').withText('Using TestCafe'))
            .click(Selector('a').withText('Test API'));
    });
    
    test('Test 2', async t => {
        await t
            .click(Selector('span').withText('Docs'))
            .click(Selector('a').withText('Continuous Integration'))
            .click(Selector('a').withText('How It Works'));
    });