简体   繁体   English

停止活动的 Testcafe 运行

[英]Stopping an active Testcafe run

I try to add a stop function to the Testcafe run.我尝试在 Testcafe 运行中添加停止 function。 I start Testcafe with:我开始Testcafe:

let testcafe = null;
let testcafeprom = null;

    testcafeprom = createTestCafe('localhost', 1337, 1338)
        .then(tc => {
            testcafe     = tc;
            const runner = testcafe.createRunner();
            return runner
                .src([__basedir + '/tests/temp.js'])
                .browsers(myBrowser)
                //.browsers('browserstack:Chrome')
                .screenshots(__basedir +'/allure/screenshots/', true)
                .reporter(['uistatusreporter', {name: 'allure',output: 'test/report.json'}])
                .run();
        })
        .then(failedCount => {
            testcafe.close();
            startReportGenerator();
            capcon.stopCapture(process.stdout);
            console.log("Testcafe Ende");


            if(failedCount>0){
                res.sendStatus(400);
                console.log('Tests failed: ' + failedCount);
                //res.statusCode = 400; //BadRequest 400
                /*
                res.json({
                    success: 'failed',
                    fails: failedCount
                });
                */
            }else{
                //res.statusCode = 200; //BadRequest 400
                res.sendStatus(200);
                console.log('All success');
                /*
                res.json({
                    success: 'ok',
                    fails: failedCount
                });
                */
            }
        })
        .catch(error => {
            testcafe.close();
            console.log('Tests failed: Testcafe Error');
            console.log(error);
            res.sendStatus(401);

        });

Then I added a function to stop the run:然后我添加了一个 function 来停止运行:

router.get('/stopit', async (req, res) => {

    testcafeprom.cancel();
    res.sendStatus(200);

});

As I understand is that createTestCafe will return a promise and in all to stop the promise I call testcafeprom.cancel();据我了解,createTestCafe 将返回 promise 并停止 promise 我调用testcafeprom.cancel(); or testcafeprom.stop();testcafeprom.stop();

But the browser is running and running.但是浏览器正在运行和运行。 A simple testcafe.close();一个简单的testcafe.close(); will stop Testcafe complete.将停止 Testcafe 完成。 But I want to stop it and not shoot it down.但我想阻止它而不是击落它。

Any suggestion to stop it a better way?有什么建议可以更好地阻止它吗?

Update: I have also tested the way to make the runner as promise:更新:我还测试了将跑步者制作为 promise 的方法:


    createTestCafe('localhost', 1337, 1338)
        .then(tc => {
            testcafe     = tc;
            const runner = testcafe.createRunner();
            testcafeprom =  runner
                .src([__basedir + '/tests/temp.js'])
                .browsers(myBrowser)
                //.browsers('browserstack:Chrome')
                .screenshots(__basedir +'/allure/screenshots/', true)
                .reporter(['uistatusreporter', {name: 'allure',output: 'test/report.json'}])
                .run();

            return testcafeprom;

        })

Adding also还添加

await testcafeprom.cancel();

This will have exact the same result as testCafe.close() , means everything is shoot down without any response.这将具有与testCafe.close()完全相同的结果,这意味着一切都被击落而没有任何响应。 Iam confused.我很困惑。

And finally I tried:最后我尝试了:

let runner = null;
    createTestCafe('localhost', 1337, 1338, void 0, true)
        .then(testcafe => {
            runner = testcafe.createRunner();
        })
        .then(() => {
            return runner
                .src([__basedir + '/tests/temp.js'])
                .browsers(myBrowser)
                //.browsers('browserstack:Chrome')
                .screenshots(__basedir +'/allure/screenshots/', true)
                .reporter(['uistatusreporter', {name: 'allure',output: 'test/report.json'}])
                .run()
                .then(failedCount => {
                    //testcafe.close();
                    startReportGenerator();
                    capcon.stopCapture(process.stdout);
                    console.log(`Finished. Count failed tests:${failedCount}`);
                    //process.exit(failedCount);
                    res.sendStatus(200);
                });
        })
        .catch(error => {
            startReportGenerator();
            capcon.stopCapture(process.stdout);
            console.log(error);
            //process.exit(1);
            res.sendStatus(401);
        });

But here is the same.但这里是一样的。 If I call await runner.stop() it looks like that the command will kill the whole process and nothing comes back to the promise.如果我调用 await runner.stop() ,看起来该命令将终止整个过程,并且没有任何内容返回到 promise。

Is this such a secret how to stop a running TestCafe instance or is the secret that the whole process is shoot down?这是如何停止正在运行的 TestCafe 实例的秘密,还是整个过程被击落的秘密?

It's difficult to say precisely why you face an issue since I cannot debug your project.由于我无法调试您的项目,因此很难准确地说出您为什么会遇到问题。 However, you are correct when you use cancel to stop test execution.但是,当您使用cancel来停止测试执行时,您是正确的。 The cancel method stops tests execution and closes the browser, but it does not stop TestCafe. cancel方法会停止测试执行并关闭浏览器,但不会停止 TestCafe。 This means that you can use the run method again, and it will start test execution and open browsers again.这意味着您可以再次使用run方法,它将开始测试执行并再次打开浏览器。

I created an example to demonstrate that this approach works.我创建了一个示例来证明这种方法有效。

Test code:测试代码:

fixture `fixture`
    .page `http://example.com`;

for (let i = 0; i < 50; i++) {
    test(`test A ${i}`, async t => {
        await t.click('h1');
    });
}

Testcafe start code: Testcafe 启动代码:

const createTestCafe = require('testcafe');

(async () => {
    const testCafe = await createTestCafe();

    let timeout;

    const runner = testCafe.createRunner();

    const runPromise = runner
        .src('test.js')
        .browsers('chrome')
        .run();

    const cancelPromise = new Promise(resolve => {
        timeout = setTimeout(() => {
            runPromise.cancel();

            resolve('canceled');
        }, 20000);
    });

    let value = await Promise.race([runPromise, cancelPromise]);

    if (value === 'canceled')
        console.log('test execution was canceled')

    value = await runner.run();

    console.log(`${value} failed tests`);

    await testCafe.close();
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM