jeudi 4 mars 2021

Unable to run tests in parallel with Testcafe cucumber integration

I have used following repo to used support files for testcafe integration with cucumber https://github.com/rquellh/testcafe-cucumber/

When I run single test 1 by 1 then it works fine, but when I try to run tests in parallel, it does not work at all and till now I have been unable to find solution for that.

Hooks files are as follows

const fs = require('fs');
const createTestCafe = require('testcafe');
const testControllerHolder = require('../support/testControllerHolder');
const {AfterAll, setDefaultTimeout, Before, After, Status} = require('@cucumber/cucumber');
const errorHandling = require('../support/errorHandling');
const TIMEOUT = 200000;

let isTestCafeError = false;
let attachScreenshotToReport = null;
let cafeRunner = null;
let n = 0;

function createTestFile() {
    fs.writeFileSync(`./${process.env.CUCUMBER_WORKER_ID}_test.js`,
        'import errorHandling from "./features/support/errorHandling.js";\n' +
        'import testControllerHolder from "./features/support/testControllerHolder.js";\n\n' +

        'fixture("fixture")\n' +

        'test\n' +
        '("test", testControllerHolder.capture)')
}

function runTest(iteration, browser) {
    let runner = null;

    createTestCafe('localhost')
        .then(function(tc) {
            cafeRunner = tc;
            runner = tc.createRunner();
            return runner
                .src(`./${process.env.CUCUMBER_WORKER_ID}_test.js`)
                .screenshots('reports/screenshots/', true) //take screenshot on failure
                .browsers(browser)
                .run()
                .catch(function(error) {
                    console.log(error);
                });
        })
        .then(function(report) {
        });
}

setDefaultTimeout(TIMEOUT);

Before(function() {
    runTest(n, this.setBrowser());
    createTestFile();
    n += 2;
    return this.waitForTestController.then(function(testController) {
        return testController.maximizeWindow();
    });
});

After(function() {
    fs.unlinkSync(`./${process.env.CUCUMBER_WORKER_ID}_test.js`);
    testControllerHolder.free();
});

After(async function(testCase) {
    const world = this;
    if (testCase.result.status === Status.FAILED) {
        isTestCafeError = true;
        attachScreenshotToReport = world.attachScreenshotToReport;
        errorHandling.addErrorToController();
        await errorHandling.ifErrorTakeScreenshot(testController)
    }
});

AfterAll(function() {
    let intervalId = null;

    function waitForTestCafe() {
        intervalId = setInterval(checkLastResponse, 500);
    }

    function checkLastResponse() {
        if (testController.testRun.lastDriverStatusResponse === 'test-done-confirmation') {
            cafeRunner.close();
            process.exit();
            clearInterval(intervalId);
        }
    }

    waitForTestCafe();
});

const getIsTestCafeError = function() {
    return isTestCafeError;
};

const getAttachScreenshotToReport = function(path) {
    return attachScreenshotToReport(path);
};

exports.getIsTestCafeError = getIsTestCafeError;
exports.getAttachScreenshotToReport = getAttachScreenshotToReport;

and this is the command to run tests

"test": "cucumber-js --world-parameters '{"browser": "chrome"}' --parallel 2 --format json:cucumberJsonTestReport/report.json --publish && node testReport.js",

Aucun commentaire:

Enregistrer un commentaire