Skip to content

Commit

Permalink
feat: improve gpo command (#3625)
Browse files Browse the repository at this point in the history
* feat: improve gpo command

* fix: remove debugging
  • Loading branch information
kobenguyent committed Jun 4, 2023
1 parent 208d427 commit 84456c0
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions lib/command/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,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);
Expand Down Expand Up @@ -111,7 +134,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);
Expand All @@ -125,13 +156,24 @@ module.exports.pageObject = function (genPath, opts) {
}
actor = `require('${actorPath}')`;
}
if (!safeFileWrite(pageObjectFile, pageObjectTemplate.replace('{{actor}}', actor))) return;

const name = lcfirst(result.name) + ucfirst(kind);
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');

Expand Down

0 comments on commit 84456c0

Please sign in to comment.