//refer https://markus.oberlehner.net/blog/crazy-powerful-nightwatch-cucumber-step-definitions/ const { defineStep } = require('@cucumber/cucumber'); const stepDefinitions = []; function register(pattern, callback) { stepDefinitions.push({ callback, pattern }); // console.log("In register with pattern: " + pattern); return defineStep(pattern, callback); } async function run(stepDescription) { console.log("Inside 'run' with: " + stepDescription); await stepDefinitions.some(async ({ callback, pattern }) => { const match = await stepDescription .replace(/^(Given|When|Then|And) /, ``) .match(pattern); if (!match) { return false; } else { console.log("Found a match"); } const parameters = await match.slice(1); console.log(" run", pattern); await callback(...parameters); return true; }); } module.exports = { Given: register, When: register, Then: register, register, run, };