Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Testcafe tests in sequence with html reports

This is not a question. I just want to share my solution for running Testcafe tests in sequence with HTML reports. Running tests in parallel on different browsers was not a solution for me. I have to wait for tests on one env to finish, then to run on the next env. It took me a while to figure this out, but it works for me. If someone has better solution, please inform me.

Just add this code (with your specific custom settings) into a runner file. i.e. runner.js and run it with node runner.js command.

The solution:

const createTestCafe = require('testcafe');
const fs = require('fs');

const browsers = [
    'chrome',
    'firefox'
];

let stream = null;
const runTest = async browser => {
    console.log('----------------- starting tests on ' + browser);
    await createTestCafe('localhost', 1337, 1338)
        .then(tc => {
            testcafe = tc;
            const runner = testcafe.createRunner();

            return runner
                .src([
                    "./smokeTests/someTests.js"
                ])
                .browsers(browser)
                .reporter('html', stream)
                .run();
        })
        .then(async failedCount => {
            console.log('Tests failed: ' + failedCount);
            await testcafe.close();
            return;
        });
}

const runAllBrowsers = async () => {

    for (const browser of browsers) {
        stream = fs.createWriteStream('./testResults' +'/report_' + browser + '.html'); 

        await runTest(browser);
        await testcafe.close();
    }
}

runAllBrowsers();

I used the original idea from https://github.com/DevExpress/testcafe/issues/2495. I'd like to thank nabrahamson for the original idea!

like image 1000
Miki P Avatar asked Mar 12 '26 12:03

Miki P


1 Answers

Thank you for sharing this solution with everyone.

You might want to contribute your sample to the testcafe-examples repo as well.

like image 110
Alex Skorkin Avatar answered Mar 14 '26 22:03

Alex Skorkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!