Search code examples
configurationautomated-testse2e-testingweb-testingtestcafe

testcafe runner to allow command line values


I have a test cafe runner defined like this

  const createTestCafe = require('testcafe');
  const glob = require('glob');

  let runner = null;
  let testcafe = null;

  const getTests = suite => {
        return new Promise(resolve => {
       glob(suite, (er, files) => resolve(files));
       });
   };

 const runTest = suite => {
    createTestCafe()
    .then(tc => {
        testcafe = tc;
        runner = testcafe.createRunner();
    })
    .then(() => {
        return getTests(suite);
    })
    .then(testFiles => {
        runner
            .src(testFiles)
            .browsers('chrome')
            .run()
            .then(failedCount => {
                console.log(failedCount);
                testcafe.close();
            });
    });
}

const suites = {
    suite1: 'src/tests/1.spec.ts',
        'src/tests/2.spec.ts, 
    suite2: 'src/tests/3.spec.ts',
 };

runTest(suites.suite1);

How do I pass the suite name, browser name, etc from the command line i.e. right now, the suite1 is hardcoded, but I want to select the suite name from the command line like

  node testCafeRunner --suite suite2 --browser firefox

How do I do this?

Also, if I have to bother .testcaferc.json and the runner, will testcafe look at both?


Solution

  • You can pass nodejs arguments as follows:  

    node testCafeRunner.js --suite=suite2 --browser=firefox
    

    In testCafeRunner.js:

    var argv = require('minimist')(process.argv.slice(2));
     
    const suite = argv.suite;
    const browser = argv.browser;
    

    Settings you specify when you run TestCafe from the command line and programming interfaces override settings from the configuration file (.testcaferc.json). TestCafe prints information about every overridden property in the console.

    See also:

    How do I pass command line arguments to a Node.js program?

    nodejs process.argv

    https://www.npmjs.com/package/minimist

    TestCafe Configuration File