无法与 Testcafe 黄瓜集成并行运行测试

时间:2021-03-04 21:13:43

标签: testing automated-tests cucumber e2e-testing testcafe

我已经使用以下 repo 来使用支持文件,用于 testcafe 与黄瓜的集成 https://github.com/rquellh/testcafe-cucumber/

当我 1 by 1 运行单个测试时,它工作正常,但是当我尝试并行运行测试时,它根本不起作用,直到现在我一直无法找到解决方案。

Hooks 文件如下

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;

这是运行测试的命令

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

0 个答案:

没有答案