With Custom Runner, how do you run the tests with run-multiple?

#1

Hello everyone,

I have a custom runner built which works like charm now. However I am trying to run my tests in chunks for faster runtimes and of course parallelization.

This is my custom runner file:

const { prompt } = require('inquirer');
const Container = require('codeceptjs').container;
const Codecept = require('codeceptjs').codecept;

const { loadLocalSettings } = require('./configs/managers/localSettings');
const { setEnvironmentVars } = require('./configs/managers/envVariables');

const launchTestRunner = (settings) => {
   // setting env variables first based on answers or local settings
   setEnvironmentVars(settings);

   // codecept options passed to the runner
   const opts = {
       steps: true,
       debug: (process.env.LOG_LEVEL === 'debug' || process.env.LOG_LEVEL === 'verbose'),
       verbose: (process.env.LOG_LEVEL === 'debug' || process.env.LOG_LEVEL === 'verbose'),
       grep: process.env.TAGS || null,
       multiple: 'parallel',
   };

   // we require the config here because earlier we don't have the var values set yet
   // eslint-disable-next-line global-require
   const codeceptConf = require('./codecept.conf.js');

   // initialize a new codecept runner with config, evn vars and runner options
   const codecept = new Codecept(codeceptConf.config, opts);

   // initialize the globals
   codecept.initGlobals(__dirname);

   // running bootstrap function to start webdriver or docker
   codecept.runBootstrap(codeceptConf.config.bootstrap);

   // creating new container with the config generated and options
   Container.create(codeceptConf.config, opts);

   // initialize and run all the hooks
   codecept.runHooks();

   // load tests based on chosen suite
   if (settings.suite === 'gherkin') {
       codecept.loadTests(codeceptConf.config.gherkin.features);
   } else if (settings.suite === 'specs') {
       codecept.loadTests(codeceptConf.config.tests);
   }

   // run the selected tests
   codecept.run();
};

const questions = [
   {
       type: 'list',
       name: 'suite',
       message: 'Which set of tests would you like to run?',
       choices: ['Gherkin', 'API', 'Specs'],
       filter: val => val.toLowerCase(),
   },
   {
       type: 'list',
       name: 'browser',
       message: 'Which browser would you like to use for your run?',
       choices: ['chrome', 'firefox'],
       default: 'chrome',
       filter: val => val.toLowerCase(),
       when: answers => (answers.suite === 'gherkin' || answers.suite === 'specs'),
   },
   {
       type: 'list',
       name: 'environment',
       message: 'Which environment would you like to test?',
       choices: ['staging', 'Live'],
       filter: val => val.toLowerCase(),
   },
   {
       type: 'input',
       name: 'tags',
       message: 'Would you like to use a tag-expression?',
       default: '@smoke',
       when: answers => (answers.suite === 'gherkin' || answers.suite === 'api' || answers.suite === 'specs'),
   },
   {
       type: 'list',
       name: 'screensize',
       message: 'What screensize would you like to emulate?',
       choices: ['mobile', 'tablet', 'desktop'],
       default: 'mobile',
       when: answers => (answers.suite === 'gherkin' || answers.suite === 'specs'),
   },
   {
       type: 'list',
       name: 'seleniumLocation',
       message: 'Run headless in docker, or selenium-standalone so you can see the action?',
       choices: ['docker', 'selenium-standalone'],
       default: 'docker',
       when: answers => (answers.suite === 'gherkin' || answers.suite === 'specs'),
   },
   {
       type: 'list',
       name: 'logLevel',
       message: 'Log Level?',
       choices: ['info', 'debug', 'verbose'],
       when: answers => (answers.suite === 'gherkin' || answers.suite === 'specs'),
   },
];

const localSettings = loadLocalSettings();

if (localSettings) {
   launchTestRunner(localSettings);
} else {
   prompt(questions)
       .then(answers => launchTestRunner(answers))
       .catch((error) => {
           console.error(error);
           process.exit(1);
       });
}

The grep attribute in the opts object works perfectly fine, however the multiple attribute does not although I do have this in my Config

multiple: {
        parallel: {
            MultiChrome,
        },
        smoke: {
            grep: '@smoke',
            MultiChrome,
        },
    },

MultiChrome is my browser config for parallel running and is set as follows:

chrome: {
        browser: 'chrome',
        desiredCapabilities: {
            'goog:chromeOptions': {
                args: [
                    '--headless',
                    '--disable-gpu',
                    '--no-sandbox',
                    '--disable-setuid-sandbox',
                    '--ignore-certificate-errors',
                    `--user-agent=${(process.env.SCREENSIZE === 'mobile') ? MOBILE_UA : DESKTOP_UA}`,
                    // uncomment the following line if you need the browser console
                    // '--auto-open-devtools-for-tabs',
                ],
            },
        },
    },
    firefox: {
        browser: 'firefox',
        desiredCapabilities: {
            'moz:firefoxOptions': {
                args: [
                    '-headless',
                    '-purgecaches',
                    `--window-size=${getDevice(process.env.SCREENSIZE).width}x`
                    + `${getDevice(process.env.SCREENSIZE).height}`,
                    // uncomment the following line if you need the browser console
                    // '-jsconsole',
                ],
                prefs: {
                    'general.useragent.override': (process.env.SCREENSIZE === 'mobile') ? MOBILE_UA : DESKTOP_UA,
                },
                log: {
                    level: 'trace',
                },
            },
            acceptInsecureCerts: true,
        },
    },
    MultiChrome: {
        chunks: 4,
        browsers: [{
            browser: 'chrome',
            desiredCapabilities: {
                'goog:chromeOptions': {
                    args: [
                        '--headless',
                        '--disable-gpu',
                        '--no-sandbox',
                        '--disable-setuid-sandbox',
                        '--ignore-certificate-errors',
                        `--user-agent=${(process.env.SCREENSIZE === 'mobile') ? MOBILE_UA : DESKTOP_UA}`,
                    ],
                },
            },
        }],
    },

Unfortunately this isn’t very clearly documented when it comes to custom runners so any help would be greatly appreciated!.

Thank you!

0 Likes