Skip to content

Instantly share code, notes, and snippets.

@adaemon
Last active October 3, 2023 06:40
Show Gist options
  • Save adaemon/0691b17687143d05ff3692db40c4848a to your computer and use it in GitHub Desktop.
Save adaemon/0691b17687143d05ff3692db40c4848a to your computer and use it in GitHub Desktop.
WebdriverIO Mocha Plugin : Using Mocha Tags
WDIO Mocha plugin is an useful adapter to use Mocha in WDIO. Mocha options can be provided through wdio config file or through command line argument. To invoke WDIO from npm scripts passing command line arguments is tricky.
Mocha Tags are useful to run specific methods tagged by common name. Requirements like running smoke test, remote, local, headless, fast, slow tests can be done using tags. https://github.com/mochajs/mocha/wiki/Tagging
Let say i have a package.json scripts section like below
"scripts": {
"smoke": "node node_modules/webdriverio/bin/wdio conf/wdio.conf.js --mochaOpts.grep=@Smoke",
"slow": "node node_modules/webdriverio/bin/wdio conf/wdio.conf.js --mochaOpts.grep=@Slow",
"fast": "node node_modules/webdriverio/bin/wdio conf/wdio.conf.js --mochaOpts.grep=@Fast"
}
The above example is straight forward. Smoke, Slow, Fast are tags in your spec file. You could call these scripts as
npm run-script smoke
npm run-script slow
npm run-script fast
If your requirement is to pass the grep from command line not from package.json scripts section, then you could do the following
Your package.json scripts section could be below
"scripts": {
"test-local": "node node_modules/webdriverio/bin/wdio conf/wdio.conf.js"
}
In Wdio.conf.js you could have mochoOpts section as below
mochaOpts: {
ui: 'bdd',
grep: process.env.npm_config_grep
}
Now you could call from the command line like below
npm run-script --grep=smoke test-local
npm run-script --grep=slow test-local
npm run-script --grep=fast test-local
process.env.npm_config_grep can be read from any javascript file which gives value passed from command line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment