1

I have 2 runner.js in testcafe and I want to run these 2 runners together, how should I do that?

runner1.js

const createTestCafe = require('testcafe');
let testcafe = null;

createTestCafe()
.then(tc => {
    testcafe     = tc;
    const runner = testcafe.createRunner();

    let id;
    const deadlinePromise = new Promise((resolve,reject) => {
        id=setTimeout(() => {
       clearTimeout(id);
       reject('testcase couldnt meet the actual preferred time');
        },215000)
     });

const runPromise=runner
    .src(test1.ts)  
    .browsers('chrome:headless')
    .reporter('html-testrail')
    .run({skipJsErrors:true})             


race =Promise.race([runPromise,deadlinePromise])
race.then((res) => console.log(res))      

})

       .catch(failedCount => {
           console.log('Tests1 failed: ' + failedCount);
            testcafe.close();
        })

runner2.js -- similar but different src and different timing in deadlinePromise

 const createTestCafe = require('testcafe');
  let testcafe = null;

createTestCafe()
.then(tc => {
    testcafe     = tc;
    const runner = testcafe.createRunner();

    let id;
    const deadlinePromise = new Promise((resolve,reject) => {
        id=setTimeout(() => {
       clearTimeout(id);
       reject('testcase couldnt meet the actual preferred time');
        },150000)
     });

const runPromise=runner
    .src(test2.ts)  
    .browsers('chrome:headless')
    .reporter('html-testrail')
    .run({skipJsErrors:true})             


race =Promise.race([runPromise,deadlinePromise])
race.then((res) => console.log(res))      

})

       .catch(failedCount => {
           console.log('Tests1 failed: ' + failedCount);
            testcafe.close();
        }) 

so basically both the runners contain promise.race. Now I want to run these 2 runners together either from the command line or from another runner. how do I do that?

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
TS0306
  • 177
  • 12

1 Answers1

2

I see you already asked a similar question in the master runner in testcafe for several other runners? thread, so please do not duplicate questions.

I prepared a sample which demonstrates the required behavior. See the following code:

const createTestCafe = require('testcafe');

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

    let timeout1, timeout2;

    const runner1RunPromise = testCafe
        .createRunner()
        .src('test1.js')
        .reporter([{ name: 'spec', output: 'report1.txt' }])
        .browsers('chrome')
        .run();

    const deadline1Promise = new Promise((resolve, reject) => {
        timeout1 = setTimeout(() => {
            runner1RunPromise.cancel();

            console.log('runner 1 canceled');

            resolve();
        }, 5000);
    });


    const runner2RunPromise = testCafe
        .createRunner()
        .src('test2.js')
        .reporter([{ name: 'spec', output: 'report2.txt' }])
        .browsers('firefox')
        .run();

    const deadline2Promise = new Promise((resolve, reject) => {
        timeout2 = setTimeout(() => {
            runner2RunPromise.cancel();

            console.log('runner 2 canceled');

            resolve();
        }, 40000);
    });

    const race1 = Promise.race([runner1RunPromise.then(() => clearTimeout(timeout1)), deadline1Promise]).then(() => { console.log('race 1 finished') });
    const race2 = Promise.race([runner2RunPromise.then(() => clearTimeout(timeout2)), deadline2Promise]).then(() => { console.log('race 2 finished') });

    await Promise.all([race1, race2]);

    console.log('finished');

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

Note that you do not need to create multiple testcafe instances. It's enough to create several runners. Refer to the following article which describes how to cancel a running task: https://devexpress.github.io/testcafe/documentation/using-testcafe/programming-interface/runner.html#cancelling-test-tasks

Thus, you can cancel your test execution on timeout or wait for all tests to finish.

Alex Kamaev
  • 6,198
  • 1
  • 14
  • 29
  • thank you! I understood the concept of cancelling tasks and thanks for pointing me to the article. – TS0306 Oct 09 '19 at 14:38
  • I had one more question , instead of doing reports in 2 separate runners is there a logic of having just one report for several runners? – TS0306 Oct 10 '19 at 15:25
  • 1
    At present, it is not possible to write a single report from several runners. I think, you can merge all report files in your run script. – Vladimir A. Oct 11 '19 at 13:35
  • @Airich how do I merge all the reports in the run script? – TS0306 Oct 11 '19 at 15:15
  • 1
    It depends on your report format. You can read the report files and write its content to a single file as you need or use any third-party tool. Here is an example for the json report: ` //... await testCafe.close(); const report1 = require('./report1.json'); const report2 = require('./report2.json'); const fs = require('fs'); fs.writeFileSync('report.json', JSON.stringify({ report1, report2 }, null, 2)); })();` – Dmitry Ostashev Oct 14 '19 at 08:47