'Check TestCafe is running

I use TestCafe for testing a website. I use the testcafe inside my node module. Users can start the test out of the node presented website. I start it with:

    createTestCafe('localhost', 1337, 1338)
            .then(tc => {
                testcafe     = tc;
                const runner = testcafe.createRunner();

                inputStore.clLogin = req.body.name;
                inputStore.clPassword = req.body.pass;
                inputStore.metaFolder = '19233456';
                inputStore.metaUrl = req.body.channel;

                return runner
                    .src(['tests/temp.js'])
                    .browsers(myBrowser)
                    //.browsers('browserstack:Chrome')
                    .screenshots('allure/screenshots/', true)
                    .reporter('allure')
                    .run();
            })
            .then(failedCount => {
                console.log('Tests failed: ' + failedCount);
                testcafe.close();
                startReportGenerator();
                res.sendStatus(201);
            });

Is it possible to check, if there is a testcafe instance running? I want to avoid that another user who connects to the node server can start another instance of testcafe.



Solution 1:[1]

The runner.run method returns a promise. This means that Testcafe is running until this promise is resolved. You can add some custom static flags to your code:

if (isTestCafeRunning)
    createTestCafe()
    ...
    .then(() => {
        isTestCafeRunning = true;
        runner.run()
    })
    .then(() => {
        isTestCafeRunning = false;
    })

Please note that it's just a pseudo-code that demonstrates my idea.

Solution 2:[2]

I came up with a simple solution but works well for me:

in package.json

...
"testcafe:test": "E2E='true' testcafe ..."
...

Then simply

Use process.env.E2E anywhere

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Kamaev
Solution 2 vcardins