From 198214bd47f8b1b5df5d54b43b1a1efb54232c4f Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Thu, 23 Mar 2023 10:36:41 +0100 Subject: [PATCH 1/2] feat: improve gpo command --- lib/command/generate.js | 49 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/command/generate.js b/lib/command/generate.js index d735e1774..cbe0a1187 100644 --- a/lib/command/generate.js +++ b/lib/command/generate.js @@ -83,6 +83,29 @@ module.exports = { } `; +const poModuleTemplateTS = `const { I } = inject(); + +export = { + + // insert your locators and methods here +} +`; + +const poClassTemplate = `const { I } = inject(); + +class {{name}} { + constructor() { + //insert your locators + // this.button = '#button' + } + // insert your methods here +} + +// For inheritance +module.exports = new {{name}}(); +export = {{name}}; +`; + module.exports.pageObject = function (genPath, opts) { const testsPath = getTestRoot(genPath); const config = getConfig(testsPath); @@ -110,7 +133,15 @@ module.exports.pageObject = function (genPath, opts) { name: 'filename', message: 'Where should it be stored', default: answers => `./${kind}s/${answers.name}.${extension}`, - }]).then((result) => { + }, + { + type: 'list', + name: 'objectType', + message: 'What is your preferred object type', + choices: ['module', 'class'], + default: 'module', + }, + ]).then((result) => { const pageObjectFile = path.join(testsPath, result.filename); const dir = path.dirname(pageObjectFile); if (!fileExists(dir)) fs.mkdirSync(dir); @@ -124,13 +155,25 @@ module.exports.pageObject = function (genPath, opts) { } actor = `require('${actorPath}')`; } - if (!safeFileWrite(pageObjectFile, pageObjectTemplate.replace('{{actor}}', actor))) return; + const name = lcfirst(result.name) + ucfirst(kind); + console.log(extension); + if (result.objectType === 'module' && extension === 'ts') { + if (!safeFileWrite(pageObjectFile, poModuleTemplateTS.replace('{{actor}}', actor))) return; + } else if (result.objectType === 'module' && extension === 'js') { + if (!safeFileWrite(pageObjectFile, pageObjectTemplate.replace('{{actor}}', actor))) return; + } else if (result.objectType === 'class') { + const content = poClassTemplate.replace(/{{actor}}/g, actor).replace(/{{name}}/g, name); + if (!safeFileWrite(pageObjectFile, content)) return; + } + let data = readConfig(configFile); config.include[name] = result.filename; + + if (!data) throw Error('Config file is empty'); const currentInclude = `${data.match(/include:[\s\S][^\}]*/i)[0]}\n ${name}:${JSON.stringify(config.include[name])}`; - data = data.replace(/include:[\s\S][^\}]*/i, `${currentInclude}`); + data = data.replace(/include:[\s\S][^\}]*/i, `${currentInclude},`); fs.writeFileSync(configFile, beautify(data), 'utf-8'); From 3c004877a6e4776610b576cac851bf8678fd36f3 Mon Sep 17 00:00:00 2001 From: KobeNguyenT <7845001+kobenguyent@users.noreply.github.com> Date: Mon, 29 May 2023 06:37:52 +0200 Subject: [PATCH 2/2] fix: remove debugging --- lib/command/generate.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/command/generate.js b/lib/command/generate.js index cbe0a1187..af31f2dee 100644 --- a/lib/command/generate.js +++ b/lib/command/generate.js @@ -157,7 +157,6 @@ module.exports.pageObject = function (genPath, opts) { } const name = lcfirst(result.name) + ucfirst(kind); - console.log(extension); if (result.objectType === 'module' && extension === 'ts') { if (!safeFileWrite(pageObjectFile, poModuleTemplateTS.replace('{{actor}}', actor))) return; } else if (result.objectType === 'module' && extension === 'js') {